ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
hlw8012.h
Go to the documentation of this file.
1#pragma once
2
4#include "esphome/core/hal.h"
7
8#include <cinttypes>
9
10namespace esphome::hlw8012 {
11
13
19
20#ifdef HAS_PCNT
21#define USE_PCNT true
22#else
23#define USE_PCNT false
24#endif
25
26class HLW8012Component final : public PollingComponent {
27 SUB_SENSOR(voltage)
28 SUB_SENSOR(current)
29 SUB_SENSOR(power)
30 SUB_SENSOR(energy)
31
32 public:
35
36 void setup() override;
37 void dump_config() override;
38 void update() override;
39
40 void set_initial_mode(HLW8012InitialMode initial_mode) {
42 }
43 void set_sensor_model(HLW8012SensorModels sensor_model) { sensor_model_ = sensor_model; }
44 void set_change_mode_every(uint32_t change_mode_every) { change_mode_every_ = change_mode_every; }
45 void set_current_resistor(float current_resistor) { current_resistor_ = current_resistor; }
46 void set_voltage_divider(float voltage_divider) { voltage_divider_ = voltage_divider; }
47 void set_sel_pin(GPIOPin *sel_pin) { sel_pin_ = sel_pin; }
48 void set_cf_pin(InternalGPIOPin *cf_pin) { cf_pin_ = cf_pin; }
49 void set_cf1_pin(InternalGPIOPin *cf1_pin) { cf1_pin_ = cf1_pin; }
50
51 protected:
53 bool current_mode_{false};
56 float current_resistor_{0.001};
57 float voltage_divider_{2351};
59 uint64_t cf_total_pulses_{0};
65
68 float power_multiplier_{0.0f};
69};
70
71} // namespace esphome::hlw8012
This class simplifies creating components that periodically check a state.
Definition component.h:532
pulse_counter::PulseCounterStorageBase & cf_store_
Definition hlw8012.h:62
pulse_counter::PulseCounterStorageBase & cf1_store_
Definition hlw8012.h:64
HLW8012SensorModels sensor_model_
Definition hlw8012.h:58
@ HLW8012_SENSOR_MODEL_BL0937
Definition hlw8012.h:17
@ HLW8012_SENSOR_MODEL_CSE7759
Definition hlw8012.h:16
@ HLW8012_SENSOR_MODEL_HLW8012
Definition hlw8012.h:15
@ HLW8012_INITIAL_MODE_CURRENT
Definition hlw8012.h:12
@ HLW8012_INITIAL_MODE_VOLTAGE
Definition hlw8012.h:12
PulseCounterStorageBase * get_storage(bool hw_pcnt)
static void uint32_t