homebridge-tahoma-fork
Version:
Sample Platform plugin for TaHoma and Cozytouch services (Somfy,Atlantic,Thermor,Sauter): https://github.com/dubocr/homebridge-tahoma
101 lines • 5.01 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Platform_1 = require("../Platform");
const overkiz_client_1 = require("overkiz-client");
const Mapper_1 = __importDefault(require("../Mapper"));
class GarageDoor extends Mapper_1.default {
constructor() {
super(...arguments);
this.expectedStates = ['core:OpenClosedPartialState', 'core:OpenClosedUnknownState', 'core:OpenClosedState'];
}
applyConfig(config) {
this.cyclic = config['cyclic'] || false;
this.cycleDuration = (config['cycleDuration'] || 5) * 1000;
}
registerMainService() {
const service = this.registerService(Platform_1.Services.GarageDoorOpener);
this.currentState = service.getCharacteristic(Platform_1.Characteristics.CurrentDoorState);
this.targetState = service.getCharacteristic(Platform_1.Characteristics.TargetDoorState);
this.targetState.onSet(this.setTargetState.bind(this));
this.cyclic = this.cyclic || this.device.hasCommand('cycle');
if (this.stateless) {
this.currentState.updateValue(Platform_1.Characteristics.CurrentDoorState.CLOSED);
this.targetState.updateValue(Platform_1.Characteristics.TargetDoorState.CLOSED);
}
return service;
}
getTargetCommands(value) {
if (this.device.hasCommand('cycle')) {
return new overkiz_client_1.Command('cycle');
}
else {
return new overkiz_client_1.Command(value ? 'close' : 'open');
}
}
async setTargetState(value) {
var _a;
const previousTarget = (_a = this.targetState) === null || _a === void 0 ? void 0 : _a.value;
const action = await this.executeCommands(this.getTargetCommands(value));
action.on('update', (state) => {
var _a, _b, _c;
switch (state) {
case overkiz_client_1.ExecutionState.IN_PROGRESS:
if (value === Platform_1.Characteristics.TargetDoorState.OPEN) {
(_a = this.currentState) === null || _a === void 0 ? void 0 : _a.updateValue(Platform_1.Characteristics.CurrentDoorState.OPENING);
}
else {
(_b = this.currentState) === null || _b === void 0 ? void 0 : _b.updateValue(Platform_1.Characteristics.CurrentDoorState.CLOSING);
}
break;
case overkiz_client_1.ExecutionState.COMPLETED:
if (this.stateless) {
this.onStateChanged(this.expectedStates[0], value === Platform_1.Characteristics.TargetDoorState.CLOSED ? 'closed' : 'open');
if (this.cyclic) {
setTimeout(() => {
this.onStateChanged(this.expectedStates[0], 'closed');
}, this.cycleDuration);
}
}
else if (this.cyclic) {
this.requestStatesUpdate(60).catch((e) => this.warn(e));
}
break;
case overkiz_client_1.ExecutionState.FAILED:
if (previousTarget) {
(_c = this.targetState) === null || _c === void 0 ? void 0 : _c.updateValue(previousTarget);
}
break;
}
});
}
onStateChanged(name, value) {
var _a, _b, _c;
let targetState;
if (this.expectedStates.includes(name)) {
switch (value) {
case 'open':
(_a = this.currentState) === null || _a === void 0 ? void 0 : _a.updateValue(Platform_1.Characteristics.CurrentDoorState.OPEN);
targetState = Platform_1.Characteristics.TargetDoorState.OPEN;
break;
case 'partial':
(_b = this.currentState) === null || _b === void 0 ? void 0 : _b.updateValue(Platform_1.Characteristics.CurrentDoorState.STOPPED);
targetState = Platform_1.Characteristics.TargetDoorState.OPEN;
break;
case 'closed':
(_c = this.currentState) === null || _c === void 0 ? void 0 : _c.updateValue(Platform_1.Characteristics.CurrentDoorState.CLOSED);
targetState = Platform_1.Characteristics.TargetDoorState.CLOSED;
break;
case 'unknown':
break;
}
}
if (this.targetState && targetState !== undefined) {
this.targetState.updateValue(targetState);
}
}
}
exports.default = GarageDoor;
//# sourceMappingURL=GarageDoor.js.map