Add support for IBS-TH1 External Sensor (#1983)

This commit is contained in:
Mikko Tervala 2021-07-12 23:55:53 +03:00 committed by GitHub
parent d77c3abdc0
commit cc9f0b3f47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 3 deletions

View file

@ -11,6 +11,7 @@ static const char *const TAG = "inkbird_ibsth1_mini";
void InkbirdIBSTH1_MINI::dump_config() {
ESP_LOGCONFIG(TAG, "Inkbird IBS TH1 MINI");
LOG_SENSOR(" ", "Temperature", this->temperature_);
LOG_SENSOR(" ", "External Temperature", this->external_temperature_);
LOG_SENSOR(" ", "Humidity", this->humidity_);
LOG_SENSOR(" ", "Battery Level", this->battery_level_);
}
@ -54,7 +55,7 @@ bool InkbirdIBSTH1_MINI::parse_device(const esp32_ble_tracker::ESPBTDevice &devi
ESP_LOGVV(TAG, "parse_device(): manufacturer data element length is expected to be of length 7");
return false;
}
if ((mnfData.data[2] != 0) || (mnfData.data[6] != 8)) {
if (mnfData.data[6] != 8) {
ESP_LOGVV(TAG, "parse_device(): unexpected data");
return false;
}
@ -63,13 +64,36 @@ bool InkbirdIBSTH1_MINI::parse_device(const esp32_ble_tracker::ESPBTDevice &devi
// data[5] is a battery level
// data[0] and data[1] is humidity * 100 (in pct)
// uuid is a temperature * 100 (in Celcius)
// when data[2] == 0 temperature is from internal sensor (IBS-TH1 or IBS-TH1 Mini)
// when data[2] == 1 temperature is from external sensor (IBS-TH1 only)
// Create empty variables to pass automatic checks
auto temperature = NAN;
auto external_temperature = NAN;
// Read bluetooth data into variable
auto measured_temperature = mnfData.uuid.get_uuid().uuid.uuid16 / 100.0f;
// Set temperature or external_temperature based on which sensor is in use
if (mnfData.data[2] == 0) {
temperature = measured_temperature;
} else if (mnfData.data[2] == 1) {
external_temperature = measured_temperature;
} else {
ESP_LOGVV(TAG, "parse_device(): unknown sensor type");
return false;
}
auto battery_level = mnfData.data[5];
auto temperature = mnfData.uuid.get_uuid().uuid.uuid16 / 100.0f;
auto humidity = ((mnfData.data[1] << 8) + mnfData.data[0]) / 100.0f;
if (this->temperature_ != nullptr) {
// Send temperature only if the value is set
if (!isnan(temperature) && this->temperature_ != nullptr) {
this->temperature_->publish_state(temperature);
}
if (!isnan(external_temperature) && this->external_temperature_ != nullptr) {
this->external_temperature_->publish_state(external_temperature);
}
if (this->humidity_ != nullptr) {
this->humidity_->publish_state(humidity);
}

View file

@ -18,12 +18,14 @@ class InkbirdIBSTH1_MINI : public Component, public esp32_ble_tracker::ESPBTDevi
void dump_config() override;
float get_setup_priority() const override { return setup_priority::DATA; }
void set_temperature(sensor::Sensor *temperature) { temperature_ = temperature; }
void set_external_temperature(sensor::Sensor *external_temperature) { external_temperature_ = external_temperature; }
void set_humidity(sensor::Sensor *humidity) { humidity_ = humidity; }
void set_battery_level(sensor::Sensor *battery_level) { battery_level_ = battery_level; }
protected:
uint64_t address_;
sensor::Sensor *temperature_{nullptr};
sensor::Sensor *external_temperature_{nullptr};
sensor::Sensor *humidity_{nullptr};
sensor::Sensor *battery_level_{nullptr};
};

View file

@ -19,6 +19,8 @@ from esphome.const import (
CODEOWNERS = ["@fkirill"]
DEPENDENCIES = ["esp32_ble_tracker"]
CONF_EXTERNAL_TEMPERATURE = "external_temperature"
inkbird_ibsth1_mini_ns = cg.esphome_ns.namespace("inkbird_ibsth1_mini")
InkbirdUBSTH1_MINI = inkbird_ibsth1_mini_ns.class_(
"InkbirdIBSTH1_MINI", esp32_ble_tracker.ESPBTDeviceListener, cg.Component
@ -36,6 +38,13 @@ CONFIG_SCHEMA = (
DEVICE_CLASS_TEMPERATURE,
STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_EXTERNAL_TEMPERATURE): sensor.sensor_schema(
UNIT_CELSIUS,
ICON_EMPTY,
1,
DEVICE_CLASS_TEMPERATURE,
STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(
UNIT_PERCENT,
ICON_EMPTY,
@ -67,6 +76,9 @@ async def to_code(config):
if CONF_TEMPERATURE in config:
sens = await sensor.new_sensor(config[CONF_TEMPERATURE])
cg.add(var.set_temperature(sens))
if CONF_EXTERNAL_TEMPERATURE in config:
sens = await sensor.new_sensor(config[CONF_EXTERNAL_TEMPERATURE])
cg.add(var.set_external_temperature(sens))
if CONF_HUMIDITY in config:
sens = await sensor.new_sensor(config[CONF_HUMIDITY])
cg.add(var.set_humidity(sens))