UNPKG

homebridge-rachio-irrigation

Version:

Rachio Irrigation System platform plugin for [Homebridge](https://github.com/nfarina/homebridge).

104 lines 5.68 kB
import RachioAPI from '../rachioapi.js'; export default class battery { 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 = []; } createBatteryService(device) { this.log.debug(`create battery service for ${device.name}`); const batteryStatus = new this.Service.Battery(device.name, device.id); switch (device.state.reportedState.batteryStatus) { case 'GOOD': batteryStatus .setCharacteristic(this.Characteristic.StatusLowBattery, this.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL) .setCharacteristic(this.Characteristic.ChargingState, this.Characteristic.ChargingState.NOT_CHARGEABLE) .setCharacteristic(this.Characteristic.BatteryLevel, 100); break; case 'LOW': batteryStatus .setCharacteristic(this.Characteristic.StatusLowBattery, this.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW) .setCharacteristic(this.Characteristic.ChargingState, this.Characteristic.ChargingState.NOT_CHARGEABLE) .setCharacteristic(this.Characteristic.BatteryLevel, 40); break; case 'REPLACE': batteryStatus .setCharacteristic(this.Characteristic.StatusLowBattery, this.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW) .setCharacteristic(this.Characteristic.ChargingState, this.Characteristic.ChargingState.NOT_CHARGEABLE) .setCharacteristic(this.Characteristic.BatteryLevel, 10); this.log.warn(`Replace batteries for ${device.name} soon`); break; } return batteryStatus; } configureBatteryService(batteryStatus) { this.log.debug(`configured battery service for ${batteryStatus.getCharacteristic(this.Characteristic.Name).value}`); this.devices.push(batteryStatus); batteryStatus.getCharacteristic(this.Characteristic.StatusLowBattery) .onGet(this.getStatusLowBattery.bind(this, batteryStatus)); } getStatusLowBattery(batteryStatus) { const index = this.devices.findIndex(device => device.subtype === batteryStatus.subtype); const currentValue = batteryStatus.getCharacteristic(this.Characteristic.StatusLowBattery).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] > 60 * 60 * 1000 || this.delta[index] == 0) { // check after 1 hour this.timeStamp[index] = +new Date(); this.updateBatteryStatus(batteryStatus, index); } else { this.log.debug(`skipped battery update, to soon. timestamp delta ${this.delta[index] / 1000} sec`); } return currentValue; } async updateBatteryStatus(batteryStatus, index) { // add charging state and level to this call try { this.log.debug(`updating battery for valve index ${index}`); const response = await this.rachioapi.getValve(this.platform.token, this.devices[index].subtype).catch(err => { throw (`Failed to get valve battery status ${err}`); }); if (response?.status == 200) { switch (response.data.valve.state.reportedState.batteryStatus) { case 'GOOD': batteryStatus.getCharacteristic(this.Characteristic.StatusLowBattery).updateValue(this.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL); batteryStatus.getCharacteristic(this.Characteristic.ChargingState).updateValue(this.Characteristic.ChargingState.NOT_CHARGEABLE); batteryStatus.getCharacteristic(this.Characteristic.BatteryLevel).updateValue(100); break; case 'LOW': batteryStatus.getCharacteristic(this.Characteristic.StatusLowBattery).updateValue(this.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW); batteryStatus.getCharacteristic(this.Characteristic.ChargingState).updateValue(this.Characteristic.ChargingState.NOT_CHARGEABLE); batteryStatus.getCharacteristic(this.Characteristic.BatteryLevel).updateValue(40); break; case 'REPLACE': batteryStatus.getCharacteristic(this.Characteristic.StatusLowBattery).updateValue(this.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW); batteryStatus.getCharacteristic(this.Characteristic.ChargingState).updateValue(this.Characteristic.ChargingState.NOT_CHARGEABLE); batteryStatus.getCharacteristic(this.Characteristic.BatteryLevel).updateValue(10); this.log.warn(`Replace batteries for ${response.data.valve.name} soon`); break; } } } catch (err) { this.log.error(`Error trying to update battery status ${err}`); } } } //# sourceMappingURL=battery.js.map