14static constexpr uint8_t REG_CONTROL = 0x00;
15static constexpr uint8_t REG_TEMPERATURE = 0x06;
16static constexpr uint8_t REG_VOLTAGE = 0x08;
17static constexpr uint8_t REG_CURRENT = 0x0C;
18static constexpr uint8_t REG_REMAINING_CAPACITY = 0x10;
19static constexpr uint8_t REG_FULL_CHARGE_CAPACITY = 0x12;
20static constexpr uint8_t REG_TIME_TO_EMPTY = 0x16;
21static constexpr uint8_t REG_STATE_OF_CHARGE = 0x2C;
22static constexpr uint8_t REG_STATE_OF_HEALTH = 0x2E;
24static constexpr uint8_t REG_MAC_DATA = 0x40;
25static constexpr uint16_t DEVICE_NUMBER = 0x0220;
26static constexpr uint16_t TIME_TO_EMPTY_NOT_APPLICABLE = 0xFFFF;
29 const uint8_t device_number_cmd[2] = {0x01, 0x00};
31 this->
mark_failed(LOG_STR(
"Failed to communicate with BQ27220"));
35 uint16_t device_number = 0;
37 this->
mark_failed(LOG_STR(
"Failed to read device number from BQ27220"));
40 if (device_number != DEVICE_NUMBER) {
41 ESP_LOGE(TAG,
"Unexpected device number 0x%04X (expected 0x%04X)", device_number, DEVICE_NUMBER);
42 this->
mark_failed(LOG_STR(
"Unexpected device number"));
46void BQ27220Component::update() {
48 this->
publish_(this->voltage_sensor_, REG_VOLTAGE, ok, [](uint16_t
raw) {
return raw * 0.001f; });
49 this->
publish_(this->current_sensor_, REG_CURRENT, ok,
50 [](uint16_t
raw) {
return static_cast<int16_t
>(
raw) * 0.001f; });
51 this->
publish_(this->battery_level_sensor_, REG_STATE_OF_CHARGE, ok, [](uint16_t
raw) {
return float(
raw); });
52 this->
publish_(this->temperature_sensor_, REG_TEMPERATURE, ok, [](uint16_t
raw) {
return raw * 0.1f - 273.15f; });
53 this->
publish_(this->remaining_capacity_sensor_, REG_REMAINING_CAPACITY, ok, [](uint16_t
raw) {
return float(
raw); });
54 this->
publish_(this->full_charge_capacity_sensor_, REG_FULL_CHARGE_CAPACITY, ok,
56 this->
publish_(this->time_to_empty_sensor_, REG_TIME_TO_EMPTY, ok,
57 [](uint16_t
raw) {
return raw == TIME_TO_EMPTY_NOT_APPLICABLE ? NAN :
float(
raw); });
58 this->
publish_(this->state_of_health_sensor_, REG_STATE_OF_HEALTH, ok, [](uint16_t
raw) {
return float(
raw); });
60 this->
status_comm_result(ok, LOG_STR(
"Failed to read one or more registers from BQ27220"));
63void BQ27220Component::dump_config() {
64 ESP_LOGCONFIG(TAG,
"BQ27220 Battery Fuel Gauge:");
66 LOG_UPDATE_INTERVAL(
this);
67 LOG_SENSOR(
" ",
"Voltage", this->voltage_sensor_);
68 LOG_SENSOR(
" ",
"Current", this->current_sensor_);
69 LOG_SENSOR(
" ",
"Battery Level", this->battery_level_sensor_);
70 LOG_SENSOR(
" ",
"Temperature", this->temperature_sensor_);
71 LOG_SENSOR(
" ",
"Remaining Capacity", this->remaining_capacity_sensor_);
72 LOG_SENSOR(
" ",
"Full Charge Capacity", this->full_charge_capacity_sensor_);
73 LOG_SENSOR(
" ",
"Time To Empty", this->time_to_empty_sensor_);
74 LOG_SENSOR(
" ",
"State Of Health", this->state_of_health_sensor_);