ESPHome  2024.12.0
bl0906.h
Go to the documentation of this file.
1 #pragma once
2 
8 
9 // https://www.belling.com.cn/media/file_object/bel_product/BL0906/datasheet/BL0906_V1.02_cn.pdf
10 // https://www.belling.com.cn/media/file_object/bel_product/BL0906/guide/BL0906%20APP%20Note_V1.02.pdf
11 
12 namespace esphome {
13 namespace bl0906 {
14 
15 struct DataPacket { // NOLINT(altera-struct-pack-align)
16  uint8_t l{0};
17  uint8_t m{0};
18  uint8_t h{0};
19  uint8_t checksum; // checksum
20  uint8_t address;
21 } __attribute__((packed));
22 
23 struct ube24_t { // NOLINT(readability-identifier-naming,altera-struct-pack-align)
24  uint8_t l{0};
25  uint8_t m{0};
26  uint8_t h{0};
27 } __attribute__((packed));
28 
29 struct sbe24_t { // NOLINT(readability-identifier-naming,altera-struct-pack-align)
30  uint8_t l{0};
31  uint8_t m{0};
32  int8_t h{0};
33 } __attribute__((packed));
34 
35 template<typename... Ts> class ResetEnergyAction;
36 
37 class BL0906;
38 
39 using ActionCallbackFuncPtr = void (BL0906::*)();
40 
41 class BL0906 : public PollingComponent, public uart::UARTDevice {
42  SUB_SENSOR(voltage)
43  SUB_SENSOR(current_1)
44  SUB_SENSOR(current_2)
45  SUB_SENSOR(current_3)
46  SUB_SENSOR(current_4)
47  SUB_SENSOR(current_5)
48  SUB_SENSOR(current_6)
49  SUB_SENSOR(power_1)
50  SUB_SENSOR(power_2)
51  SUB_SENSOR(power_3)
52  SUB_SENSOR(power_4)
53  SUB_SENSOR(power_5)
54  SUB_SENSOR(power_6)
55  SUB_SENSOR(total_power)
56  SUB_SENSOR(energy_1)
57  SUB_SENSOR(energy_2)
58  SUB_SENSOR(energy_3)
59  SUB_SENSOR(energy_4)
60  SUB_SENSOR(energy_5)
61  SUB_SENSOR(energy_6)
62  SUB_SENSOR(total_energy)
63  SUB_SENSOR(frequency)
64  SUB_SENSOR(temperature)
65 
66  public:
67  void loop() override;
68 
69  void update() override;
70  void setup() override;
71  void dump_config() override;
72 
73  protected:
74  template<typename... Ts> friend class ResetEnergyAction;
75 
76  void reset_energy_();
77 
78  void read_data_(uint8_t address, float reference, sensor::Sensor *sensor);
79 
80  void bias_correction_(uint8_t address, float measurements, float correction);
81 
82  uint8_t current_channel_{0};
83  size_t enqueue_action_(ActionCallbackFuncPtr function);
84  void handle_actions_();
85 
86  private:
87  std::vector<ActionCallbackFuncPtr> action_queue_{};
88 };
89 
90 template<typename... Ts> class ResetEnergyAction : public Action<Ts...>, public Parented<BL0906> {
91  public:
92  void play(Ts... x) override { this->parent_->enqueue_action_(&BL0906::reset_energy_); }
93 };
94 
95 } // namespace bl0906
96 } // namespace esphome
void setup()
void loop()
uint16_t x
Definition: tt21100.cpp:17
This class simplifies creating components that periodically check a state.
Definition: component.h:283
uint16_t temperature
Definition: sun_gtil2.cpp:26
uint16_le_t frequency
Definition: bl0942.h:72
esphome::bl0906::BL0906 __attribute__
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void(BL0906::*)() ActionCallbackFuncPtr
Definition: bl0906.h:39
Base-class for all sensors.
Definition: sensor.h:57
esphome::sensor::Sensor * sensor
Definition: statsd.h:38
void play(Ts... x) override
Definition: bl0906.h:92
Helper class to easily give an object a parent of type T.
Definition: helpers.h:530