ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
thermopro_ble.cpp
Go to the documentation of this file.
1#include "thermopro_ble.h"
2#include <cmath>
4#include "esphome/core/log.h"
5
7
8// this size must be large enough to hold the largest data frame
9// of all supported devices
10static constexpr std::size_t MAX_DATA_SIZE = 24;
11
12struct DeviceParserMapping {
13 const char *prefix;
14 DeviceParser parser;
15};
16
17static float tp96_battery(uint16_t voltage);
18
19static optional<ParseResult> parse_tp972(const uint8_t *data, std::size_t data_size);
20static optional<ParseResult> parse_tp96(const uint8_t *data, std::size_t data_size);
21static optional<ParseResult> parse_tp3(const uint8_t *data, std::size_t data_size);
22
23ESPHOME_LOG_TAG(TAG, "thermopro_ble");
24
25static const struct DeviceParserMapping DEVICE_PARSER_MAP[] = {
26 {"TP972", parse_tp972}, {"TP970", parse_tp96}, {"TP96", parse_tp96}, {"TP3", parse_tp3}};
27
28static DeviceParser find_device_parser(StringRef device_name) {
29 for (const auto &mapping : DEVICE_PARSER_MAP) {
30 if (device_name.starts_with(mapping.prefix))
31 return mapping.parser;
32 }
33 ESP_LOGVV(TAG, "find_device_parser(): unknown device type %s.", device_name.c_str());
34 return nullptr;
35}
36
37void ThermoProBLE::dump_config() {
38 ESP_LOGCONFIG(TAG, "ThermoPro BLE");
39 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
40 LOG_SENSOR(" ", "External temperature", this->external_temperature_sensor_);
41 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
42 LOG_SENSOR(" ", "Battery Level", this->battery_level_sensor_);
43}
44
45bool ThermoProBLE::parse_device(const ble_device_base::ESPBTDevice &device) {
46 // check for matching mac address
47 if (device.address_uint64() != this->address_) {
48 ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
49 return false;
50 }
51
52 // check for valid device type
53 DeviceParser parser = find_device_parser(device.get_name());
54 if (parser == nullptr) {
55 ESP_LOGVV(TAG, "parse_device(): invalid device type.");
56 return false;
57 }
58
59 char addr_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
60 ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str_to(addr_buf));
61
62 // publish signal strength
63 float signal_strength = float(device.get_rssi());
64 PUBLISH_STATE(this->signal_strength_sensor_, signal_strength);
65
66 bool success = false;
67 for (auto &service_data : device.get_manufacturer_datas()) {
68 // check maximum data size
69 std::size_t data_size = service_data.data.size() + 2;
70 if (data_size > MAX_DATA_SIZE) {
71 ESP_LOGVV(TAG, "parse_device(): maximum data size exceeded!");
72 continue;
73 }
74
75 // reconstruct whole record from 2 byte uuid and data
76 uint16_t svc_uuid16 = service_data.uuid.uuid16();
77 uint8_t data[MAX_DATA_SIZE] = {static_cast<uint8_t>(svc_uuid16), static_cast<uint8_t>(svc_uuid16 >> 8)};
78 std::copy(service_data.data.begin(), service_data.data.end(), std::begin(data) + 2);
79
80 // dispatch data to parser
81 optional<ParseResult> result = parser(data, data_size);
82 if (!result.has_value()) {
83 continue;
84 }
85
86 // publish sensor values
87 publish_optional(this->temperature_sensor_, result->temperature);
88 publish_optional(this->external_temperature_sensor_, result->external_temperature);
89 publish_optional(this->humidity_sensor_, result->humidity);
90 publish_optional(this->battery_level_sensor_, result->battery_level);
91
92 success = true;
93 }
94
95 return success;
96}
97
98static inline uint16_t read_uint16(const uint8_t *data, std::size_t offset) {
99 return encode_value_le<uint16_t>(&data[offset]);
100}
101
102static inline int16_t read_int16(const uint8_t *data, std::size_t offset) {
103 return encode_value_le<int16_t>(&data[offset]);
104}
105
106static inline uint32_t read_uint32(const uint8_t *data, std::size_t offset) {
107 return encode_value_le<uint32_t>(&data[offset]);
108}
109
110// Battery calculation used with permission from:
111// https://github.com/Bluetooth-Devices/thermopro-ble/blob/main/src/thermopro_ble/parser.py
112//
113// TP96x battery values appear to be a voltage reading, probably in millivolts.
114// This means that calculating battery life from it is a non-linear function.
115// Examining the curve, it looked fairly close to a curve from the tanh function.
116// So, I created a script to use Tensorflow to optimize an equation in the format
117// A*tanh(B*x+C)+D
118// Where A,B,C,D are the variables to optimize for. This yielded the below function
119static float tp96_battery(uint16_t voltage) {
120 float level = 52.317286f * std::tanh(static_cast<float>(voltage) / 273.624277936f - 8.76485439394f) + 51.06925f;
121 return std::max(0.0f, std::min(level, 100.0f));
122}
123
124static optional<ParseResult> parse_tp972(const uint8_t *data, std::size_t data_size) {
125 if (data_size != 23) {
126 ESP_LOGVV(TAG, "parse_tp972(): payload has wrong size of %d (!= 23)!", data_size);
127 return {};
128 }
129
130 ParseResult result;
131
132 // ambient temperature, 2 bytes, 16-bit unsigned integer, -54 °C offset
133 result.external_temperature = static_cast<float>(read_uint16(data, 1)) - 54.0f;
134
135 // battery level, 2 bytes, 16-bit unsigned integer, voltage (convert to percentage)
136 result.battery_level = tp96_battery(read_uint16(data, 3));
137
138 // internal temperature, 4 bytes, float, -54 °C offset
139 result.temperature = static_cast<float>(read_uint32(data, 9)) - 54.0f;
140
141 return result;
142}
143
144static optional<ParseResult> parse_tp96(const uint8_t *data, std::size_t data_size) {
145 if (data_size != 7) {
146 ESP_LOGVV(TAG, "parse_tp96(): payload has wrong size of %d (!= 7)!", data_size);
147 return {};
148 }
149
150 ParseResult result;
151
152 // internal temperature, 2 bytes, 16-bit unsigned integer, -30 °C offset
153 result.temperature = static_cast<float>(read_uint16(data, 1)) - 30.0f;
154
155 // battery level, 2 bytes, 16-bit unsigned integer, voltage (convert to percentage)
156 result.battery_level = tp96_battery(read_uint16(data, 3));
157
158 // ambient temperature, 2 bytes, 16-bit unsigned integer, -30 °C offset
159 result.external_temperature = static_cast<float>(read_uint16(data, 5)) - 30.0f;
160
161 return result;
162}
163
164static optional<ParseResult> parse_tp3(const uint8_t *data, std::size_t data_size) {
165 if (data_size < 6) {
166 ESP_LOGVV(TAG, "parse_tp3(): payload has wrong size of %d (< 6)!", data_size);
167 return {};
168 }
169
170 ParseResult result;
171
172 // temperature, 2 bytes, 16-bit signed integer, 0.1 °C
173 result.temperature = static_cast<float>(read_int16(data, 1)) * 0.1f;
174
175 // humidity, 1 byte, 8-bit unsigned integer, 1.0 %
176 result.humidity = static_cast<float>(data[3]);
177
178 // battery level, 2 bits (0-2)
179 result.battery_level = static_cast<float>(data[4] & 0x3) * 50.0f;
180
181 return result;
182}
183
184} // namespace esphome::thermopro_ble
FLAG_HAS_TRANSITION float
std::span< const uint8_t > data
optional< ParseResult >(*)(const uint8_t *data, std::size_t data_size) DeviceParser
ESPHOME_LOG_TAG(TAG, "thermopro_ble")
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
Definition helpers.h:1084
void publish_optional(E *entity, const optional< V > &value)
Publish value to entity when the entity is configured and the value is set.
static void uint32_t