diff --git a/components/soyosource_virtual_meter/soyosource_virtual_meter.cpp b/components/soyosource_virtual_meter/soyosource_virtual_meter.cpp index 8e34362..783bf6a 100644 --- a/components/soyosource_virtual_meter/soyosource_virtual_meter.cpp +++ b/components/soyosource_virtual_meter/soyosource_virtual_meter.cpp @@ -23,20 +23,6 @@ void SoyosourceVirtualMeter::setup() { }); } -void SoyosourceVirtualMeter::publish_state_(sensor::Sensor *sensor, float value) { - if (sensor == nullptr) - return; - - sensor->publish_state(value); -} - -void SoyosourceVirtualMeter::publish_state_(text_sensor::TextSensor *text_sensor, const std::string &state) { - if (text_sensor == nullptr) - return; - - text_sensor->publish_state(state); -} - void SoyosourceVirtualMeter::dump_config() { ESP_LOGCONFIG(TAG, "SoyosourceVirtualMeter:"); ESP_LOGCONFIG(TAG, " Address: 0x%02X", this->address_); @@ -56,14 +42,14 @@ void SoyosourceVirtualMeter::update() { } // else default = 0 } else { // Automatic mode - if (millis() - this->last_power_demand_received_ < (this->power_sensor_inactivity_timeout_s_ * 1000)) { - power_demand = (uint16_t) this->power_demand_; - operation_mode = "Auto"; - } else { + if (this->inactivity_timeout_()) { power_demand = 0; operation_mode = "Inactivity timeout"; ESP_LOGW(TAG, "'%s': No power sensor update received since %d seconds. Shutting down for safety reasons.", this->get_modbus_name(), this->power_sensor_inactivity_timeout_s_); + } else { + power_demand = (uint16_t) this->power_demand_; + operation_mode = "Auto"; } } @@ -161,5 +147,27 @@ int16_t SoyosourceVirtualMeter::calculate_power_demand_oem_(int16_t consumption) return 0; } +bool SoyosourceVirtualMeter::inactivity_timeout_() { + if (this->power_sensor_inactivity_timeout_s_ == 0) { + return false; + } + + return millis() - this->last_power_demand_received_ > (this->power_sensor_inactivity_timeout_s_ * 1000); +} + +void SoyosourceVirtualMeter::publish_state_(sensor::Sensor *sensor, float value) { + if (sensor == nullptr) + return; + + sensor->publish_state(value); +} + +void SoyosourceVirtualMeter::publish_state_(text_sensor::TextSensor *text_sensor, const std::string &state) { + if (text_sensor == nullptr) + return; + + text_sensor->publish_state(state); +} + } // namespace soyosource_virtual_meter } // namespace esphome diff --git a/components/soyosource_virtual_meter/soyosource_virtual_meter.h b/components/soyosource_virtual_meter/soyosource_virtual_meter.h index 10b69eb..4508754 100644 --- a/components/soyosource_virtual_meter/soyosource_virtual_meter.h +++ b/components/soyosource_virtual_meter/soyosource_virtual_meter.h @@ -85,6 +85,7 @@ class SoyosourceVirtualMeter : public PollingComponent, public soyosource_modbus void publish_state_(sensor::Sensor *sensor, float value); void publish_state_(text_sensor::TextSensor *text_sensor, const std::string &state); + bool inactivity_timeout_(); int16_t calculate_power_demand_(int16_t consumption, uint16_t last_power_demand); int16_t calculate_power_demand_negative_measurements_(int16_t consumption, uint16_t last_power_demand);