homebridge-smartsystem
Version:
SmartServer (Proxy TCP sockets to the cloud, Smappee MQTT, Duotecno IP Nodes, Homekit interface)
105 lines • 4.76 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GarageDoor = void 0;
const types_1 = require("../duotecno/types");
const logger_1 = require("../duotecno/logger");
const accessory_1 = require("./accessory");
// Johan Coppieters Jun 2020
//
// Garagedoor
// - has security, so when opening you need to authenticate
// - TargetDoorState -> Characteristic.CurrentDoorState.OPEN, .STOPPED, .CLOSED
//
class GarageDoor extends accessory_1.Accessory {
constructor(homebridge, unit) {
super(homebridge, unit);
this.targetState = this.homebridge.Characteristic.TargetDoorState.CLOSED;
}
getAccessoryServices() {
// GarageDoorOpener needs authentication
const garageDoor = this.makeService(this.homebridge.Service.GarageDoorOpener);
this.attachServices(garageDoor);
return [garageDoor];
}
attachServices(door) {
door.getCharacteristic(this.homebridge.Characteristic.TargetDoorState)
.on('get', this.getTargetDoorState.bind(this))
.on('set', this.setDoorState.bind(this));
door.getCharacteristic(this.homebridge.Characteristic.CurrentDoorState)
.on('get', this.getDoorState.bind(this));
}
DT2HB(status) {
if (status == types_1.UnitState.kOpen)
return this.homebridge.Characteristic.CurrentDoorState.OPEN;
else if (status == types_1.UnitState.kClosing)
return this.homebridge.Characteristic.CurrentDoorState.CLOSING;
else if (status == types_1.UnitState.kClosed)
return this.homebridge.Characteristic.CurrentDoorState.CLOSED;
else if (status == types_1.UnitState.kOpening)
return this.homebridge.Characteristic.CurrentDoorState.OPENING;
else if (status == types_1.UnitState.kStopped)
return this.homebridge.Characteristic.CurrentDoorState.STOPPED;
else {
(0, logger_1.log)("accessory", `Unknown Door/UnitState ${status}, defaulting to STOPPED`);
return this.homebridge.Characteristic.CurrentDoorState.STOPPED;
}
}
HB2DT(state) {
if (state == this.homebridge.Characteristic.TargetDoorState.OPEN)
return types_1.UnitMotorCmd.kOpen;
else if (state == this.homebridge.Characteristic.TargetDoorState.CLOSED)
return types_1.UnitMotorCmd.kClose;
else if (state == this.homebridge.Characteristic.CurrentDoorState.STOPPED)
return types_1.UnitMotorCmd.kStop;
else
return null;
}
getDoorState(next) {
try {
this.unit.reqState(unit => {
const hb = this.DT2HB(unit.status);
(0, logger_1.log)("accessory", "Get CurrentDoorState of " + this.name + " = " + unit.status + " -> " + hb);
next(null, hb);
});
}
catch (err) {
next(err);
}
}
setDoorState(value, next) {
// homekit is giving OPEN, .STOPPED, .CLOSED
// for the smartbox -> 5=down, 4=up, 3=stop
this.targetState = value;
let cmd = this.HB2DT(value);
if (cmd) {
(0, logger_1.log)("accessory", "Set TargetDoorState of " + this.name + ", value =" + value + " -> cmd=" + cmd);
this.unit.setState(cmd)
.then(() => next())
.catch(err => next(err));
}
else {
(0, logger_1.log)("accessory", "Set TargetDoorState of " + this.name + " -> failed for : " + value);
next(new Error("TargetDoorState of " + this.name + " -> failed"));
}
}
getTargetDoorState(next) {
(0, logger_1.log)("accessory", "Characteristic.TargetDoorState.get was called of " + this.name + " = " + this.targetState);
next(null, this.targetState);
}
// in response to Duotecno status messages
updateState() {
const current = this.DT2HB(this.unit.status);
const target = current === this.homebridge.Characteristic.CurrentDoorState.OPEN
? this.homebridge.Characteristic.TargetDoorState.OPEN
: this.homebridge.Characteristic.TargetDoorState.CLOSED;
(0, logger_1.log)("accessory", "Received updateState -> Homekit GarageDoor for " + this.unit.node.getName() + " - " + this.unit.getName() + " -> " + this.unit.status + " -> passing to HB (current/target): " + current + " / " + target);
this.me
.getCharacteristic(this.homebridge.Characteristic.CurrentDoorState)
.updateValue(current);
this.me
.getCharacteristic(this.homebridge.Characteristic.TargetDoorState)
.updateValue(target);
}
}
exports.GarageDoor = GarageDoor;
//# sourceMappingURL=garagedoor.js.map