homebridge-rachio-irrigation
Version:
Rachio Irrigation System platform plugin for [Homebridge](https://github.com/nfarina/homebridge).
134 lines • 6.86 kB
JavaScript
import RachioAPI from '../rachioapi.js';
export default class skipSwitch {
platform;
log;
rachioapi;
Service;
Characteristic;
delta;
timeStamp;
devices;
constructor(platform, log = platform.log, rachioapi = new RachioAPI(platform)) {
this.platform = platform;
this.log = log;
this.rachioapi = rachioapi;
this.Service = platform.Service;
this.Characteristic = platform.Characteristic;
this.timeStamp = [];
this.delta = [];
this.devices = [];
}
createSwitchService(switchName, uuid) {
// Create Valve Service
this.log.debug('adding new switch');
const switchService = new this.Service.Switch(switchName, uuid);
switchService.addCharacteristic(this.Characteristic.ConfiguredName);
switchService
.setCharacteristic(this.Characteristic.On, false)
.setCharacteristic(this.Characteristic.Name, switchName)
.setCharacteristic(this.Characteristic.ConfiguredName, switchName)
.setCharacteristic(this.Characteristic.StatusFault, this.Characteristic.StatusFault.NO_FAULT);
return switchService;
}
configureSwitchService(baseStation, switchService) {
this.log.debug(`Configured switch for ${switchService.getCharacteristic(this.Characteristic.Name).value}`);
this.devices.push(switchService);
switchService.getCharacteristic(this.Characteristic.On)
.onGet(this.getSwitchValue.bind(this, baseStation, switchService))
.onSet(this.setSwitchValue.bind(this, baseStation, switchService));
}
async setSwitchValue(baseStation, switchService) {
if (switchService.getCharacteristic(this.Characteristic.StatusFault).value == this.Characteristic.StatusFault.GENERAL_FAULT) {
throw new this.platform.HapStatusError(-70402 /* this.platform.HAPStatus.SERVICE_COMMUNICATION_FAILURE */);
}
this.log.debug(`toggle skip switch state ${switchService.getCharacteristic(this.Characteristic.Name).value}`);
const programs = await this.rachioapi.getValveDayViews(this.platform.token, baseStation.id).catch(err => {
this.log.error(`Failed to get daily view ${err}`);
throw err;
});
programs.valveDayViews.forEach((day) => {
day.valveProgramRunSummaries.forEach(run => {
run.valveRunSummaries.forEach(async (summary) => {
if (switchService.subtype == run.programId) {
if (summary.skip?.manualOverrideTrigger == undefined) {
if (run.plannedRunId) {
const response = await this.rachioapi.createSkip(this.platform.token, run.plannedRunId);
if (response?.status == 200) {
this.log.info(`Added skip for program ${run.programName} valve ${summary.valveName}`);
switchService.getCharacteristic(this.Characteristic.On).updateValue(true);
}
else {
switchService.getCharacteristic(this.Characteristic.On).updateValue(false);
}
}
else {
this.log.info('No upcoming planned program scheduled');
switchService.getCharacteristic(this.Characteristic.On).updateValue(false);
}
}
else {
if (run.plannedRunId) {
const response = await this.rachioapi.deleteSkip(this.platform.token, run.plannedRunId);
if (response?.status == 200) {
this.log.info(`Removed skip for program ${run.programName} valve ${summary.valveName}`);
switchService.getCharacteristic(this.Characteristic.On).updateValue(false);
}
else {
switchService.getCharacteristic(this.Characteristic.On).updateValue(true);
}
}
}
}
});
});
});
return;
}
getSwitchValue(baseStation, switchService) {
if (switchService.getCharacteristic(this.Characteristic.StatusFault).value == this.Characteristic.StatusFault.GENERAL_FAULT) {
throw new this.platform.HapStatusError(-70402 /* this.platform.HAPStatus.SERVICE_COMMUNICATION_FAILURE */);
}
const index = this.devices.findIndex(device => device.subtype === switchService.subtype);
const currentValue = switchService.getCharacteristic(this.Characteristic.On).value;
//set new timestamp
if (!this.timeStamp[index]) {
this.timeStamp[index] = +new Date();
}
//check for duplicate call
this.delta[index] = new Date().valueOf() - this.timeStamp[index];
if (this.delta[index] > 2 * 60 * 1000 || this.delta[index] == 0) { // check after 2 minutes
this.timeStamp[index] = +new Date();
this.updateSkipSwitch(baseStation, switchService);
}
else {
this.log.debug(`skipped program update, to soon. timestamp delta ${this.delta[index] / 1000} sec`);
}
return currentValue;
}
async updateSkipSwitch(baseStation, switchService) {
try {
this.log.debug(`updating skip program for station ${baseStation.id}`);
const programs = await this.rachioapi.getValveDayViews(this.platform.token, baseStation.id).catch(err => {
throw (`Failed to get daily view ${err}`);
});
programs.valveDayViews.forEach((day) => {
day.valveProgramRunSummaries.forEach(run => {
run.valveRunSummaries.forEach(summary => {
if (switchService.subtype == run.programId) {
if (summary.skip?.manualOverrideTrigger == undefined) {
switchService.getCharacteristic(this.Characteristic.On).updateValue(false);
}
else {
switchService.getCharacteristic(this.Characteristic.On).updateValue(true);
}
}
});
});
});
}
catch (err) {
this.log.error(`Error trying to update skip switch ${err}`);
}
}
}
//# sourceMappingURL=skipSwitch.js.map