homebridge-freeathome-local-api
Version:
Control your free@home setup using the local API provided by your System Access Point
48 lines • 2.32 kB
JavaScript
import { FreeAtHomeAccessory } from "./freeAtHomeAccessory.js";
import { getDataPointByPairingID } from "./util.js";
const pidOpenClosed = 53;
/** A contact sensor accessory. */
export class ContactSensorAccessory extends FreeAtHomeAccessory {
platform;
accessory;
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) {
super(platform, accessory);
this.platform = platform;
this.accessory = accessory;
// 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 = !!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 = !!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