mirror of
https://github.com/esphome/esphome.git
synced 2024-11-25 16:38:16 +01:00
report battery level in volt
This commit is contained in:
parent
38b7c1588a
commit
b49fa849ac
2 changed files with 17 additions and 15 deletions
|
@ -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) {
|
bool ATCMiThermometer::parse_message(const std::vector<uint8_t> &message, ParseResult &result) {
|
||||||
|
|
||||||
// Byte 5-10 mac in correct order
|
// Byte 0-5 mac in correct order
|
||||||
// Byte 11-12 Temperature in uint16
|
// Byte 6-7 Temperature in uint16
|
||||||
// Byte 13 Humidity in percent
|
// Byte 8 Humidity in percent
|
||||||
// Byte 14 Battery in percent
|
// Byte 9 Battery in percent
|
||||||
// Byte 15-16 Battery in mV uint16_t
|
// Byte 10-11 Battery in mV uint16_t
|
||||||
// Byte 17 frame packet counter
|
// Byte 12 frame packet counter
|
||||||
|
|
||||||
const uint8_t *data = message.data();
|
const uint8_t *data = message.data();
|
||||||
const int data_length = 13;
|
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);
|
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, 1 %
|
// humidity, 1 byte, 8-bit unsigned integer, 0.1 %
|
||||||
result.humidity = data[8];
|
result.humidity = data[8];
|
||||||
|
|
||||||
// battery, 1 byte, 8-bit unsigned integer, 1 %
|
// battery, 1 byte, 8-bit unsigned integer, 0.001 V
|
||||||
result.battery_level = data[9];
|
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;
|
||||||
}
|
}
|
||||||
|
@ -110,13 +111,13 @@ bool ATCMiThermometer::report_results(const optional<ParseResult> &result, const
|
||||||
ESP_LOGD(TAG, "Got ATC MiThermometer (%s):", address.c_str());
|
ESP_LOGD(TAG, "Got ATC MiThermometer (%s):", address.c_str());
|
||||||
|
|
||||||
if (result->temperature.has_value()) {
|
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()) {
|
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()) {
|
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;
|
return true;
|
||||||
|
|
|
@ -2,7 +2,8 @@ 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']
|
||||||
|
|
||||||
|
@ -15,8 +16,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, 0),
|
cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(UNIT_PERCENT, ICON_WATER_PERCENT, 1),
|
||||||
cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(UNIT_PERCENT, ICON_BATTERY, 0),
|
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)
|
}).extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA).extend(cv.COMPONENT_SCHEMA)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue