ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
bq27220.h
Go to the documentation of this file.
1#pragma once
2
3// Datasheet: https://www.ti.com/lit/ds/symlink/bq27220.pdf
4// Technical Reference Manual: https://www.ti.com/lit/ug/sluubd4a/sluubd4a.pdf
5
9
10namespace esphome::bq27220 {
11
12class BQ27220Component final : public PollingComponent, public i2c::I2CDevice {
13 SUB_SENSOR(voltage)
14 SUB_SENSOR(current)
15 SUB_SENSOR(battery_level)
16 SUB_SENSOR(temperature)
17 SUB_SENSOR(remaining_capacity)
18 SUB_SENSOR(full_charge_capacity)
19 SUB_SENSOR(time_to_empty)
20 SUB_SENSOR(state_of_health)
21
22 public:
23 void setup() override;
24 void update() override;
25 void dump_config() override;
26
27 protected:
28 bool read_word_(uint8_t reg, uint16_t &value);
31 template<typename F> void publish_(sensor::Sensor *sensor, uint8_t reg, bool &ok, F convert) {
32 if (sensor == nullptr)
33 return;
34 uint16_t raw;
35 if (!this->read_word_(reg, raw)) {
36 ok = false;
37 sensor->publish_state(NAN);
38 return;
39 }
40 sensor->publish_state(convert(raw));
41 }
42};
43
44} // namespace esphome::bq27220
uint8_t raw[35]
Definition bl0939.h:0
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
This class simplifies creating components that periodically check a state.
Definition component.h:514
bool read_word_(uint8_t reg, uint16_t &value)
Definition bq27220.cpp:28
void publish_(sensor::Sensor *sensor, uint8_t reg, bool &ok, F convert)
Read a standard command register and publish it through the sensor, if one is configured.
Definition bq27220.h:31
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:132
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:152
Base-class for all sensors.
Definition sensor.h:47
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
uint16_t temperature
Definition sun_gtil2.cpp:12