From 2e01c70d2d431642394c1e527b6e8316a7fbb821 Mon Sep 17 00:00:00 2001 From: Johann Richard <189003+johannrichard@users.noreply.github.com> Date: Sun, 11 Dec 2022 16:12:08 +0100 Subject: [PATCH 1/2] fix(platform): :bug: improve temperature, humidty and battery handling --- src/myStromButtonPlusAccessory.ts | 39 ++++++++++++++++++------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/myStromButtonPlusAccessory.ts b/src/myStromButtonPlusAccessory.ts index 3e8ec33..2d3407f 100644 --- a/src/myStromButtonPlusAccessory.ts +++ b/src/myStromButtonPlusAccessory.ts @@ -44,10 +44,6 @@ export class MyStromButtonPlusAccessory extends MyStromButtonAccessory { ButtonState.OFF, ]; - private batteryService: Service | undefined = undefined; - private temperatureService: Service | undefined = undefined; - private humidityService: Service | undefined = undefined; - private buttonPlusState = { temperature: 0, humidity: 0, @@ -104,31 +100,31 @@ export class MyStromButtonPlusAccessory extends MyStromButtonAccessory { // Button Plus 2nd Gen has a temperature sensor, make it available here // create a new Temperature Sensor service - this.temperatureService = + const temperatureService = this.accessory.getService(this.platform.Service.TemperatureSensor) ?? this.accessory.addService(this.platform.Service.TemperatureSensor); - this.temperatureService.setCharacteristic( + temperatureService.setCharacteristic( this.platform.Characteristic.Name, 'Temperature', ); // create handlers for required characteristics - this.temperatureService + temperatureService .getCharacteristic(this.platform.Characteristic.CurrentTemperature) .on(CharacteristicEventTypes.GET, this.getTemperature.bind(this)); // Button Plus 2nd Gen has a humidity sensor, make it available here // create a new Humidity Sensor service - this.humidityService = + const humidityService = this.accessory.getService(this.platform.Service.HumiditySensor) ?? this.accessory.addService(this.platform.Service.HumiditySensor); - this.humidityService.setCharacteristic( + humidityService.setCharacteristic( this.platform.Characteristic.Name, 'Humidity', ); // create handlers for required characteristics - this.humidityService + humidityService .getCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity) .on(CharacteristicEventTypes.GET, this.getHumidity.bind(this)); @@ -199,7 +195,10 @@ export class MyStromButtonPlusAccessory extends MyStromButtonAccessory { ); // battery, temperature and humidity are reported in any case - if (this.batteryService && battery) { + const batteryService = this.accessory.getService( + this.platform.Service.Battery, + ); + if (batteryService && battery) { // TODO: Check if true // MyStrom Button+ (2nd Gen) reportedly returns voltages when updating via button actions // see https://github.com/myStrom/mystrom-button/blob/master/user/peri.c (IQS) @@ -210,7 +209,8 @@ export class MyStromButtonPlusAccessory extends MyStromButtonAccessory { MyStromButtonPlusBattery.BATTERY_MIN; this.batteryLevel = ((battery - MyStromButtonPlusBattery.BATTERY_MIN) * 100) / range; - this.batteryService + + batteryService .getCharacteristic(this.platform.Characteristic.BatteryLevel) .updateValue(this.batteryLevel); this.log.debug( @@ -218,9 +218,13 @@ export class MyStromButtonPlusAccessory extends MyStromButtonAccessory { ); } - if (this.temperatureService && temperature) { + const temperatureService = this.accessory.getService( + this.platform.Service.TemperatureSensor, + ); + + if (temperatureService && temperature) { this.buttonPlusState.temperature = temperature; - this.temperatureService + temperatureService .getCharacteristic( this.platform.Characteristic.CurrentTemperature, ) @@ -230,9 +234,12 @@ export class MyStromButtonPlusAccessory extends MyStromButtonAccessory { ); } - if (this.humidityService && humidity) { + const humidityService = this.accessory.getService( + this.platform.Service.HumiditySensor, + ); + if (humidityService && humidity) { this.buttonPlusState.humidity = humidity; - this.humidityService + humidityService .getCharacteristic( this.platform.Characteristic.CurrentRelativeHumidity, ) From 8347ba5444e8c7ead1bbe35780f5076ff4d37421 Mon Sep 17 00:00:00 2001 From: Johann Richard <189003+johannrichard@users.noreply.github.com> Date: Sun, 11 Dec 2022 16:16:50 +0100 Subject: [PATCH 2/2] fix(platform): :rocket: improved logging --- src/myStromButtonPlusAccessory.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/myStromButtonPlusAccessory.ts b/src/myStromButtonPlusAccessory.ts index 2d3407f..2e23c4a 100644 --- a/src/myStromButtonPlusAccessory.ts +++ b/src/myStromButtonPlusAccessory.ts @@ -192,6 +192,7 @@ export class MyStromButtonPlusAccessory extends MyStromButtonAccessory { if (mac === this.device.mac && module) { this.log.debug( `Module ${module} triggered -> ${action}, MAC: ${mac} (This: ${this.device.mac})`, + `\nBatt [V]: ${battery}, °C: ${temperature}, RH: ${humidity})`, ); // battery, temperature and humidity are reported in any case @@ -213,9 +214,7 @@ export class MyStromButtonPlusAccessory extends MyStromButtonAccessory { batteryService .getCharacteristic(this.platform.Characteristic.BatteryLevel) .updateValue(this.batteryLevel); - this.log.debug( - `Setting new battery level to ${this.batteryLevel}%`, - ); + this.log.debug(`Setting battery level to ${this.batteryLevel}%`); } const temperatureService = this.accessory.getService( @@ -229,9 +228,7 @@ export class MyStromButtonPlusAccessory extends MyStromButtonAccessory { this.platform.Characteristic.CurrentTemperature, ) .updateValue(temperature); - this.log.debug( - `Setting new battery temperature to ${temperature}%`, - ); + this.log.debug(`Setting temperature to ${temperature}°`); } const humidityService = this.accessory.getService( @@ -244,7 +241,7 @@ export class MyStromButtonPlusAccessory extends MyStromButtonAccessory { this.platform.Characteristic.CurrentRelativeHumidity, ) .updateValue(humidity); - this.log.debug(`Setting new humidty level to ${humidity}%`); + this.log.debug(`Setting humidty level to ${humidity}%`); } switch (module) {