homebridge-freeathome-local-api
Version:
Control your free@home setup using the local API provided by your System Access Point
67 lines • 3.03 kB
JavaScript
import { AccessoryType } from "./typeMappings.js";
import { FreeAtHomeAccessory } from "./freeAtHomeAccessory.js";
import { EmptyGuid, convertToString } from "./util.js";
/**
* A switch actuator accessory.
* @description
* A switch actuator can be used to control more or less any free@home device
* that has a binary on/off power state exposed on data point 0000 like, for example,
* binary switches, outlets or non-dimmable lights.
*/
export class SwitchActuatorAccessory extends FreeAtHomeAccessory {
platform;
accessory;
accessoryType;
service;
stateOn;
/**
* Constructs a new switch actuator accessory instance.
* @param platform The free@home Homebridge platform controlling the accessory
* @param accessory The platform accessory.
* @param accessoryType The accessory type
*/
constructor(platform, accessory, accessoryType = AccessoryType.Undefined) {
super(platform, accessory);
this.platform = platform;
this.accessory = accessory;
this.accessoryType = accessoryType;
// set initial state
this.stateOn = !!parseInt(this.accessory.context.channel.outputs?.odp0000.value ?? "0");
/*
* Configure the service service for the switch actuator. By default the Switch service will be used.
* If the instance was initialized with configureAsOutlet set to true, a Outlet service will be used instead.
* In any case, the service instance will be re-used if it exists already, otherwise create a new instance is created.
*/
const serviceType = accessoryType === AccessoryType.Outlet
? this.platform.Service.Outlet
: this.platform.Service.Switch;
this.service =
this.accessory.getService(serviceType) ||
this.accessory.addService(serviceType);
// register handlers for the On/Off Characteristic
this.service
.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setOn.bind(this))
.onGet(() => this.stateOn);
}
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} (Switch Actuator ${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");
}
updateDatapoint(datapoint, value) {
// ignore unknown data points
if (datapoint !== "odp0000")
return;
// do the update
this.stateOn = !!parseInt(value);
this.doUpdateDatapoint("Switch Actuator", this.service, this.platform.Characteristic.On, this.stateOn);
}
}
//# sourceMappingURL=switchActuatorAccessory.js.map