34
34
ABSOLUTE_MAX_HUMIDITY__PERCENTAGE = 100
35
35
ABSOLUTE_MIN_TEMPERATURE__CELCIUS = - 273.15
36
36
37
+
37
38
# TP96x battery values appear to be a voltage reading, probably in millivolts.
38
39
# This means that calculating battery life from it is a non-linear function.
39
40
# Examining the curve, it looked fairly close to a curve from the tanh function.
@@ -45,16 +46,22 @@ def tp96_battery(voltage: int) -> float:
45
46
clamped = max (0 , min (raw , 100 ))
46
47
return round (clamped , 2 )
47
48
48
- def is_temp_hum_invalid (temperature : Union [int , float ], humidity : Union [int , float ]) -> bool :
49
+
50
+ def is_temp_hum_invalid (temperature : int | float , humidity : int | float ) -> bool :
49
51
"""Returns true if the measured values are outside the physically possible range."""
50
52
# Note: This will not catch implausibly high temperature values, but a clear
51
53
# upper temperature cutoff is not easy to define
52
54
if temperature < ABSOLUTE_MIN_TEMPERATURE__CELCIUS :
53
55
return True
54
- if not (ABSOLUTE_MIN_HUMIDITY__PERCENTAGE <= humidity <= ABSOLUTE_MAX_HUMIDITY__PERCENTAGE ):
56
+ if not (
57
+ ABSOLUTE_MIN_HUMIDITY__PERCENTAGE
58
+ <= humidity
59
+ <= ABSOLUTE_MAX_HUMIDITY__PERCENTAGE
60
+ ):
55
61
return True
56
62
return False
57
63
64
+
58
65
class ThermoProBluetoothDeviceData (BluetoothData ):
59
66
"""Date update for ThermoPro Bluetooth devices."""
60
67
@@ -105,10 +112,12 @@ def _start_update(self, service_info: BluetoothServiceInfo) -> None:
105
112
ambient_temp = ambient_temp - 30
106
113
battery_percent = tp96_battery (battery_voltage )
107
114
108
- if is_temp_hum_invalid (internal_temp , 0 ) or is_temp_hum_invalid (ambient_temp , 0 ):
115
+ if is_temp_hum_invalid (internal_temp , 0 ) or is_temp_hum_invalid (
116
+ ambient_temp , 0
117
+ ):
109
118
# Invalid packet, probably corrupted
110
119
return
111
-
120
+
112
121
self .update_predefined_sensor (
113
122
SensorLibrary .TEMPERATURE__CELSIUS ,
114
123
internal_temp ,
@@ -129,20 +138,20 @@ def _start_update(self, service_info: BluetoothServiceInfo) -> None:
129
138
name = f"Probe { probe_one_indexed } Battery" ,
130
139
)
131
140
else :
132
- # TP357S seems to be in 6, TP397 and TP393 in 4
141
+ # TP357S seems to be in 6, TP397 and TP393 in 4
133
142
battery_byte = data [6 ] if len (data ) == 7 else data [4 ]
134
143
(temp_deci , humi ) = UNPACK_TEMP_HUMID (data [1 :4 ])
135
144
temp = temp_deci / 10
136
145
137
146
if is_temp_hum_invalid (temp , humi ):
138
147
# Invalid data, probably corrupted
139
148
return
140
-
149
+
141
150
if battery_byte in BATTERY_VALUE_TO_LEVEL :
142
151
self .update_predefined_sensor (
143
152
SensorLibrary .BATTERY__PERCENTAGE ,
144
153
BATTERY_VALUE_TO_LEVEL [battery_byte ],
145
154
)
146
-
155
+
147
156
self .update_predefined_sensor (SensorLibrary .TEMPERATURE__CELSIUS , temp )
148
157
self .update_predefined_sensor (SensorLibrary .HUMIDITY__PERCENTAGE , humi )
0 commit comments