Skip to content

Commit

Permalink
correctly handle Signed data (very true for cold temperature nowadays
Browse files Browse the repository at this point in the history
  • Loading branch information
pipiche38 committed Nov 30, 2023
1 parent c1a3156 commit 9ccbc5b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
9 changes: 6 additions & 3 deletions Modules/tuya.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions Modules/tuyaTS0601.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9ccbc5b

Please sign in to comment.