From 2620420ed5f9116e8f1d8a45c6a5f66b4d2c1c18 Mon Sep 17 00:00:00 2001 From: JDierkse Date: Mon, 3 Mar 2025 14:33:49 +0100 Subject: [PATCH] Add LowBattery and Active states to wireless sensors --- doorWindowSensorAccessory.ts | 46 ++++++++++++++++++++++++++++ environmentSensorAccessory.ts | 56 ++++++++++++++++++++++++++++++++++- motionSensorAccessory.ts | 48 +++++++++++++++++++++++++++++- 3 files changed, 148 insertions(+), 2 deletions(-) diff --git a/doorWindowSensorAccessory.ts b/doorWindowSensorAccessory.ts index 3dcc8d2..1870e27 100644 --- a/doorWindowSensorAccessory.ts +++ b/doorWindowSensorAccessory.ts @@ -10,6 +10,8 @@ export class doorWindowSensorAccessory extends accessory { private state = { ContactSensorState: this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED, + LowBattery: false, + Active: false } static async discoverDevices(platform: domoticaPlatform): Promise { @@ -62,6 +64,12 @@ export class doorWindowSensorAccessory extends accessory { this.service.getCharacteristic(this.platform.Characteristic.ContactSensorState) .onGet(this.getContactSensorState.bind(this)); + this.service.getCharacteristic(this.platform.Characteristic.StatusLowBattery) + .onGet(this.getLowBattery.bind(this)) + + this.service.getCharacteristic(this.platform.Characteristic.StatusActive) + .onGet(this.getActive.bind(this)) + this.setName(accessory.context.device.deviceName) this.update() } @@ -79,6 +87,22 @@ export class doorWindowSensorAccessory extends accessory { } this.service.updateCharacteristic(this.platform.Characteristic.ContactSensorState, this.state.ContactSensorState) } + else if (key == "battery" && value != "") { + if (value < 25) { + this.state.LowBattery = true + } else { + this.state.LowBattery = false + } + this.service.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.state.LowBattery) + } + else if (key == "online" && value != "") { + if (value == "false") { + this.state.Active = false + } else { + this.state.Active = true + } + this.service.updateCharacteristic(this.platform.Characteristic.StatusActive, this.state.Active) + } } update() { @@ -100,7 +124,21 @@ export class doorWindowSensorAccessory extends accessory { this.state.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED } + if (device.battery < 25) { + this.state.LowBattery = true + } else { + this.state.LowBattery = false + } + + if (device.online == "false") { + this.state.Active = false + } else { + this.state.Active = true + } + this.service.updateCharacteristic(this.platform.Characteristic.ContactSensorState, this.state.ContactSensorState) + this.service.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.state.LowBattery) + this.service.updateCharacteristic(this.platform.Characteristic.StatusActive, this.state.Active) break } @@ -115,4 +153,12 @@ export class doorWindowSensorAccessory extends accessory { getContactSensorState(): CharacteristicValue { return this.state.ContactSensorState } + + getLowBattery(): CharacteristicValue { + return this.state.LowBattery + } + + getActive(): CharacteristicValue { + return this.state.Active + } } \ No newline at end of file diff --git a/environmentSensorAccessory.ts b/environmentSensorAccessory.ts index e83e3d1..ef2ccca 100644 --- a/environmentSensorAccessory.ts +++ b/environmentSensorAccessory.ts @@ -11,7 +11,9 @@ export class environmentSensorAccessory extends accessory { private state = { Temperature: 0, - Humidity: 0 + Humidity: 0, + LowBattery: false, + Active: false } static async discoverDevices(platform: domoticaPlatform): Promise { @@ -63,6 +65,16 @@ export class environmentSensorAccessory extends accessory { this.serviceHumidity.getCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity) .onGet(this.getHumidity.bind(this)) + this.serviceTemperature.getCharacteristic(this.platform.Characteristic.StatusLowBattery) + .onGet(this.getLowBattery.bind(this)) + this.serviceHumidity.getCharacteristic(this.platform.Characteristic.StatusLowBattery) + .onGet(this.getLowBattery.bind(this)) + + this.serviceTemperature.getCharacteristic(this.platform.Characteristic.StatusActive) + .onGet(this.getActive.bind(this)) + this.serviceHumidity.getCharacteristic(this.platform.Characteristic.StatusActive) + .onGet(this.getActive.bind(this)) + this.setName(accessory.context.device.deviceName) this.update() } @@ -81,6 +93,24 @@ export class environmentSensorAccessory extends accessory { this.state.Humidity = value / 100 this.serviceHumidity.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.state.Humidity) } + else if (key == "battery" && value != "") { + if (value < 25) { + this.state.LowBattery = true + } else { + this.state.LowBattery = false + } + this.serviceTemperature.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.state.LowBattery) + this.serviceHumidity.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.state.LowBattery) + } + else if (key == "online" && value != "") { + if (value == "false") { + this.state.Active = false + } else { + this.state.Active = true + } + this.serviceTemperature.updateCharacteristic(this.platform.Characteristic.StatusActive, this.state.Active) + this.serviceHumidity.updateCharacteristic(this.platform.Characteristic.StatusActive, this.state.Active) + } } update() { @@ -99,8 +129,24 @@ export class environmentSensorAccessory extends accessory { this.state.Temperature = device.temperature this.state.Humidity = device.humidity + if (device.battery < 25) { + this.state.LowBattery = true + } else { + this.state.LowBattery = false + } + + if (device.online == "false") { + this.state.Active = false + } else { + this.state.Active = true + } + this.serviceTemperature.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.state.Temperature) this.serviceHumidity.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.state.Humidity) + this.serviceTemperature.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.state.LowBattery) + this.serviceTemperature.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.state.LowBattery) + this.serviceHumidity.updateCharacteristic(this.platform.Characteristic.StatusActive, this.state.Active) + this.serviceHumidity.updateCharacteristic(this.platform.Characteristic.StatusActive, this.state.Active) break } @@ -119,4 +165,12 @@ export class environmentSensorAccessory extends accessory { getHumidity(): CharacteristicValue { return this.state.Humidity } + + getLowBattery(): CharacteristicValue { + return this.state.LowBattery + } + + getActive(): CharacteristicValue { + return this.state.Active + } } diff --git a/motionSensorAccessory.ts b/motionSensorAccessory.ts index 271872b..7d8b6d8 100644 --- a/motionSensorAccessory.ts +++ b/motionSensorAccessory.ts @@ -9,7 +9,9 @@ export class motionSensorAccessory extends accessory { private service: Service private state = { - Motion: false + Motion: false, + LowBattery: false, + Active: false } static async discoverDevices(platform: domoticaPlatform): Promise { @@ -58,6 +60,12 @@ export class motionSensorAccessory extends accessory { this.service.getCharacteristic(this.platform.Characteristic.MotionDetected) .onGet(this.getState.bind(this)); + this.service.getCharacteristic(this.platform.Characteristic.StatusLowBattery) + .onGet(this.getLowBattery.bind(this)) + + this.service.getCharacteristic(this.platform.Characteristic.StatusActive) + .onGet(this.getActive.bind(this)) + this.setName(accessory.context.device.deviceName) this.update() } @@ -75,6 +83,22 @@ export class motionSensorAccessory extends accessory { } this.service.updateCharacteristic(this.platform.Characteristic.MotionDetected, this.state.Motion) } + else if (key == "battery" && value != "") { + if (value < 25) { + this.state.LowBattery = true + } else { + this.state.LowBattery = false + } + this.service.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.state.LowBattery) + } + else if (key == "online" && value != "") { + if (value == "false") { + this.state.Active = false + } else { + this.state.Active = true + } + this.service.updateCharacteristic(this.platform.Characteristic.StatusActive, this.state.Active) + } } update() { @@ -96,7 +120,21 @@ export class motionSensorAccessory extends accessory { this.state.Motion = true } + if (device.battery < 25) { + this.state.LowBattery = true + } else { + this.state.LowBattery = false + } + + if (device.online == "false") { + this.state.Active = false + } else { + this.state.Active = true + } + this.service.updateCharacteristic(this.platform.Characteristic.MotionDetected, this.state.Motion) + this.service.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.state.LowBattery) + this.service.updateCharacteristic(this.platform.Characteristic.StatusActive, this.state.Active) break } @@ -111,4 +149,12 @@ export class motionSensorAccessory extends accessory { getState(): CharacteristicValue { return this.state.Motion } + + getLowBattery(): CharacteristicValue { + return this.state.LowBattery + } + + getActive(): CharacteristicValue { + return this.state.Active + } } \ No newline at end of file