report battery level in volt

This commit is contained in:
Alexander Pohl 2020-09-05 12:34:28 +02:00
parent 38b7c1588a
commit b49fa849ac
2 changed files with 17 additions and 15 deletions

View file

@ -73,12 +73,12 @@ optional<ParseResult> ATCMiThermometer::parse_header(const esp32_ble_tracker::Se
bool ATCMiThermometer::parse_message(const std::vector<uint8_t> &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<uint8_t> &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;
}
@ -116,7 +117,7 @@ bool ATCMiThermometer::report_results(const optional<ParseResult> &result, const
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;

View file

@ -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)