From 5fdab1cfb0ac98e2f582d5a5a14dbbe903180040 Mon Sep 17 00:00:00 2001 From: Pipiche <8291674+pipiche38@users.noreply.github.com> Date: Sun, 12 Jan 2025 18:32:08 +0100 Subject: [PATCH] Fix battery state which can be a float as well as a int (#1822) * refactor battery_state * Accept "Battery" to be an int but also a float * remove unused import from distutils.command.build import build --- Classes/WebServer/WebServer.py | 52 ++++++++++++++++++++-------------- Zigbee/zclDecoders.py | 2 +- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/Classes/WebServer/WebServer.py b/Classes/WebServer/WebServer.py index eaf3d4efd..efe6fcd2a 100644 --- a/Classes/WebServer/WebServer.py +++ b/Classes/WebServer/WebServer.py @@ -1082,8 +1082,8 @@ def rest_zDevice(self, verb, data, parameters): if attribut == "Battery" and attribut in self.ListOfDevices[item]: if self.ListOfDevices[item]["Battery"] in ( {}, ) and "IASBattery" in self.ListOfDevices[item]: device[attribut] = str(self.ListOfDevices[item][ "IASBattery" ]) - elif isinstance( self.ListOfDevices[item]["Battery"], int): - device[attribut] = self.ListOfDevices[item]["Battery"] + elif isinstance( self.ListOfDevices[item]["Battery"], (int,float)): + device[attribut] = int(self.ListOfDevices[item]["Battery"]) device["BatteryInside"] = True elif item == "CheckParam": @@ -1530,43 +1530,53 @@ def rest_zigate_mode(self, verb, data, parameters): _response["Data"] = json.dumps("ZiGate mode: %s requested" % mode) return _response + def rest_battery_state(self, verb, data, parameters): _response = prepResponseMessage(self, setupHeadersResponse()) _response["Headers"]["Content-Type"] = "application/json; charset=utf-8" if verb == "GET": _battEnv = {"Battery":{"<30%":{}, "<50%": {}, ">50%" : {}},"Update Time":{ "Unknown": {}, "< 1 week": {}, "> 1 week": {}}} for x in self.ListOfDevices: + self.logging("Debug", f"rest_battery_state - {x}") if x == "0000": - continue + continue - if self.ListOfDevices[x]["ZDeviceName"] == "": - _deviceName = x - else: - _deviceName = self.ListOfDevices[x]["ZDeviceName"] + battery = self.ListOfDevices[x].get("Battery") - if "Battery" in self.ListOfDevices[x] and isinstance(self.ListOfDevices[x]["Battery"], int): - if self.ListOfDevices[x]["Battery"] > 50: - _battEnv["Battery"][">50%"][_deviceName] = {"Battery": self.ListOfDevices[x]["Battery"]} + if battery is None: + continue + self.logging("Debug", f"rest_battery_state - {x} Battery found") - elif self.ListOfDevices[x]["Battery"] > 30: - _battEnv["Battery"]["<50%"][_deviceName] = {"Battery": self.ListOfDevices[x]["Battery"]} + _deviceName = self.ListOfDevices[x].get("ZDeviceName", x ) - else: - _battEnv["Battery"]["<30%"][_deviceName] = {"Battery": self.ListOfDevices[x]["Battery"]} + if not isinstance( battery, (int, float)): + self.logging("Debug", f"rest_battery_state - {x} Battery found, but not int !! {type(battery)}") + continue + battery = int(battery) - if "BatteryUpdateTime" in self.ListOfDevices[x]: - if (int(time.time()) - self.ListOfDevices[x]["BatteryUpdateTime"]) > 604800: # one week in seconds - _battEnv["Update Time"]["> 1 week"][_deviceName] = {"BatteryUpdateTime": self.ListOfDevices[x]["BatteryUpdateTime"]} + if self.ListOfDevices[x]["Battery"] > 50: + _battEnv["Battery"][">50%"][_deviceName] = {"Battery": battery} - else: - _battEnv["Update Time"]["< 1 week"][_deviceName] = {"BatteryUpdateTime": self.ListOfDevices[x]["BatteryUpdateTime"]} + elif self.ListOfDevices[x]["Battery"] > 30: + _battEnv["Battery"]["<50%"][_deviceName] = {"Battery": battery} + + else: + _battEnv["Battery"]["<30%"][_deviceName] = {"Battery": battery} + + if "BatteryUpdateTime" in self.ListOfDevices[x]: + if (int(time.time()) - self.ListOfDevices[x]["BatteryUpdateTime"]) > 604800: # one week in seconds + _battEnv["Update Time"]["> 1 week"][_deviceName] = {"BatteryUpdateTime": self.ListOfDevices[x]["BatteryUpdateTime"]} else: - _battEnv["Update Time"]["Unknown"][_deviceName] = "Unknown" + _battEnv["Update Time"]["< 1 week"][_deviceName] = {"BatteryUpdateTime": self.ListOfDevices[x]["BatteryUpdateTime"]} + else: + _battEnv["Update Time"]["Unknown"][_deviceName] = "Unknown" + + self.logging("Debug", f"rest_battery_state - {_battEnv}") _response["Data"] = json.dumps(_battEnv, sort_keys=True) return _response - + def logging(self, logType, message): self.log.logging("WebServer", logType, message) diff --git a/Zigbee/zclDecoders.py b/Zigbee/zclDecoders.py index 430055492..7b0177507 100644 --- a/Zigbee/zclDecoders.py +++ b/Zigbee/zclDecoders.py @@ -6,7 +6,7 @@ import struct -from distutils.command.build import build + from os import stat from Modules.tools import (is_direction_to_client, is_direction_to_server,