@@ -75,7 +75,7 @@ def tlv_unpack(byte_array: bytes) -> dict[str, Any]:
7575 }
7676
7777
78- def decode_th_data (byte_array : bytes ) -> dict [str , Any ]:
78+ def decode_th_data (byte_array : bytes , product_id : int = 0 ) -> dict [str , Any ]:
7979 """Decode temperature, humidity, pressure, and battery data."""
8080 if len (byte_array ) < 6 :
8181 _LOGGER .error ("Byte array too short for TH data decoding" )
@@ -84,19 +84,25 @@ def decode_th_data(byte_array: bytes) -> dict[str, Any]:
8484 th = bytes_to_int_little_endian (byte_array [0 :3 ])
8585 temperature = ((th >> 12 ) - 500 ) / 10
8686 humidity = (th & 0xFFF ) / 10
87- pressure = bytes_to_int_little_endian (byte_array [3 :5 ]) / 100.0 # Convert to kPa
87+ raw_3_5 = bytes_to_int_little_endian (byte_array [3 :5 ])
8888 battery = byte_array [5 ]
8989
90- return {
90+ out : dict [ str , Any ] = {
9191 "dataType" : "data" ,
9292 "timestamp" : 0 ,
9393 "time" : "" ,
9494 "temperature" : temperature ,
9595 "humidity" : humidity ,
96- "pressure" : pressure ,
9796 "battery" : battery ,
9897 }
9998
99+ if product_id == 51 :
100+ out ["co2" ] = raw_3_5
101+ else :
102+ out ["pressure" ] = raw_3_5 / 100.0 # Convert to kPa
103+
104+ return out
105+
100106
101107def decode_realtime_data (byte_array : bytes , product_id : int = 0 ) -> dict [str , Any ]:
102108 """Decode real-time sensor data."""
@@ -105,7 +111,7 @@ def decode_realtime_data(byte_array: bytes, product_id: int = 0) -> dict[str, An
105111 return {}
106112
107113 timestamp = bytes_to_int_little_endian (byte_array [0 :4 ])
108- realtime_data = decode_th_data (byte_array [4 :])
114+ realtime_data = decode_th_data (byte_array [4 :], product_id )
109115 rssi = byte_array [10 ]
110116 if rssi >= 128 :
111117 rssi = rssi - 256
@@ -134,7 +140,7 @@ def decode_history_data(byte_array: bytes, product_id: int = 0) -> list[dict[str
134140
135141 while index + pack_len <= len (byte_array ):
136142 history_pack = byte_array [index :index + pack_len ]
137- history_data = decode_th_data (history_pack )
143+ history_data = decode_th_data (history_pack , product_id )
138144 history_data ["timestamp" ] = timestamp + duration * i
139145 history_data ["dataType" ] = "data"
140146 history_data ["time" ] = fmt_timestamp (history_data ["timestamp" ])
0 commit comments