10static constexpr std::size_t MAX_DATA_SIZE = 24;
12struct DeviceParserMapping {
17static float tp96_battery(uint16_t voltage);
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);
25static const struct DeviceParserMapping DEVICE_PARSER_MAP[] = {
26 {
"TP972", parse_tp972}, {
"TP970", parse_tp96}, {
"TP96", parse_tp96}, {
"TP3", parse_tp3}};
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;
33 ESP_LOGVV(TAG,
"find_device_parser(): unknown device type %s.", device_name.c_str());
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_);
45bool ThermoProBLE::parse_device(
const ble_device_base::ESPBTDevice &device) {
47 if (device.address_uint64() != this->address_) {
48 ESP_LOGVV(TAG,
"parse_device(): unknown MAC address.");
53 DeviceParser parser = find_device_parser(device.get_name());
54 if (parser ==
nullptr) {
55 ESP_LOGVV(TAG,
"parse_device(): invalid device type.");
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));
63 float signal_strength =
float(device.get_rssi());
64 PUBLISH_STATE(this->signal_strength_sensor_, signal_strength);
67 for (
auto &service_data : device.get_manufacturer_datas()) {
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!");
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);
81 optional<ParseResult> result = parser(data, data_size);
82 if (!result.has_value()) {
88 publish_optional(this->external_temperature_sensor_, result->external_temperature);
98static inline uint16_t read_uint16(
const uint8_t *data, std::size_t offset) {
102static inline int16_t read_int16(
const uint8_t *data, std::size_t offset) {
106static inline uint32_t read_uint32(
const uint8_t *data, std::size_t offset) {
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));
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);
133 result.external_temperature =
static_cast<float>(read_uint16(data, 1)) - 54.0f;
136 result.battery_level = tp96_battery(read_uint16(data, 3));
139 result.temperature =
static_cast<float>(read_uint32(data, 9)) - 54.0f;
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);
153 result.temperature =
static_cast<float>(read_uint16(data, 1)) - 30.0f;
156 result.battery_level = tp96_battery(read_uint16(data, 3));
159 result.external_temperature =
static_cast<float>(read_uint16(data, 5)) - 30.0f;
164static optional<ParseResult> parse_tp3(
const uint8_t *data, std::size_t data_size) {
166 ESP_LOGVV(TAG,
"parse_tp3(): payload has wrong size of %d (< 6)!", data_size);
173 result.temperature =
static_cast<float>(read_int16(data, 1)) * 0.1f;
176 result.humidity =
static_cast<float>(
data[3]);
179 result.battery_level =
static_cast<float>(
data[4] & 0x3) * 50.0f;