Thermostat publish state fix (#2427)

This commit is contained in:
Keith Burzinski 2021-10-03 07:10:43 -05:00 committed by GitHub
parent a7687c3e17
commit eaa5200a35
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 16 deletions

View file

@ -18,8 +18,8 @@ void ThermostatClimate::setup() {
// add a callback so that whenever the sensor state changes we can take action // add a callback so that whenever the sensor state changes we can take action
this->sensor_->add_on_state_callback([this](float state) { this->sensor_->add_on_state_callback([this](float state) {
this->current_temperature = state; this->current_temperature = state;
// required action may have changed, recompute, refresh // required action may have changed, recompute, refresh, we'll publish_state() later
this->switch_to_action_(this->compute_action_()); this->switch_to_action_(this->compute_action_(), false);
this->switch_to_supplemental_action_(this->compute_supplemental_action_()); this->switch_to_supplemental_action_(this->compute_supplemental_action_());
// current temperature and possibly action changed, so publish the new state // current temperature and possibly action changed, so publish the new state
this->publish_state(); this->publish_state();
@ -34,8 +34,8 @@ void ThermostatClimate::setup() {
this->mode = this->default_mode_; this->mode = this->default_mode_;
this->change_away_(false); this->change_away_(false);
} }
// refresh the climate action based on the restored settings // refresh the climate action based on the restored settings, we'll publish_state() later
this->switch_to_action_(this->compute_action_()); this->switch_to_action_(this->compute_action_(), false);
this->switch_to_supplemental_action_(this->compute_supplemental_action_()); this->switch_to_supplemental_action_(this->compute_supplemental_action_());
this->setup_complete_ = true; this->setup_complete_ = true;
this->publish_state(); this->publish_state();
@ -47,11 +47,11 @@ float ThermostatClimate::heat_deadband() { return this->heating_deadband_; }
float ThermostatClimate::heat_overrun() { return this->heating_overrun_; } float ThermostatClimate::heat_overrun() { return this->heating_overrun_; }
void ThermostatClimate::refresh() { void ThermostatClimate::refresh() {
this->switch_to_mode_(this->mode); this->switch_to_mode_(this->mode, false);
this->switch_to_action_(this->compute_action_()); this->switch_to_action_(this->compute_action_(), false);
this->switch_to_supplemental_action_(this->compute_supplemental_action_()); this->switch_to_supplemental_action_(this->compute_supplemental_action_());
this->switch_to_fan_mode_(this->fan_mode.value()); this->switch_to_fan_mode_(this->fan_mode.value(), false);
this->switch_to_swing_mode_(this->swing_mode); this->switch_to_swing_mode_(this->swing_mode, false);
this->check_temperature_change_trigger_(); this->check_temperature_change_trigger_();
this->publish_state(); this->publish_state();
} }
@ -346,7 +346,7 @@ climate::ClimateAction ThermostatClimate::compute_supplemental_action_() {
return target_action; return target_action;
} }
void ThermostatClimate::switch_to_action_(climate::ClimateAction action) { void ThermostatClimate::switch_to_action_(climate::ClimateAction action, bool publish_state) {
// setup_complete_ helps us ensure an action is called immediately after boot // setup_complete_ helps us ensure an action is called immediately after boot
if ((action == this->action) && this->setup_complete_) if ((action == this->action) && this->setup_complete_)
// already in target mode // already in target mode
@ -358,6 +358,8 @@ void ThermostatClimate::switch_to_action_(climate::ClimateAction action) {
// switching from OFF to IDLE or vice-versa -- this is only a visual difference. // switching from OFF to IDLE or vice-versa -- this is only a visual difference.
// OFF means user manually disabled, IDLE means the temperature is in target range. // OFF means user manually disabled, IDLE means the temperature is in target range.
this->action = action; this->action = action;
if (publish_state)
this->publish_state();
return; return;
} }
@ -452,6 +454,8 @@ void ThermostatClimate::switch_to_action_(climate::ClimateAction action) {
ESP_LOGVV(TAG, "Calling FAN_ONLY action with HEATING/COOLING action"); ESP_LOGVV(TAG, "Calling FAN_ONLY action with HEATING/COOLING action");
trig_fan->trigger(); trig_fan->trigger();
} }
if (publish_state)
this->publish_state();
} }
} }
@ -509,13 +513,15 @@ void ThermostatClimate::trigger_supplemental_action_() {
} }
} }
void ThermostatClimate::switch_to_fan_mode_(climate::ClimateFanMode fan_mode) { void ThermostatClimate::switch_to_fan_mode_(climate::ClimateFanMode fan_mode, bool publish_state) {
// setup_complete_ helps us ensure an action is called immediately after boot // setup_complete_ helps us ensure an action is called immediately after boot
if ((fan_mode == this->prev_fan_mode_) && this->setup_complete_) if ((fan_mode == this->prev_fan_mode_) && this->setup_complete_)
// already in target mode // already in target mode
return; return;
this->fan_mode = fan_mode; this->fan_mode = fan_mode;
if (publish_state)
this->publish_state();
if (this->fan_mode_ready_()) { if (this->fan_mode_ready_()) {
Trigger<> *trig = this->fan_mode_auto_trigger_; Trigger<> *trig = this->fan_mode_auto_trigger_;
@ -574,7 +580,7 @@ void ThermostatClimate::switch_to_fan_mode_(climate::ClimateFanMode fan_mode) {
} }
} }
void ThermostatClimate::switch_to_mode_(climate::ClimateMode mode) { void ThermostatClimate::switch_to_mode_(climate::ClimateMode mode, bool publish_state) {
// setup_complete_ helps us ensure an action is called immediately after boot // setup_complete_ helps us ensure an action is called immediately after boot
if ((mode == this->prev_mode_) && this->setup_complete_) if ((mode == this->prev_mode_) && this->setup_complete_)
// already in target mode // already in target mode
@ -615,9 +621,11 @@ void ThermostatClimate::switch_to_mode_(climate::ClimateMode mode) {
this->mode = mode; this->mode = mode;
this->prev_mode_ = mode; this->prev_mode_ = mode;
this->prev_mode_trigger_ = trig; this->prev_mode_trigger_ = trig;
if (publish_state)
this->publish_state();
} }
void ThermostatClimate::switch_to_swing_mode_(climate::ClimateSwingMode swing_mode) { void ThermostatClimate::switch_to_swing_mode_(climate::ClimateSwingMode swing_mode, bool publish_state) {
// setup_complete_ helps us ensure an action is called immediately after boot // setup_complete_ helps us ensure an action is called immediately after boot
if ((swing_mode == this->prev_swing_mode_) && this->setup_complete_) if ((swing_mode == this->prev_swing_mode_) && this->setup_complete_)
// already in target mode // already in target mode
@ -652,6 +660,8 @@ void ThermostatClimate::switch_to_swing_mode_(climate::ClimateSwingMode swing_mo
this->swing_mode = swing_mode; this->swing_mode = swing_mode;
this->prev_swing_mode_ = swing_mode; this->prev_swing_mode_ = swing_mode;
this->prev_swing_mode_trigger_ = trig; this->prev_swing_mode_trigger_ = trig;
if (publish_state)
this->publish_state();
} }
bool ThermostatClimate::idle_action_ready_() { bool ThermostatClimate::idle_action_ready_() {

View file

@ -160,18 +160,18 @@ class ThermostatClimate : public climate::Climate, public Component {
climate::ClimateAction compute_supplemental_action_(); climate::ClimateAction compute_supplemental_action_();
/// Switch the climate device to the given climate action. /// Switch the climate device to the given climate action.
void switch_to_action_(climate::ClimateAction action); void switch_to_action_(climate::ClimateAction action, bool publish_state = true);
void switch_to_supplemental_action_(climate::ClimateAction action); void switch_to_supplemental_action_(climate::ClimateAction action);
void trigger_supplemental_action_(); void trigger_supplemental_action_();
/// Switch the climate device to the given climate fan mode. /// Switch the climate device to the given climate fan mode.
void switch_to_fan_mode_(climate::ClimateFanMode fan_mode); void switch_to_fan_mode_(climate::ClimateFanMode fan_mode, bool publish_state = true);
/// Switch the climate device to the given climate mode. /// Switch the climate device to the given climate mode.
void switch_to_mode_(climate::ClimateMode mode); void switch_to_mode_(climate::ClimateMode mode, bool publish_state = true);
/// Switch the climate device to the given climate swing mode. /// Switch the climate device to the given climate swing mode.
void switch_to_swing_mode_(climate::ClimateSwingMode swing_mode); void switch_to_swing_mode_(climate::ClimateSwingMode swing_mode, bool publish_state = true);
/// Check if the temperature change trigger should be called. /// Check if the temperature change trigger should be called.
void check_temperature_change_trigger_(); void check_temperature_change_trigger_();