Skip to content

Commit 5d775fb

Browse files
joeknollbdraco
andauthored
fix: temperatures for TP97x (#129)
Co-authored-by: J. Nick Koston <nick@koston.org> Co-authored-by: J. Nick Koston <nick+github@koston.org> Co-authored-by: J. Nick Koston <nick@home-assistant.io>
1 parent ad03d14 commit 5d775fb

File tree

2 files changed

+142
-17
lines changed

2 files changed

+142
-17
lines changed

src/thermopro_ble/parser.py

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from bluetooth_sensor_state_data import BluetoothData
1818
from sensor_state_data import SensorLibrary
1919

20-
from habluetooth import BluetoothServiceInfo, BluetoothServiceInfoBleak
20+
from habluetooth import BluetoothServiceInfoBleak
2121

2222
_LOGGER = logging.getLogger(__name__)
2323

@@ -45,6 +45,12 @@ def tp96_battery(voltage: int) -> float:
4545
return round(clamped, 2)
4646

4747

48+
# TP97x only supply temperatures in F, even when display is set to celsius
49+
def fahrenheit_to_celsius(fahrenheit_temp: float) -> float:
50+
celsius_temp = (fahrenheit_temp - 32) * 5 / 9
51+
return round(celsius_temp, 1)
52+
53+
4854
class ThermoProBluetoothDeviceData(BluetoothData):
4955
"""Date update for ThermoPro Bluetooth devices."""
5056

@@ -75,9 +81,49 @@ def _update_sensors(
7581
name=f"Probe {probe_one_indexed} Battery",
7682
)
7783

78-
def _start_update(
79-
self, service_info: BluetoothServiceInfo | BluetoothServiceInfoBleak
84+
def _update_sensors_tp97(
85+
self,
86+
probe_one_indexed: int,
87+
internal_temp_tip: float,
88+
internal_temp_center: float,
89+
internal_temp_end: float,
90+
ambient_temp: float,
91+
battery_percent: float,
8092
) -> None:
93+
self.set_precision(1)
94+
self.update_predefined_sensor(
95+
SensorLibrary.TEMPERATURE__CELSIUS,
96+
internal_temp_tip,
97+
key=f"internal_temperature_probe_{probe_one_indexed}",
98+
name=f"Probe {probe_one_indexed} Internal Tip Temperature",
99+
)
100+
self.update_predefined_sensor(
101+
SensorLibrary.TEMPERATURE__CELSIUS,
102+
internal_temp_center,
103+
key=f"internal_center_temperature_probe_{probe_one_indexed}",
104+
name=f"Probe {probe_one_indexed} Internal Center Temperature",
105+
)
106+
self.update_predefined_sensor(
107+
SensorLibrary.TEMPERATURE__CELSIUS,
108+
internal_temp_end,
109+
key=f"internal_end_temperature_probe_{probe_one_indexed}",
110+
name=f"Probe {probe_one_indexed} Internal End Temperature",
111+
)
112+
self.update_predefined_sensor(
113+
SensorLibrary.TEMPERATURE__CELSIUS,
114+
ambient_temp,
115+
key=f"ambient_temperature_probe_{probe_one_indexed}",
116+
name=f"Probe {probe_one_indexed} Ambient Temperature",
117+
)
118+
self.set_precision(0)
119+
self.update_predefined_sensor(
120+
SensorLibrary.BATTERY__PERCENTAGE,
121+
battery_percent,
122+
key=f"battery_probe_{probe_one_indexed}",
123+
name=f"Probe {probe_one_indexed} Battery",
124+
)
125+
126+
def _start_update(self, service_info: BluetoothServiceInfoBleak) -> None:
81127
"""Update from BLE advertisement data."""
82128
_LOGGER.debug("Parsing thermopro BLE advertisement data: %s", service_info)
83129
name = service_info.name
@@ -113,20 +159,27 @@ def _start_update(
113159
probe_zero_indexed,
114160
ambient_temp,
115161
battery_voltage,
116-
_, # looks to be part of some temp range (min)
117-
internal_temp,
118-
_, # looks to be part of some temp range (max)
162+
internal_tip_temp, # tip temperature
163+
internal_center_temp, # center temperature
164+
internal_end_temp, # end temperature
165+
_, # looks like a static id
119166
_,
120167
_,
121-
_, # looks like a static id
122168
) = UNPACK_SPIKE_PRO_TEMP(data)
123169

124170
probe_one_indexed = probe_zero_indexed + 1
125-
internal_temp = int(internal_temp) - 54
126-
ambient_temp = int(ambient_temp) - 54
171+
internal_tip_temp = fahrenheit_to_celsius(internal_tip_temp - 54)
172+
internal_center_temp = fahrenheit_to_celsius(internal_center_temp - 54)
173+
internal_end_temp = fahrenheit_to_celsius(internal_end_temp - 54)
174+
ambient_temp = int(fahrenheit_to_celsius(ambient_temp - 54))
127175
battery_percent = tp96_battery(battery_voltage)
128-
self._update_sensors(
129-
probe_one_indexed, internal_temp, ambient_temp, battery_percent
176+
self._update_sensors_tp97(
177+
probe_one_indexed,
178+
internal_tip_temp,
179+
internal_center_temp,
180+
internal_end_temp,
181+
ambient_temp,
182+
battery_percent,
130183
)
131184
return
132185

tests/test_parser.py

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,24 @@ def test_tp972s() -> None:
13361336
device_class=SensorDeviceClass.TEMPERATURE,
13371337
native_unit_of_measurement=Units.TEMP_CELSIUS,
13381338
),
1339+
DeviceKey(
1340+
key="internal_center_temperature_probe_1", device_id=None
1341+
): SensorDescription(
1342+
device_key=DeviceKey(
1343+
key="internal_center_temperature_probe_1", device_id=None
1344+
),
1345+
device_class=SensorDeviceClass.TEMPERATURE,
1346+
native_unit_of_measurement=Units.TEMP_CELSIUS,
1347+
),
1348+
DeviceKey(
1349+
key="internal_end_temperature_probe_1", device_id=None
1350+
): SensorDescription(
1351+
device_key=DeviceKey(
1352+
key="internal_end_temperature_probe_1", device_id=None
1353+
),
1354+
device_class=SensorDeviceClass.TEMPERATURE,
1355+
native_unit_of_measurement=Units.TEMP_CELSIUS,
1356+
),
13391357
DeviceKey(
13401358
key="ambient_temperature_probe_1", device_id=None
13411359
): SensorDescription(
@@ -1359,13 +1377,31 @@ def test_tp972s() -> None:
13591377
device_key=DeviceKey(
13601378
key="internal_temperature_probe_1", device_id=None
13611379
),
1362-
name="Probe 1 Internal Temperature",
1363-
native_value=33,
1380+
name="Probe 1 Internal Tip Temperature",
1381+
native_value=3.6,
1382+
),
1383+
DeviceKey(
1384+
key="internal_center_temperature_probe_1", device_id=None
1385+
): SensorValue(
1386+
device_key=DeviceKey(
1387+
key="internal_center_temperature_probe_1", device_id=None
1388+
),
1389+
name="Probe 1 Internal Center Temperature",
1390+
native_value=0.6,
1391+
),
1392+
DeviceKey(
1393+
key="internal_end_temperature_probe_1", device_id=None
1394+
): SensorValue(
1395+
device_key=DeviceKey(
1396+
key="internal_end_temperature_probe_1", device_id=None
1397+
),
1398+
name="Probe 1 Internal End Temperature",
1399+
native_value=0.6,
13641400
),
13651401
DeviceKey(key="ambient_temperature_probe_1", device_id=None): SensorValue(
13661402
device_key=DeviceKey(key="ambient_temperature_probe_1", device_id=None),
13671403
name="Probe 1 Ambient Temperature",
1368-
native_value=60,
1404+
native_value=15,
13691405
),
13701406
},
13711407
binary_entity_descriptions={},
@@ -1403,6 +1439,24 @@ def test_tp972s() -> None:
14031439
device_class=SensorDeviceClass.TEMPERATURE,
14041440
native_unit_of_measurement=Units.TEMP_CELSIUS,
14051441
),
1442+
DeviceKey(
1443+
key="internal_center_temperature_probe_1", device_id=None
1444+
): SensorDescription(
1445+
device_key=DeviceKey(
1446+
key="internal_center_temperature_probe_1", device_id=None
1447+
),
1448+
device_class=SensorDeviceClass.TEMPERATURE,
1449+
native_unit_of_measurement=Units.TEMP_CELSIUS,
1450+
),
1451+
DeviceKey(
1452+
key="internal_end_temperature_probe_1", device_id=None
1453+
): SensorDescription(
1454+
device_key=DeviceKey(
1455+
key="internal_end_temperature_probe_1", device_id=None
1456+
),
1457+
device_class=SensorDeviceClass.TEMPERATURE,
1458+
native_unit_of_measurement=Units.TEMP_CELSIUS,
1459+
),
14061460
DeviceKey(
14071461
key="ambient_temperature_probe_1", device_id=None
14081462
): SensorDescription(
@@ -1426,13 +1480,31 @@ def test_tp972s() -> None:
14261480
device_key=DeviceKey(
14271481
key="internal_temperature_probe_1", device_id=None
14281482
),
1429-
name="Probe 1 Internal Temperature",
1430-
native_value=74,
1483+
name="Probe 1 Internal Tip Temperature",
1484+
native_value=23.3,
1485+
),
1486+
DeviceKey(
1487+
key="internal_center_temperature_probe_1", device_id=None
1488+
): SensorValue(
1489+
device_key=DeviceKey(
1490+
key="internal_center_temperature_probe_1", device_id=None
1491+
),
1492+
name="Probe 1 Internal Center Temperature",
1493+
native_value=23.6,
1494+
),
1495+
DeviceKey(
1496+
key="internal_end_temperature_probe_1", device_id=None
1497+
): SensorValue(
1498+
device_key=DeviceKey(
1499+
key="internal_end_temperature_probe_1", device_id=None
1500+
),
1501+
name="Probe 1 Internal End Temperature",
1502+
native_value=23.6,
14311503
),
14321504
DeviceKey(key="ambient_temperature_probe_1", device_id=None): SensorValue(
14331505
device_key=DeviceKey(key="ambient_temperature_probe_1", device_id=None),
14341506
name="Probe 1 Ambient Temperature",
1435-
native_value=87,
1507+
native_value=30.0,
14361508
),
14371509
},
14381510
binary_entity_descriptions={},

0 commit comments

Comments
 (0)