Skip to content

Commit c396f81

Browse files
authored
Temp spike battery improvements (#21)
1 parent f768afc commit c396f81

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/thermopro_ble/parser.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from __future__ import annotations
99

1010
import logging
11+
from math import tanh
1112
from struct import Struct
1213

1314
from bluetooth_data_tools import short_address
@@ -27,8 +28,17 @@
2728
UNPACK_TEMP_HUMID = Struct("<hB").unpack
2829
UNPACK_SPIKE_TEMP = Struct("<BHHH").unpack
2930

30-
TP96_MAX_BAT = 2880
31-
TP96_MIN_BAT = 2000 # ??
31+
32+
# TP96x battery values appear to be a voltage reading, probably in millivolts.
33+
# This means that calculating battery life from it is a non-linear function.
34+
# Examining the curve, it looked fairly close to a curve from the tanh function.
35+
# So, I created a script to use Tensorflow to optimize an equation in the format
36+
# A*tanh(B*x+C)+D
37+
# Where A,B,C,D are the variables to optimize for. This yielded the below function
38+
def tp96_battery(voltage: int) -> float:
39+
raw = 52.317286 * tanh(voltage / 273.624277936 - 8.76485439394) + 51.06925
40+
clamped = max(0, min(raw, 100))
41+
return round(clamped, 2)
3242

3343

3444
class ThermoProBluetoothDeviceData(BluetoothData):
@@ -68,22 +78,18 @@ def _start_update(self, service_info: BluetoothServiceInfo) -> None:
6878
return
6979

7080
if name.startswith("TP96"):
71-
bat_range = TP96_MAX_BAT - TP96_MIN_BAT
72-
7381
# TP96 has a different format
7482
# It has an internal temp probe and an ambient temp probe
7583
(
7684
probe_zero_indexed,
7785
internal_temp,
78-
battery,
86+
battery_voltage,
7987
ambient_temp,
8088
) = UNPACK_SPIKE_TEMP(data)
8189
probe_one_indexed = probe_zero_indexed + 1
8290
internal_temp = internal_temp - 30
8391
ambient_temp = ambient_temp - 30
84-
battery_percent = ((battery - TP96_MIN_BAT) / bat_range) * 100
85-
if battery_percent > 100:
86-
battery_percent = 100
92+
battery_percent = tp96_battery(battery_voltage)
8793
self.update_predefined_sensor(
8894
SensorLibrary.TEMPERATURE__CELSIUS,
8995
internal_temp,

tests/test_parser.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ def test_tp960r():
339339
DeviceKey(key="battery_probe_1", device_id=None): SensorValue(
340340
device_key=DeviceKey(key="battery_probe_1", device_id=None),
341341
name="Probe 1 Battery",
342-
native_value=11,
342+
native_value=9,
343343
),
344344
},
345345
binary_entity_descriptions={},
@@ -405,7 +405,7 @@ def test_tp960r():
405405
DeviceKey(key="battery_probe_1", device_id=None): SensorValue(
406406
device_key=DeviceKey(key="battery_probe_1", device_id=None),
407407
name="Probe 1 Battery",
408-
native_value=11,
408+
native_value=9,
409409
),
410410
},
411411
binary_entity_descriptions={},
@@ -475,7 +475,7 @@ def test_tp962r():
475475
DeviceKey(key="battery_probe_2", device_id=None): SensorValue(
476476
device_key=DeviceKey(key="battery_probe_2", device_id=None),
477477
name="Probe 2 Battery",
478-
native_value=99,
478+
native_value=100.0,
479479
),
480480
},
481481
binary_entity_descriptions={},
@@ -567,12 +567,12 @@ def test_tp962r():
567567
DeviceKey(key="battery_probe_1", device_id=None): SensorValue(
568568
device_key=DeviceKey(key="battery_probe_1", device_id=None),
569569
name="Probe 1 Battery",
570-
native_value=66,
570+
native_value=82,
571571
),
572572
DeviceKey(key="battery_probe_2", device_id=None): SensorValue(
573573
device_key=DeviceKey(key="battery_probe_2", device_id=None),
574574
name="Probe 2 Battery",
575-
native_value=99,
575+
native_value=100.0,
576576
),
577577
DeviceKey(key="internal_temperature_probe_2", device_id=None): SensorValue(
578578
device_key=DeviceKey(

0 commit comments

Comments
 (0)