diff --git a/esphome/components/bang_bang/bang_bang_climate.cpp b/esphome/components/bang_bang/bang_bang_climate.cpp index 5d8c0eb7b7..f26377a38a 100644 --- a/esphome/components/bang_bang/bang_bang_climate.cpp +++ b/esphome/components/bang_bang/bang_bang_climate.cpp @@ -6,6 +6,9 @@ namespace bang_bang { static const char *const TAG = "bang_bang.climate"; +BangBangClimate::BangBangClimate() + : idle_trigger_(new Trigger<>()), cool_trigger_(new Trigger<>()), heat_trigger_(new Trigger<>()) {} + void BangBangClimate::setup() { this->sensor_->add_on_state_callback([this](float state) { this->current_temperature = state; @@ -31,54 +34,63 @@ void BangBangClimate::setup() { restore->to_call(this).perform(); } else { // restore from defaults, change_away handles those for us - if (supports_cool_ && supports_heat_) { + if (this->supports_cool_ && this->supports_heat_) { this->mode = climate::CLIMATE_MODE_HEAT_COOL; - } else if (supports_cool_) { + } else if (this->supports_cool_) { this->mode = climate::CLIMATE_MODE_COOL; - } else if (supports_heat_) { + } else if (this->supports_heat_) { this->mode = climate::CLIMATE_MODE_HEAT; } this->change_away_(false); } } + void BangBangClimate::control(const climate::ClimateCall &call) { - if (call.get_mode().has_value()) + if (call.get_mode().has_value()) { this->mode = *call.get_mode(); - if (call.get_target_temperature_low().has_value()) + } + if (call.get_target_temperature_low().has_value()) { this->target_temperature_low = *call.get_target_temperature_low(); - if (call.get_target_temperature_high().has_value()) + } + if (call.get_target_temperature_high().has_value()) { this->target_temperature_high = *call.get_target_temperature_high(); - if (call.get_preset().has_value()) + } + if (call.get_preset().has_value()) { this->change_away_(*call.get_preset() == climate::CLIMATE_PRESET_AWAY); + } this->compute_state_(); this->publish_state(); } + climate::ClimateTraits BangBangClimate::traits() { auto traits = climate::ClimateTraits(); traits.add_feature_flags(climate::CLIMATE_SUPPORTS_CURRENT_TEMPERATURE | climate::CLIMATE_REQUIRES_TWO_POINT_TARGET_TEMPERATURE | climate::CLIMATE_SUPPORTS_ACTION); - - if (this->humidity_sensor_ != nullptr) + if (this->humidity_sensor_ != nullptr) { traits.add_feature_flags(climate::CLIMATE_SUPPORTS_CURRENT_HUMIDITY); - - traits.set_supported_modes({climate::CLIMATE_MODE_OFF}); - if (supports_cool_) + } + traits.set_supported_modes({ + climate::CLIMATE_MODE_OFF, + }); + if (this->supports_cool_) { traits.add_supported_mode(climate::CLIMATE_MODE_COOL); - if (supports_heat_) + } + if (this->supports_heat_) { traits.add_supported_mode(climate::CLIMATE_MODE_HEAT); - if (supports_cool_ && supports_heat_) + } + if (this->supports_cool_ && this->supports_heat_) { traits.add_supported_mode(climate::CLIMATE_MODE_HEAT_COOL); - - if (supports_away_) { + } + if (this->supports_away_) { traits.set_supported_presets({ climate::CLIMATE_PRESET_HOME, climate::CLIMATE_PRESET_AWAY, }); } - return traits; } + void BangBangClimate::compute_state_() { if (this->mode == climate::CLIMATE_MODE_OFF) { this->switch_to_action_(climate::CLIMATE_ACTION_OFF); @@ -123,6 +135,7 @@ void BangBangClimate::compute_state_() { this->switch_to_action_(target_action); } + void BangBangClimate::switch_to_action_(climate::ClimateAction action) { if (action == this->action) { // already in target mode @@ -167,6 +180,7 @@ void BangBangClimate::switch_to_action_(climate::ClimateAction action) { this->prev_trigger_ = trig; this->publish_state(); } + void BangBangClimate::change_away_(bool away) { if (!away) { this->target_temperature_low = this->normal_config_.default_temperature_low; @@ -177,22 +191,26 @@ void BangBangClimate::change_away_(bool away) { } this->preset = away ? climate::CLIMATE_PRESET_AWAY : climate::CLIMATE_PRESET_HOME; } + void BangBangClimate::set_normal_config(const BangBangClimateTargetTempConfig &normal_config) { this->normal_config_ = normal_config; } + void BangBangClimate::set_away_config(const BangBangClimateTargetTempConfig &away_config) { this->supports_away_ = true; this->away_config_ = away_config; } -BangBangClimate::BangBangClimate() - : idle_trigger_(new Trigger<>()), cool_trigger_(new Trigger<>()), heat_trigger_(new Trigger<>()) {} + void BangBangClimate::set_sensor(sensor::Sensor *sensor) { this->sensor_ = sensor; } void BangBangClimate::set_humidity_sensor(sensor::Sensor *humidity_sensor) { this->humidity_sensor_ = humidity_sensor; } + Trigger<> *BangBangClimate::get_idle_trigger() const { return this->idle_trigger_; } Trigger<> *BangBangClimate::get_cool_trigger() const { return this->cool_trigger_; } -void BangBangClimate::set_supports_cool(bool supports_cool) { this->supports_cool_ = supports_cool; } Trigger<> *BangBangClimate::get_heat_trigger() const { return this->heat_trigger_; } + +void BangBangClimate::set_supports_cool(bool supports_cool) { this->supports_cool_ = supports_cool; } void BangBangClimate::set_supports_heat(bool supports_heat) { this->supports_heat_ = supports_heat; } + void BangBangClimate::dump_config() { LOG_CLIMATE("", "Bang Bang Climate", this); ESP_LOGCONFIG(TAG, diff --git a/esphome/components/bang_bang/bang_bang_climate.h b/esphome/components/bang_bang/bang_bang_climate.h index 96368af34c..2e7da93a07 100644 --- a/esphome/components/bang_bang/bang_bang_climate.h +++ b/esphome/components/bang_bang/bang_bang_climate.h @@ -25,14 +25,15 @@ class BangBangClimate : public climate::Climate, public Component { void set_sensor(sensor::Sensor *sensor); void set_humidity_sensor(sensor::Sensor *humidity_sensor); - Trigger<> *get_idle_trigger() const; - Trigger<> *get_cool_trigger() const; void set_supports_cool(bool supports_cool); - Trigger<> *get_heat_trigger() const; void set_supports_heat(bool supports_heat); void set_normal_config(const BangBangClimateTargetTempConfig &normal_config); void set_away_config(const BangBangClimateTargetTempConfig &away_config); + Trigger<> *get_idle_trigger() const; + Trigger<> *get_cool_trigger() const; + Trigger<> *get_heat_trigger() const; + protected: /// Override control to change settings of the climate device. void control(const climate::ClimateCall &call) override; @@ -56,16 +57,10 @@ class BangBangClimate : public climate::Climate, public Component { * * In idle mode, the controller is assumed to have both heating and cooling disabled. */ - Trigger<> *idle_trigger_; + Trigger<> *idle_trigger_{nullptr}; /** The trigger to call when the controller should switch to cooling mode. */ - Trigger<> *cool_trigger_; - /** Whether the controller supports cooling. - * - * A false value for this attribute means that the controller has no cooling action - * (for example a thermostat, where only heating and not-heating is possible). - */ - bool supports_cool_{false}; + Trigger<> *cool_trigger_{nullptr}; /** The trigger to call when the controller should switch to heating mode. * * A null value for this attribute means that the controller has no heating action @@ -73,15 +68,23 @@ class BangBangClimate : public climate::Climate, public Component { * (blinds open) is possible. */ Trigger<> *heat_trigger_{nullptr}; - bool supports_heat_{false}; /** A reference to the trigger that was previously active. * * This is so that the previous trigger can be stopped before enabling a new one. */ Trigger<> *prev_trigger_{nullptr}; - BangBangClimateTargetTempConfig normal_config_{}; + /** Whether the controller supports cooling/heating + * + * A false value for this attribute means that the controller has no respective action + * (for example a thermostat, where only heating and not-heating is possible). + */ + bool supports_cool_{false}; + bool supports_heat_{false}; + bool supports_away_{false}; + + BangBangClimateTargetTempConfig normal_config_{}; BangBangClimateTargetTempConfig away_config_{}; };