report battery level again in percent

This commit is contained in:
Alexander Pohl 2020-09-05 12:57:05 +02:00
parent b49fa849ac
commit f67e803af5
2 changed files with 12 additions and 10 deletions

View file

@ -92,12 +92,15 @@ bool ATCMiThermometer::parse_message(const std::vector<uint8_t> &message, ParseR
const int16_t temperature = uint16_t(data[7]) | (uint16_t(data[6]) << 8); const int16_t temperature = uint16_t(data[7]) | (uint16_t(data[6]) << 8);
result.temperature = temperature / 10.0f; result.temperature = temperature / 10.0f;
// humidity, 1 byte, 8-bit unsigned integer, 0.1 % // humidity, 1 byte, 8-bit unsigned integer, 1.0 %
result.humidity = data[8]; result.humidity = data[8];
// battery, 1 byte, 8-bit unsigned integer, 0.001 V // battery, 1 byte, 8-bit unsigned integer, 1.0 %
const int16_t battery_level = uint16_t(data[11]) | (uint16_t(data[10]) << 8); result.battery_level = data[9];
result.battery_level = battery_level / 1.0e3f;
// battery, 2 bytes, 16-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; return true;
} }
@ -114,10 +117,10 @@ bool ATCMiThermometer::report_results(const optional<ParseResult> &result, const
ESP_LOGD(TAG, " Temperature: %.1f °C", *result->temperature); ESP_LOGD(TAG, " Temperature: %.1f °C", *result->temperature);
} }
if (result->humidity.has_value()) { if (result->humidity.has_value()) {
ESP_LOGD(TAG, " Humidity: %.1f %%", *result->humidity); ESP_LOGD(TAG, " Humidity: %.0f %%", *result->humidity);
} }
if (result->battery_level.has_value()) { if (result->battery_level.has_value()) {
ESP_LOGD(TAG, " Battery Level: %.3f V", *result->battery_level); ESP_LOGD(TAG, " Battery Level: %.0f %%", *result->battery_level);
} }
return true; return true;

View file

@ -2,8 +2,7 @@ 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_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'] DEPENDENCIES = ['esp32_ble_tracker']
@ -16,8 +15,8 @@ CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(ATCMiThermometer), cv.GenerateID(): cv.declare_id(ATCMiThermometer),
cv.Required(CONF_MAC_ADDRESS): cv.mac_address, cv.Required(CONF_MAC_ADDRESS): cv.mac_address,
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, 1), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(UNIT_PERCENT, ICON_WATER_PERCENT, 0),
cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(UNIT_VOLT, ICON_BATTERY, 3), cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(UNIT_PERCENT, ICON_BATTERY, 0),
}).extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA).extend(cv.COMPONENT_SCHEMA) }).extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA).extend(cv.COMPONENT_SCHEMA)