Skip to content

Commit

Permalink
Disable the inactivity_timeout if set to 0s
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi committed Jun 12, 2022
1 parent 265401a commit 9905de0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
44 changes: 26 additions & 18 deletions components/soyosource_virtual_meter/soyosource_virtual_meter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
Expand All @@ -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";
}
}

Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9905de0

Please sign in to comment.