@homebridge-plugins/homebridge-rainbird
Version:
The Rainbird plugin allows you to access your Rainbird device(s) from HomeKit.
75 lines • 3.01 kB
JavaScript
import { fromEvent } from 'rxjs';
import { DeviceBase } from './DeviceBase.js';
export class ProgramSwitch extends DeviceBase {
platform;
programSwitch;
constructor(platform, accessory, device, rainbird) {
super(platform, accessory, device, rainbird);
this.platform = platform;
// Program Switch Service
this.debugLog(`Load Switch Service for ${accessory.displayName}`);
this.programSwitch = {
service: this.accessory.getService(this.hap.Service.Switch) ?? this.accessory.addService(this.hap.Service.Switch),
state: false,
};
// Add Contact Sensor's Characteristics
this.programSwitch.service
.setCharacteristic(this.hap.Characteristic.On, false)
.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
this.programSwitch.service
.getCharacteristic(this.hap.Characteristic.On)
.onGet(() => {
this.rainbird.refreshStatus();
return this.programSwitch.state;
})
.onSet(this.setOn.bind(this));
// Initial Device Parse
this.parseStatus();
this.updateHomeKitCharacteristics();
// Device Parse when status event emitted
fromEvent(rainbird, 'status').subscribe({
next: () => {
this.parseStatus();
this.updateHomeKitCharacteristics();
},
});
}
async setOn(value) {
this.debugLog(`${this.constructor.name}: ${this.accessory.displayName}, Set On: ${value}`);
this.programSwitch.state = value;
if (value) {
await this.rainbird.startProgram(this.accessory.context.programId);
}
else {
await this.rainbird.stopIrrigation();
}
}
/**
* Parse the device status from the RainbirdClient
*/
parseStatus() {
const isRunning = this.rainbird.isProgramRunning(this.accessory.context.programId);
if (isRunning !== undefined) {
this.programSwitch.state = isRunning;
}
else {
if (this.programSwitch.state && !this.rainbird.isInUse()) {
this.programSwitch.state = false;
}
}
this.debugLog(`${this.constructor.name}: ${this.accessory.displayName} On: ${this.programSwitch.state}`);
}
/**
* Updates the status for each of the HomeKit Characteristics
*/
updateHomeKitCharacteristics() {
if (this.programSwitch.state === undefined) {
this.debugLog(`${this.constructor.name}: ${this.accessory.displayName} On: ${this.programSwitch.state}`);
}
else {
this.programSwitch.service.updateCharacteristic(this.hap.Characteristic.On, this.programSwitch.state);
this.debugLog(`${this.constructor.name}: ${this.accessory.displayName} updateCharacteristic On: ${this.programSwitch.state}`);
}
}
}
//# sourceMappingURL=ProgramSwitch.js.map