ESPHome  2024.10.2
inkbird_ibsth1_mini.cpp
Go to the documentation of this file.
1 #include "inkbird_ibsth1_mini.h"
2 #include "esphome/core/log.h"
3 
4 #ifdef USE_ESP32
5 
6 namespace esphome {
7 namespace inkbird_ibsth1_mini {
8 
9 static const char *const TAG = "inkbird_ibsth1_mini";
10 
12  ESP_LOGCONFIG(TAG, "Inkbird IBS TH1 MINI");
13  LOG_SENSOR(" ", "Temperature", this->temperature_);
14  LOG_SENSOR(" ", "External Temperature", this->external_temperature_);
15  LOG_SENSOR(" ", "Humidity", this->humidity_);
16  LOG_SENSOR(" ", "Battery Level", this->battery_level_);
17 }
18 
20  // The below is based on my research and reverse engineering of a single device
21  // It is entirely possible that some of that may be inaccurate or incomplete
22 
23  // for Inkbird IBS-TH1 Mini device we expect
24  // 1) expected mac address
25  // 2) device address type == PUBLIC
26  // 3) no service data
27  // 4) one manufacturer data
28  // 5) the manufacturer data should contain a 16-bit uuid amd a 7-byte data vector
29  // 6) the 7-byte data component should have data[2] == 0 and data[6] == 8
30 
31  // the address should match the address we declared
32  if (device.address_uint64() != this->address_) {
33  ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
34  return false;
35  }
36  if (device.get_address_type() != BLE_ADDR_TYPE_PUBLIC) {
37  ESP_LOGVV(TAG, "parse_device(): address is not public");
38  return false;
39  }
40  if (!device.get_service_datas().empty()) {
41  ESP_LOGVV(TAG, "parse_device(): service_data is expected to be empty");
42  return false;
43  }
44  auto mnf_datas = device.get_manufacturer_datas();
45  if (mnf_datas.size() != 1) {
46  ESP_LOGVV(TAG, "parse_device(): manufacturer_datas is expected to have a single element");
47  return false;
48  }
49  auto mnf_data = mnf_datas[0];
50  if (mnf_data.uuid.get_uuid().len != ESP_UUID_LEN_16) {
51  ESP_LOGVV(TAG, "parse_device(): manufacturer data element is expected to have uuid of length 16");
52  return false;
53  }
54  if (mnf_data.data.size() != 7) {
55  ESP_LOGVV(TAG, "parse_device(): manufacturer data element length is expected to be of length 7");
56  return false;
57  }
58  if ((mnf_data.data[6] != 8) && (mnf_data.data[6] != 6)) {
59  ESP_LOGVV(TAG, "parse_device(): unexpected data");
60  return false;
61  }
62 
63  // sensor output encoding
64  // data[5] is a battery level
65  // data[0] and data[1] is humidity * 100 (in pct)
66  // uuid is a temperature * 100 (in Celsius)
67  // when data[2] == 0 temperature is from internal sensor (IBS-TH1 or IBS-TH1 Mini)
68  // when data[2] == 1 temperature is from external sensor (IBS-TH1 only)
69 
70  // Create empty variables to pass automatic checks
71  auto temperature = NAN;
72  auto external_temperature = NAN;
73 
74  // Read bluetooth data into variable
75  auto measured_temperature = ((int16_t) mnf_data.uuid.get_uuid().uuid.uuid16) / 100.0f;
76 
77  // Set temperature or external_temperature based on which sensor is in use
78  if (mnf_data.data[2] == 0) {
79  temperature = measured_temperature;
80  } else if (mnf_data.data[2] == 1) {
81  external_temperature = measured_temperature;
82  } else {
83  ESP_LOGVV(TAG, "parse_device(): unknown sensor type");
84  return false;
85  }
86 
87  auto battery_level = mnf_data.data[5];
88  auto humidity = ((mnf_data.data[1] << 8) + mnf_data.data[0]) / 100.0f;
89 
90  // Send temperature only if the value is set
91  if (!std::isnan(temperature) && this->temperature_ != nullptr) {
93  }
94  if (!std::isnan(external_temperature) && this->external_temperature_ != nullptr) {
95  this->external_temperature_->publish_state(external_temperature);
96  }
97  if (this->humidity_ != nullptr) {
98  this->humidity_->publish_state(humidity);
99  }
100  if (this->battery_level_ != nullptr) {
101  this->battery_level_->publish_state(battery_level);
102  }
103 
104  return true;
105 }
106 
107 } // namespace inkbird_ibsth1_mini
108 } // namespace esphome
109 
110 #endif
const std::vector< ServiceData > & get_manufacturer_datas() const
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
uint16_t temperature
Definition: sun_gtil2.cpp:26
const std::vector< ServiceData > & get_service_datas() const
esp_ble_addr_type_t get_address_type() const
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7