UNPKG

homebridge-freeathome-local-api

Version:

Control your free@home setup using the local API provided by your System Access Point

71 lines 3.63 kB
import { FreeAtHomeAccessory } from "./freeAtHomeAccessory.js"; import { EmptyGuid, convertToString } from "./util.js"; /** A door opener accessory.*/ export class DoorOpenerAccessory extends FreeAtHomeAccessory { platform; accessory; service; stateActive; /** * Constructs a new door opener accessory instance. * @param platform The free@home Homebridge platform controlling the accessory * @param accessory The platform accessory. */ constructor(platform, accessory) { super(platform, accessory); this.platform = platform; this.accessory = accessory; // set initial state this.stateActive = !!parseInt(this.accessory.context.channel.outputs?.odp0000.value ?? "0"); // get the LockMechanism service if it exists, otherwise create a new service instance this.service = this.accessory.getService(this.platform.Service.LockMechanism) || this.accessory.addService(this.platform.Service.LockMechanism); // register handlers for the current lock state Characteristic this.service .getCharacteristic(this.platform.Characteristic.LockCurrentState) .onGet(() => this.stateActive ? this.platform.Characteristic.LockCurrentState.UNSECURED : this.platform.Characteristic.LockCurrentState.SECURED); // register handlers for the current lock state Characteristic this.service .getCharacteristic(this.platform.Characteristic.LockTargetState) .onSet(this.setLockTargetState.bind(this)) .onGet(() => this.stateActive ? this.platform.Characteristic.LockTargetState.UNSECURED : this.platform.Characteristic.LockTargetState.SECURED); } async setLockTargetState(value) { // avoid unncessary updates or update cache const booleanTargetState = !value; if (booleanTargetState === this.stateActive) return; else this.stateActive = booleanTargetState; // log event this.platform.log.info(`${this.accessory.displayName} (Door Opener ${this.serialNumber}) set characteristic LockTargetState -> ${convertToString(value)}`); // set data point at SysAP try { await this.platform.sysap.setDatapoint(EmptyGuid, this.accessory.context.deviceSerial, this.accessory.context.channelId, "idp0000", booleanTargetState ? "1" : "0"); } catch { // Reset to previous state and log error this.stateActive = !this.stateActive; this.platform.log.error(`${this.accessory.displayName} (Door Opener ${this.serialNumber}) failed to set characteristic LockTargetState -> ${convertToString(value)}. Is the door opener configured in free@home?`); } } updateDatapoint(datapoint, value) { // ignore unknown data points if (datapoint !== "odp0000") return; // do the update this.stateActive = !!parseInt(value); this.doUpdateDatapoint("Door Opener", this.service, this.platform.Characteristic.LockCurrentState, this.stateActive ? this.platform.Characteristic.LockCurrentState.UNSECURED : this.platform.Characteristic.LockCurrentState.SECURED); this.doUpdateDatapoint("Door Opener", this.service, this.platform.Characteristic.LockTargetState, this.stateActive ? this.platform.Characteristic.LockTargetState.UNSECURED : this.platform.Characteristic.LockTargetState.SECURED); } } //# sourceMappingURL=doorOpenerAccessory.js.map