homebridge-loxone-proxy
Version:
Homebridge Dynamic Platform Plugin which exposes a Loxone System to Homekit.
114 lines • 4.41 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Valve = void 0;
class Valve {
constructor(platform, accessory, zone, sendCommand = () => void 0) {
this.platform = platform;
this.accessory = accessory;
this.zone = zone;
this.sendCommand = sendCommand;
this.interval = null;
const { Characteristic, Service } = this.platform;
const rawName = zone.name || `Zone #${zone.id + 1}`;
const safeName = this.platform.sanitizeName(rawName);
const subtype = `zone-${zone.id}`;
this.valveService =
this.accessory.getServiceById(Service.Valve, subtype) ||
this.accessory.addService(Service.Valve, rawName, subtype);
this.valveService.setCharacteristic(Characteristic.Name, rawName);
this.valveService.setCharacteristic(Characteristic.ConfiguredName, safeName);
this.valveService.setCharacteristic(Characteristic.ValveType, Characteristic.ValveType.IRRIGATION);
this.valveService.setCharacteristic(Characteristic.Active, 0);
this.valveService.setCharacteristic(Characteristic.InUse, 0);
this.valveService.setCharacteristic(Characteristic.SetDuration, zone.duration);
this.valveService.setCharacteristic(Characteristic.RemainingDuration, 0);
this.meta = {
id: zone.id,
duration: zone.duration,
startTime: null,
};
this.setupListeners();
}
setupListeners() {
const { Characteristic } = this.platform;
this.valveService
.getCharacteristic(Characteristic.Active)
.removeAllListeners('set')
.on('set', (value, callback) => {
const zoneId = this.meta.id + 1;
if (value === 1) {
this.sendCommand(`select/${zoneId}`);
}
else {
this.sendCommand('select/0');
}
callback(null);
});
this.valveService
.getCharacteristic(Characteristic.SetDuration)
.removeAllListeners('set')
.on('set', (value, callback) => {
const duration = typeof value === 'number' ? Math.max(0, Math.floor(value)) : 0;
this.valveService.updateCharacteristic(this.platform.Characteristic.SetDuration, duration);
this.meta.duration = duration;
this.sendCommand(`setDuration/${this.meta.id}=${duration}`);
callback(null);
});
}
reset() {
const { Characteristic } = this.platform;
this.meta.startTime = null;
this.valveService.updateCharacteristic(Characteristic.InUse, 0);
this.valveService.updateCharacteristic(Characteristic.Active, 0);
this.valveService.updateCharacteristic(Characteristic.RemainingDuration, 0);
}
activate(now) {
const { Characteristic } = this.platform;
this.meta.startTime = now;
this.valveService.updateCharacteristic(Characteristic.InUse, 1);
this.valveService.updateCharacteristic(Characteristic.Active, 1);
this.valveService.updateCharacteristic(Characteristic.RemainingDuration, this.meta.duration);
this.startTimerLoop();
}
tick(now) {
const { Characteristic } = this.platform;
if (this.meta.startTime) {
const elapsed = Math.floor((now - this.meta.startTime) / 1000);
const remaining = Math.max(0, this.meta.duration - elapsed);
this.valveService.updateCharacteristic(Characteristic.RemainingDuration, remaining);
if (remaining === 0) {
this.reset();
}
}
}
updateFromLoxone(currentZoneId, now) {
if (currentZoneId === -1 || currentZoneId !== this.meta.id) {
this.reset();
}
else {
this.activate(now);
}
}
getId() {
return this.meta.id;
}
isActive() {
return this.meta.startTime !== null;
}
startTimerLoop() {
if (this.interval) {
return;
}
this.interval = setInterval(() => {
this.tick(Date.now());
}, 1000);
}
stopTimerLoop() {
if (this.interval) {
clearInterval(this.interval);
this.interval = null;
}
}
}
exports.Valve = Valve;
//# sourceMappingURL=Valve.js.map