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;
38 const uint8_t device_number_cmd[2] = {0x01, 0x00};
40 this->
mark_failed(LOG_STR(
"Failed to communicate with BQ27220"));
44 uint16_t device_number = 0;
45 if (!this->
read_word_(REG_MAC_DATA, device_number)) {
46 this->
mark_failed(LOG_STR(
"Failed to read device number from BQ27220"));
49 if (device_number != DEVICE_NUMBER) {
50 ESP_LOGE(TAG,
"Unexpected device number 0x%04X (expected 0x%04X)", device_number, DEVICE_NUMBER);
51 this->
mark_failed(LOG_STR(
"Unexpected device number"));
55void BQ27220Component::update() {
57 this->
publish_(this->voltage_sensor_, REG_VOLTAGE, ok, [](uint16_t
raw) {
return raw * 0.001f; });
58 this->
publish_(this->current_sensor_, REG_CURRENT, ok,
59 [](uint16_t
raw) {
return static_cast<int16_t
>(
raw) * 0.001f; });
60 this->
publish_(this->battery_level_sensor_, REG_STATE_OF_CHARGE, ok, [](uint16_t
raw) {
return float(
raw); });
61 this->
publish_(this->temperature_sensor_, REG_TEMPERATURE, ok, [](uint16_t
raw) {
return raw * 0.1f - 273.15f; });
62 this->
publish_(this->remaining_capacity_sensor_, REG_REMAINING_CAPACITY, ok, [](uint16_t
raw) {
return float(
raw); });
63 this->
publish_(this->full_charge_capacity_sensor_, REG_FULL_CHARGE_CAPACITY, ok,
65 this->
publish_(this->time_to_empty_sensor_, REG_TIME_TO_EMPTY, ok,
66 [](uint16_t
raw) {
return raw == TIME_TO_EMPTY_NOT_APPLICABLE ? NAN :
float(
raw); });
67 this->
publish_(this->state_of_health_sensor_, REG_STATE_OF_HEALTH, ok, [](uint16_t
raw) {
return float(
raw); });
72 this->
status_set_warning(LOG_STR(
"Failed to read one or more registers from BQ27220"));
76void BQ27220Component::dump_config() {
77 ESP_LOGCONFIG(TAG,
"BQ27220 Battery Fuel Gauge:");
79 LOG_UPDATE_INTERVAL(
this);
80 LOG_SENSOR(
" ",
"Voltage", this->voltage_sensor_);
81 LOG_SENSOR(
" ",
"Current", this->current_sensor_);
82 LOG_SENSOR(
" ",
"Battery Level", this->battery_level_sensor_);
83 LOG_SENSOR(
" ",
"Temperature", this->temperature_sensor_);
84 LOG_SENSOR(
" ",
"Remaining Capacity", this->remaining_capacity_sensor_);
85 LOG_SENSOR(
" ",
"Full Charge Capacity", this->full_charge_capacity_sensor_);
86 LOG_SENSOR(
" ",
"Time To Empty", this->time_to_empty_sensor_);
87 LOG_SENSOR(
" ",
"State Of Health", this->state_of_health_sensor_);