homebridge-freeathome-local-api
Version:
Control your free@home setup using the local API provided by your System Access Point
100 lines • 4.78 kB
JavaScript
import { FreeAtHomeAccessory } from "./freeAtHomeAccessory.js";
import { EmptyGuid, convertToString, getDataPointByPairingID } from "./util.js";
const pidOutOn = 256;
const pidOutBrightness = 272;
const pidInOn = 1;
const pidInBrightness = 17;
/** A dimming actuator accessory.*/
export class DimmerAccessory extends FreeAtHomeAccessory {
platform;
accessory;
service;
stateOn;
stateBrightness;
dpOutOn;
dpOutBrightness;
dpInOn;
dpInBrightness;
/**
* 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;
// Resolve data points
if (!this.accessory.context.channel.outputs ||
!this.accessory.context.channel.inputs)
throw new Error("Channel lacks expected input or output data points.");
this.dpOutOn = getDataPointByPairingID(this.accessory.context.channel.outputs, pidOutOn);
this.dpOutBrightness = getDataPointByPairingID(this.accessory.context.channel.outputs, pidOutBrightness);
this.dpInOn = getDataPointByPairingID(this.accessory.context.channel.inputs, pidInOn);
this.dpInBrightness = getDataPointByPairingID(this.accessory.context.channel.inputs, pidInBrightness);
// set initial state
this.stateOn = !!parseInt(this.accessory.context.channel.outputs[this.dpOutOn].value ?? "0");
this.stateBrightness = parseInt(this.accessory.context.channel.outputs[this.dpOutBrightness].value ??
this.accessory.context.channel.inputs[this.dpInBrightness].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, this.dpInOn, 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, this.dpInBrightness, convertToString(value));
}
updateDatapoint(datapoint, value) {
// ignore unknown data points
switch (datapoint) {
case this.dpOutOn:
this.stateOn = !!parseInt(value);
// do the update
this.doUpdateDatapoint("Dimmer Accessory", this.service, this.platform.Characteristic.On, this.stateOn);
return;
case this.dpOutBrightness:
// 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