Merge branch 'atc_mithermometer' of github.com:ahpohl/esphome into atc_mithermometer

This commit is contained in:
Alexander Pohl 2020-10-11 11:22:36 +02:00
commit 6d8fc4e5cb
3 changed files with 18 additions and 5 deletions

View file

@ -13,6 +13,7 @@ void ATCMiThermometer::dump_config() {
LOG_SENSOR(" ", "Temperature", this->temperature_); LOG_SENSOR(" ", "Temperature", this->temperature_);
LOG_SENSOR(" ", "Humidity", this->humidity_); LOG_SENSOR(" ", "Humidity", this->humidity_);
LOG_SENSOR(" ", "Battery Level", this->battery_level_); LOG_SENSOR(" ", "Battery Level", this->battery_level_);
LOG_SENSOR(" ", "Battery Voltage", this->battery_voltage_);
} }
bool ATCMiThermometer::parse_device(const esp32_ble_tracker::ESPBTDevice &device) { bool ATCMiThermometer::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
@ -40,6 +41,8 @@ bool ATCMiThermometer::parse_device(const esp32_ble_tracker::ESPBTDevice &device
this->humidity_->publish_state(*res->humidity); this->humidity_->publish_state(*res->humidity);
if (res->battery_level.has_value() && this->battery_level_ != nullptr) if (res->battery_level.has_value() && this->battery_level_ != nullptr)
this->battery_level_->publish_state(*res->battery_level); this->battery_level_->publish_state(*res->battery_level);
if (res->battery_voltage.has_value() && this->battery_voltage_ != nullptr)
this->battery_voltage_->publish_state(*res->battery_voltage);
success = true; success = true;
} }
@ -72,7 +75,6 @@ optional<ParseResult> ATCMiThermometer::parse_header(const esp32_ble_tracker::Se
} }
bool ATCMiThermometer::parse_message(const std::vector<uint8_t> &message, ParseResult &result) { bool ATCMiThermometer::parse_message(const std::vector<uint8_t> &message, ParseResult &result) {
// Byte 0-5 mac in correct order // Byte 0-5 mac in correct order
// Byte 6-7 Temperature in uint16 // Byte 6-7 Temperature in uint16
// Byte 8 Humidity in percent // Byte 8 Humidity in percent
@ -99,8 +101,8 @@ bool ATCMiThermometer::parse_message(const std::vector<uint8_t> &message, ParseR
result.battery_level = data[9]; result.battery_level = data[9];
// battery, 2 bytes, 16-bit unsigned integer, 0.001 V // battery, 2 bytes, 16-bit unsigned integer, 0.001 V
// const int16_t battery_level = uint16_t(data[11]) | (uint16_t(data[10]) << 8); const int16_t battery_voltage = uint16_t(data[11]) | (uint16_t(data[10]) << 8);
// result.battery_level = battery_level / 1.0e3f; result.battery_voltage = battery_voltage / 1.0e3f;
return true; return true;
} }
@ -122,6 +124,9 @@ bool ATCMiThermometer::report_results(const optional<ParseResult> &result, const
if (result->battery_level.has_value()) { if (result->battery_level.has_value()) {
ESP_LOGD(TAG, " Battery Level: %.0f %%", *result->battery_level); ESP_LOGD(TAG, " Battery Level: %.0f %%", *result->battery_level);
} }
if (result->battery_voltage.has_value()) {
ESP_LOGD(TAG, " Battery Voltage: %.3f V", *result->battery_voltage);
}
return true; return true;
} }

View file

@ -13,6 +13,7 @@ struct ParseResult {
optional<float> temperature; optional<float> temperature;
optional<float> humidity; optional<float> humidity;
optional<float> battery_level; optional<float> battery_level;
optional<float> battery_voltage;
bool is_duplicate; bool is_duplicate;
int raw_offset; int raw_offset;
}; };
@ -27,6 +28,7 @@ class ATCMiThermometer : public Component, public esp32_ble_tracker::ESPBTDevice
void set_temperature(sensor::Sensor *temperature) { temperature_ = temperature; } void set_temperature(sensor::Sensor *temperature) { temperature_ = temperature; }
void set_humidity(sensor::Sensor *humidity) { humidity_ = humidity; } void set_humidity(sensor::Sensor *humidity) { humidity_ = humidity; }
void set_battery_level(sensor::Sensor *battery_level) { battery_level_ = battery_level; } void set_battery_level(sensor::Sensor *battery_level) { battery_level_ = battery_level; }
void set_battery_voltage(sensor::Sensor *battery_voltage) { battery_voltage_ = battery_voltage; }
optional<ParseResult> parse_header(const esp32_ble_tracker::ServiceData &service_data); optional<ParseResult> parse_header(const esp32_ble_tracker::ServiceData &service_data);
bool parse_message(const std::vector<uint8_t> &message, ParseResult &result); bool parse_message(const std::vector<uint8_t> &message, ParseResult &result);
@ -37,6 +39,7 @@ class ATCMiThermometer : public Component, public esp32_ble_tracker::ESPBTDevice
sensor::Sensor *temperature_{nullptr}; sensor::Sensor *temperature_{nullptr};
sensor::Sensor *humidity_{nullptr}; sensor::Sensor *humidity_{nullptr};
sensor::Sensor *battery_level_{nullptr}; sensor::Sensor *battery_level_{nullptr};
sensor::Sensor *battery_voltage_{nullptr};
}; };
} // namespace atc_mithermometer } // namespace atc_mithermometer

View file

@ -1,8 +1,9 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import sensor, esp32_ble_tracker from esphome.components import sensor, esp32_ble_tracker
from esphome.const import CONF_BATTERY_LEVEL, CONF_HUMIDITY, CONF_MAC_ADDRESS, CONF_TEMPERATURE, \ from esphome.const import CONF_BATTERY_LEVEL, CONF_BATTERY_VOLTAGE, CONF_MAC_ADDRESS, \
UNIT_CELSIUS, ICON_THERMOMETER, UNIT_PERCENT, ICON_WATER_PERCENT, ICON_BATTERY, CONF_ID CONF_HUMIDITY, CONF_TEMPERATURE, CONF_ID, UNIT_CELSIUS, UNIT_PERCENT, UNIT_VOLT, \
ICON_BATTERY, ICON_THERMOMETER, ICON_WATER_PERCENT
DEPENDENCIES = ['esp32_ble_tracker'] DEPENDENCIES = ['esp32_ble_tracker']
@ -17,6 +18,7 @@ CONFIG_SCHEMA = cv.Schema({
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(UNIT_CELSIUS, ICON_THERMOMETER, 1), cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(UNIT_CELSIUS, ICON_THERMOMETER, 1),
cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(UNIT_PERCENT, ICON_WATER_PERCENT, 0), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(UNIT_PERCENT, ICON_WATER_PERCENT, 0),
cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(UNIT_PERCENT, ICON_BATTERY, 0), cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(UNIT_PERCENT, ICON_BATTERY, 0),
cv.Optional(CONF_BATTERY_VOLTAGE): sensor.sensor_schema(UNIT_VOLT, ICON_BATTERY, 3),
}).extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA).extend(cv.COMPONENT_SCHEMA) }).extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA).extend(cv.COMPONENT_SCHEMA)
@ -36,3 +38,6 @@ def to_code(config):
if CONF_BATTERY_LEVEL in config: if CONF_BATTERY_LEVEL in config:
sens = yield sensor.new_sensor(config[CONF_BATTERY_LEVEL]) sens = yield sensor.new_sensor(config[CONF_BATTERY_LEVEL])
cg.add(var.set_battery_level(sens)) cg.add(var.set_battery_level(sens))
if CONF_BATTERY_VOLTAGE in config:
sens = yield sensor.new_sensor(config[CONF_BATTERY_VOLTAGE])
cg.add(var.set_battery_voltage(sens))