From 9ccbc5bc21706d69f7d357375c8bc72217d41afa Mon Sep 17 00:00:00 2001 From: Patrick Pichon Date: Thu, 30 Nov 2023 20:11:53 +0100 Subject: [PATCH] correctly handle Signed data (very true for cold temperature nowadays --- Modules/tuya.py | 9 ++++++--- Modules/tuyaTS0601.py | 8 ++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Modules/tuya.py b/Modules/tuya.py index c3eb2985b..baafa9b04 100644 --- a/Modules/tuya.py +++ b/Modules/tuya.py @@ -1267,9 +1267,12 @@ def tuya_temphumi_response(self, Devices, _ModelName, NwkId, srcEp, ClusterID, d self.log.logging("Tuya", "Log", "tuya_temphumi_response - %s %s %s %s %s" % (NwkId, srcEp, dp, datatype, data), NwkId) if dp == 0x01: # Temperature, - store_tuya_attribute(self, NwkId, "Temp", data) - MajDomoDevice(self, Devices, NwkId, srcEp, "0402", (int(data, 16) / 10)) - checkAndStoreAttributeValue(self, NwkId, "01", "0402", "0000", (int(data, 16) / 10)) + unsigned_value = int( data,16) + signed_value = struct.unpack('>i', struct.pack('>I', unsigned_value))[0] + + store_tuya_attribute(self, NwkId, "Temp", signed_value) + MajDomoDevice(self, Devices, NwkId, srcEp, "0402", (signed_value / 10)) + checkAndStoreAttributeValue(self, NwkId, "01", "0402", "0000", (signed_value/ 10)) elif dp == 0x02: # Humi humi = int(data, 16) diff --git a/Modules/tuyaTS0601.py b/Modules/tuyaTS0601.py index b7cf7abea..e6eca3b8e 100644 --- a/Modules/tuyaTS0601.py +++ b/Modules/tuyaTS0601.py @@ -35,8 +35,12 @@ def ts0601_response(self, Devices, model_name, NwkId, Ep, dp, datatype, data): NwkId, str_dp, datatype, data, str(dps_mapping)), NwkId) store_tuya_attribute(self, NwkId, "UnknowDp_0x%02x_Dt_0x%02x" % (dp, datatype) , data) return False - + value = int(data, 16) + # If we have a signed number in an unsigned, let's convert + if len(data) <= 8: + value = struct.unpack('>i', struct.pack('>I', value))[0] + self.log.logging("Tuya0601", "Debug", " - value: %s" % (value), NwkId) self.log.logging("Tuya0601", "Debug", " - dps_mapping[ %s ]: %s (%s)" % ( str_dp, dps_mapping[ str_dp ], type(dps_mapping[ str_dp ])), NwkId) @@ -213,7 +217,7 @@ def ts0601_illuminance(self, Devices, nwkid, ep, value): def ts0601_temperature(self, Devices, nwkid, ep, value): - self.log.logging("Tuya0601", "Debug", "ts0601_temperature - Temperature %s %s %s " % (nwkid, ep, value), nwkid) + store_tuya_attribute(self, nwkid, "Temp", value) checkAndStoreAttributeValue(self, nwkid, "01", "0402", "0000", value) MajDomoDevice(self, Devices, nwkid, ep, "0402", value)