homebridge-freeathome-local-api
Version:
Control your free@home setup using the local API provided by your System Access Point
84 lines • 3.89 kB
JavaScript
import { FreeAtHomeAccessory } from "./freeAtHomeAccessory.js";
import { EmptyGuid, convertToString } from "./util.js";
/** A dimming actuator accessory.*/
export class DimmerAccessory extends FreeAtHomeAccessory {
platform;
accessory;
service;
stateOn;
stateBrightness;
/**
* Constructs a new dimming actuator 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.stateOn = !!parseInt(this.accessory.context.channel.outputs?.odp0000.value ?? "0");
this.stateBrightness = parseInt(this.accessory.context.channel.outputs?.odp0001.value ??
this.accessory.context.channel.inputs?.idp0002.value ??
"0");
// get the Lightbulb service if it exists, otherwise create a new service instance
this.service =
this.accessory.getService(this.platform.Service.Lightbulb) ||
this.accessory.addService(this.platform.Service.Lightbulb);
// register handlers for the On/Off Characteristic
this.service
.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setOn.bind(this))
.onGet(() => this.stateOn);
// register handlers for the Brightness Characteristic
this.service
.getCharacteristic(this.platform.Characteristic.Brightness)
.onSet(this.setBrightness.bind(this))
.onGet(() => this.stateBrightness);
}
async setOn(value) {
// avoid unncessary updates or update cache
if (value === this.stateOn)
return;
else
this.stateOn = value;
// log event
this.platform.log.info(`${this.accessory.displayName} (Dimmer Accessory ${this.serialNumber}) set characteristic On -> ${convertToString(value)}`);
// set data point at SysAP
await this.platform.sysap.setDatapoint(EmptyGuid, this.accessory.context.deviceSerial, this.accessory.context.channelId, "idp0000", value ? "1" : "0");
// restore previous brightness
if (value && this.stateBrightness)
await this.setBrightness(this.stateBrightness);
}
async setBrightness(value) {
// avoid unncessary updates or update cache
if (value === this.stateBrightness)
return;
else
this.stateBrightness = value;
// log event
this.platform.log.info(`${this.accessory.displayName} (Dimmer Accessory ${this.serialNumber}) set characteristic Brightness -> ${convertToString(value)}`);
// set data point at SysAP
await this.platform.sysap.setDatapoint(EmptyGuid, this.accessory.context.deviceSerial, this.accessory.context.channelId, "idp0002", convertToString(value));
}
updateDatapoint(datapoint, value) {
// ignore unknown data points
switch (datapoint) {
case "odp0000":
this.stateOn = !!parseInt(value);
// do the update
this.doUpdateDatapoint("Dimmer Accessory", this.service, this.platform.Characteristic.On, this.stateOn);
return;
case "odp0001":
// Do NOT set brightness to 0, otherwise when turning the dimmer back on, brightness will be 100%.
if (value === "0")
return;
this.stateBrightness = parseInt(value);
this.doUpdateDatapoint("Dimmer Accessory", this.service, this.platform.Characteristic.Brightness, this.stateBrightness);
return;
default:
return;
}
}
}
//# sourceMappingURL=dimmerAccessory.js.map