UNPKG

@pst-on-npm/homebridge-enocean

Version:
92 lines • 4.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BatteryServiceHelper = void 0; ; /** * The `EnoBatteryService` class is responsible for managing the battery service * of an EnOcean accessory within the Homebridge platform. It handles the battery * status, battery level, and charging state characteristics, updating them based * on incoming messages. * * @class EnoBatteryService */ class BatteryServiceHelper { platform; accessory; isChargeable; _statusLowBattery = 0; _batteryLevel = 100; _previousBatteryLevel; _chargingState; service; _hap; /** * Constructs an instance of the BatteryService. * * @param platform - The EnOceanHomebridgePlatform instance. * @param accessory - The PlatformAccessory instance with IEnoAccessoryContext. * * This constructor initializes the BatteryService by either retrieving an existing * Battery service from the accessory or adding a new one. It also sets up handlers * for the StatusLowBattery, BatteryLevel, and ChargingState characteristics to * retrieve their respective properties. */ constructor(platform, accessory, isChargeable = true) { this.platform = platform; this.accessory = accessory; this.isChargeable = isChargeable; this._hap = platform.api.hap; this._chargingState = isChargeable ? this._hap.Characteristic.ChargingState.NOT_CHARGING : this._hap.Characteristic.ChargingState.NOT_CHARGEABLE; // See https://developers.homebridge.io/#/service/Battery this.service = this.accessory.getService(this._hap.Service.Battery) || this.accessory.addService(this._hap.Service.Battery); this.service.getCharacteristic(this._hap.Characteristic.StatusLowBattery) .onGet(async () => this.getProperty(this._statusLowBattery)); this.service.getCharacteristic(this._hap.Characteristic.BatteryLevel) .onGet(async () => this.getProperty(this._batteryLevel)); this.service.getCharacteristic(this._hap.Characteristic.ChargingState) .onGet(async () => this.getProperty(this._chargingState)); } updateCharacteristics(message) { if (this.service !== undefined) { if (message.values.isBatteryLow !== undefined) { this._statusLowBattery = message.values.isBatteryLow ? this._hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this._hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL; this.service.updateCharacteristic(this._hap.Characteristic.StatusLowBattery, this._statusLowBattery); } if (message.values.batteryLevel !== undefined) { this._batteryLevel = message.values.batteryLevel; this.service.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this._batteryLevel); if (this.isChargeable) { if (this._previousBatteryLevel === undefined) { this._previousBatteryLevel = this._batteryLevel; this._chargingState = this._hap.Characteristic.ChargingState.NOT_CHARGING; this.service.updateCharacteristic(this._hap.Characteristic.ChargingState, this._chargingState); } else if (Math.abs(this._previousBatteryLevel - this._batteryLevel) > 0.5) { const oldChargingState = this._chargingState; if (this._batteryLevel > this._previousBatteryLevel) { this._chargingState = this._hap.Characteristic.ChargingState.CHARGING; } else { this._chargingState = this._hap.Characteristic.ChargingState.NOT_CHARGING; } if (oldChargingState !== this._chargingState) { this.platform.log.info(`${this.accessory.displayName}: battery ${this._chargingState ? 'CHARGING' : 'NOT CHARGING'}`); } this._previousBatteryLevel = this._batteryLevel; this.service.updateCharacteristic(this._hap.Characteristic.ChargingState, this._chargingState); } } } } } async getProperty(property) { return property; } } exports.BatteryServiceHelper = BatteryServiceHelper; //# sourceMappingURL=BatteryServiceHelper.js.map