UNPKG

homebridge-freeathome-local-api

Version:

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

49 lines 2.39 kB
import { FreeAtHomeAccessory } from "./freeAtHomeAccessory.js"; import { getDataPointByPairingID } from "./util.js"; /** A contact sensor accessory. */ export class ContactSensorAccessory extends FreeAtHomeAccessory { platform; accessory; pidOpenClosed; service; contactOpen; dpOpenClosed; /** * Constructs a new contact sensor accessory instance. * @param platform The free@home Homebridge platform controlling the accessory * @param accessory The platform accessory. */ constructor(platform, accessory, pidOpenClosed = 53) { super(platform, accessory); this.platform = platform; this.accessory = accessory; this.pidOpenClosed = pidOpenClosed; // Resolve data points if (!this.accessory.context.channel.outputs) throw new Error("Channel lacks expected input or output data points."); this.dpOpenClosed = getDataPointByPairingID(this.accessory.context.channel.outputs, pidOpenClosed); // set initial state this.contactOpen = !!Number.parseInt(this.accessory.context.channel.outputs[this.dpOpenClosed].value ?? "0"); // get the ContactSensor service if it exists, otherwise create a new service instance this.service = this.accessory.getService(this.platform.Service.ContactSensor) || this.accessory.addService(this.platform.Service.ContactSensor); // register handlers for the contact sensor state characteristic this.service .getCharacteristic(this.platform.Characteristic.ContactSensorState) .onGet(() => this.contactOpen ? this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED : this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED); } updateDatapoint(datapoint, value) { // ignore unknown data points if (datapoint !== this.dpOpenClosed) return; // do the update this.contactOpen = !!Number.parseInt(value); this.doUpdateDatapoint("Contact Sensor", this.service, this.platform.Characteristic.ContactSensorState, this.contactOpen ? this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED : this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED); } } //# sourceMappingURL=contactSensorAccessory.js.map