9static constexpr uint32_t STATUS_CHECK_TIMEOUT_ID = 0;
58 float voltage_full_scale = 0.25;
83 ESP_LOGE(TAG,
"CS5460A reset failed!");
89 ESP_LOGCONFIG(TAG,
" Version: %" PRIx32, (
status >> 6) & 7);
100 ESP_LOGCONFIG(TAG,
" Init ok");
137 time_left -= time_left / 5;
139 if (time_left > -500) {
150 if (time_left > -15000) {
154 ESP_LOGCONFIG(TAG,
"Device officially stuck, resetting");
161 this->
set_timeout(STATUS_CHECK_TIMEOUT_ID, time_left, [
this]() {
171 if (!(status & 0xcbf83c)) {
182 ESP_LOGE(TAG,
"Low supply detected");
186 if (status & (1 << 3)) {
188 ESP_LOGE(TAG,
"Modulator oscillation on current channel");
192 if (status & (1 << 4)) {
194 ESP_LOGE(TAG,
"Modulator oscillation on voltage channel");
198 if (status & (1 << 5)) {
200 ESP_LOGE(TAG,
"Watch-dog timeout");
204 if (status & (1 << 11)) {
206 ESP_LOGE(TAG,
"EOUT Energy Accumulation Register out of range");
210 if (status & (1 << 12)) {
212 ESP_LOGE(TAG,
"Energy out of range");
216 if (status & (1 << 13)) {
218 ESP_LOGE(TAG,
"RMS voltage out of range");
222 if (status & (1 << 14)) {
224 ESP_LOGE(TAG,
"RMS current out of range");
228 if (status & (1 << 15)) {
230 ESP_LOGE(TAG,
"Power calculation out of range");
234 if (status & (1 << 16)) {
236 ESP_LOGE(TAG,
"Voltage out of range");
240 if (status & (1 << 17)) {
242 ESP_LOGE(TAG,
"Current out of range");
246 if (status & (1 << 19)) {
248 ESP_LOGE(TAG,
"Divide overflowed");
252 bool dir =
status & (1 << 21);
255 ESP_LOGI(TAG,
"Energy counter %s pulse", dir ? LOG_STR_LITERAL(
"negative") : LOG_STR_LITERAL(
"positive"));
273 if (status & ((1 << 23) | (1 << 5))) {
296 if (status & (1 << 23)) {
326 ? LOG_STR_LITERAL(
"OK")
328 LOG_PIN(
" CS Pin: ",
cs_);
330 " Samples / cycle: %" PRIu32
"\n"
331 " Phase offset: %i\n"
333 " Current gain: %.5f\n"
334 " Voltage gain: %.5f\n"
337 " Pulse energy: %.2f Wh",
void mark_failed()
Mark this component as failed.
uint8_t get_component_state() const
void status_momentary_warning(uint32_t length=5000)
Set warning status flag and clear it after length ms.
bool cancel_timeout(const char *name)
Cancel a timeout function.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
float current_multiplier_
void dump_config() override
uint32_t prev_raw_energy_
void write_register_(enum CS5460ARegister addr, uint32_t value)
sensor::Sensor * voltage_sensor_
uint32_t read_register_(uint8_t addr)
void schedule_next_check_()
float voltage_multiplier_
sensor::Sensor * power_sensor_
uint32_t prev_raw_current_
sensor::Sensor * current_sensor_
void publish_state(float state)
Publish a new state to the front-end.
void spi_setup() override
void write_byte(uint8_t data)
uint8_t transfer_byte(uint8_t data)
ESPHOME_LOG_TAG(TAG, "cs5460a")
constexpr uint8_t COMPONENT_STATE_FAILED
constexpr uint8_t COMPONENT_STATE_LOOP
constexpr int32_t sign_extend(uint32_t value)
Sign-extend the low BITS bits of value, e.g. sign_extend<24>(0xFFFFFE) == -2. Higher bits are ignored...
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()