From b49fa849ac69acf2f3d8bd3a3e8915e92277c581 Mon Sep 17 00:00:00 2001 From: Alexander Pohl Date: Sat, 5 Sep 2020 12:34:28 +0200 Subject: [PATCH] report battery level in volt --- .../atc_mithermometer/atc_mithermometer.cpp | 25 ++++++++++--------- .../components/atc_mithermometer/sensor.py | 7 +++--- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/esphome/components/atc_mithermometer/atc_mithermometer.cpp b/esphome/components/atc_mithermometer/atc_mithermometer.cpp index 293133d56e..9e0b9d018b 100644 --- a/esphome/components/atc_mithermometer/atc_mithermometer.cpp +++ b/esphome/components/atc_mithermometer/atc_mithermometer.cpp @@ -73,12 +73,12 @@ optional ATCMiThermometer::parse_header(const esp32_ble_tracker::Se bool ATCMiThermometer::parse_message(const std::vector &message, ParseResult &result) { - // Byte 5-10 mac in correct order - // Byte 11-12 Temperature in uint16 - // Byte 13 Humidity in percent - // Byte 14 Battery in percent - // Byte 15-16 Battery in mV uint16_t - // Byte 17 frame packet counter + // Byte 0-5 mac in correct order + // Byte 6-7 Temperature in uint16 + // Byte 8 Humidity in percent + // Byte 9 Battery in percent + // Byte 10-11 Battery in mV uint16_t + // Byte 12 frame packet counter const uint8_t *data = message.data(); const int data_length = 13; @@ -92,11 +92,12 @@ bool ATCMiThermometer::parse_message(const std::vector &message, ParseR const int16_t temperature = uint16_t(data[7]) | (uint16_t(data[6]) << 8); result.temperature = temperature / 10.0f; - // humidity, 1 byte, 8-bit unsigned integer, 1 % + // humidity, 1 byte, 8-bit unsigned integer, 0.1 % result.humidity = data[8]; - // battery, 1 byte, 8-bit unsigned integer, 1 % - result.battery_level = data[9]; + // battery, 1 byte, 8-bit unsigned integer, 0.001 V + const int16_t battery_level = uint16_t(data[11]) | (uint16_t(data[10]) << 8); + result.battery_level = battery_level / 1.0e3f; return true; } @@ -110,13 +111,13 @@ bool ATCMiThermometer::report_results(const optional &result, const ESP_LOGD(TAG, "Got ATC MiThermometer (%s):", address.c_str()); if (result->temperature.has_value()) { - ESP_LOGD(TAG, " Temperature: %.1f°C", *result->temperature); + ESP_LOGD(TAG, " Temperature: %.1f °C", *result->temperature); } if (result->humidity.has_value()) { - ESP_LOGD(TAG, " Humidity: %.1f%%", *result->humidity); + ESP_LOGD(TAG, " Humidity: %.1f %%", *result->humidity); } if (result->battery_level.has_value()) { - ESP_LOGD(TAG, " Battery Level: %.0f%%", *result->battery_level); + ESP_LOGD(TAG, " Battery Level: %.3f V", *result->battery_level); } return true; diff --git a/esphome/components/atc_mithermometer/sensor.py b/esphome/components/atc_mithermometer/sensor.py index c4f2dbb1c1..3c98c2367c 100644 --- a/esphome/components/atc_mithermometer/sensor.py +++ b/esphome/components/atc_mithermometer/sensor.py @@ -2,7 +2,8 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import sensor, esp32_ble_tracker from esphome.const import CONF_BATTERY_LEVEL, CONF_HUMIDITY, CONF_MAC_ADDRESS, CONF_TEMPERATURE, \ - UNIT_CELSIUS, ICON_THERMOMETER, UNIT_PERCENT, ICON_WATER_PERCENT, ICON_BATTERY, CONF_ID + UNIT_CELSIUS, ICON_THERMOMETER, UNIT_PERCENT, ICON_WATER_PERCENT, ICON_BATTERY, CONF_ID, \ + UNIT_VOLT DEPENDENCIES = ['esp32_ble_tracker'] @@ -15,8 +16,8 @@ CONFIG_SCHEMA = cv.Schema({ cv.GenerateID(): cv.declare_id(ATCMiThermometer), cv.Required(CONF_MAC_ADDRESS): cv.mac_address, 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_BATTERY_LEVEL): sensor.sensor_schema(UNIT_PERCENT, ICON_BATTERY, 0), + cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(UNIT_PERCENT, ICON_WATER_PERCENT, 1), + cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(UNIT_VOLT, ICON_BATTERY, 3), }).extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA).extend(cv.COMPONENT_SCHEMA)