mirror of
https://github.com/esphome/esphome.git
synced 2025-10-25 13:13:48 +01:00
[bang_bang] Various clean-up (#11356)
This commit is contained in:
@@ -6,6 +6,9 @@ namespace bang_bang {
|
|||||||
|
|
||||||
static const char *const TAG = "bang_bang.climate";
|
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() {
|
void BangBangClimate::setup() {
|
||||||
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;
|
||||||
@@ -31,54 +34,63 @@ void BangBangClimate::setup() {
|
|||||||
restore->to_call(this).perform();
|
restore->to_call(this).perform();
|
||||||
} else {
|
} else {
|
||||||
// restore from defaults, change_away handles those for us
|
// 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;
|
this->mode = climate::CLIMATE_MODE_HEAT_COOL;
|
||||||
} else if (supports_cool_) {
|
} else if (this->supports_cool_) {
|
||||||
this->mode = climate::CLIMATE_MODE_COOL;
|
this->mode = climate::CLIMATE_MODE_COOL;
|
||||||
} else if (supports_heat_) {
|
} else if (this->supports_heat_) {
|
||||||
this->mode = climate::CLIMATE_MODE_HEAT;
|
this->mode = climate::CLIMATE_MODE_HEAT;
|
||||||
}
|
}
|
||||||
this->change_away_(false);
|
this->change_away_(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BangBangClimate::control(const climate::ClimateCall &call) {
|
void BangBangClimate::control(const climate::ClimateCall &call) {
|
||||||
if (call.get_mode().has_value())
|
if (call.get_mode().has_value()) {
|
||||||
this->mode = *call.get_mode();
|
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();
|
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();
|
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->change_away_(*call.get_preset() == climate::CLIMATE_PRESET_AWAY);
|
||||||
|
}
|
||||||
|
|
||||||
this->compute_state_();
|
this->compute_state_();
|
||||||
this->publish_state();
|
this->publish_state();
|
||||||
}
|
}
|
||||||
|
|
||||||
climate::ClimateTraits BangBangClimate::traits() {
|
climate::ClimateTraits BangBangClimate::traits() {
|
||||||
auto traits = climate::ClimateTraits();
|
auto traits = climate::ClimateTraits();
|
||||||
traits.add_feature_flags(climate::CLIMATE_SUPPORTS_CURRENT_TEMPERATURE |
|
traits.add_feature_flags(climate::CLIMATE_SUPPORTS_CURRENT_TEMPERATURE |
|
||||||
climate::CLIMATE_REQUIRES_TWO_POINT_TARGET_TEMPERATURE | climate::CLIMATE_SUPPORTS_ACTION);
|
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.add_feature_flags(climate::CLIMATE_SUPPORTS_CURRENT_HUMIDITY);
|
||||||
|
}
|
||||||
traits.set_supported_modes({climate::CLIMATE_MODE_OFF});
|
traits.set_supported_modes({
|
||||||
if (supports_cool_)
|
climate::CLIMATE_MODE_OFF,
|
||||||
|
});
|
||||||
|
if (this->supports_cool_) {
|
||||||
traits.add_supported_mode(climate::CLIMATE_MODE_COOL);
|
traits.add_supported_mode(climate::CLIMATE_MODE_COOL);
|
||||||
if (supports_heat_)
|
}
|
||||||
|
if (this->supports_heat_) {
|
||||||
traits.add_supported_mode(climate::CLIMATE_MODE_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);
|
traits.add_supported_mode(climate::CLIMATE_MODE_HEAT_COOL);
|
||||||
|
}
|
||||||
if (supports_away_) {
|
if (this->supports_away_) {
|
||||||
traits.set_supported_presets({
|
traits.set_supported_presets({
|
||||||
climate::CLIMATE_PRESET_HOME,
|
climate::CLIMATE_PRESET_HOME,
|
||||||
climate::CLIMATE_PRESET_AWAY,
|
climate::CLIMATE_PRESET_AWAY,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return traits;
|
return traits;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BangBangClimate::compute_state_() {
|
void BangBangClimate::compute_state_() {
|
||||||
if (this->mode == climate::CLIMATE_MODE_OFF) {
|
if (this->mode == climate::CLIMATE_MODE_OFF) {
|
||||||
this->switch_to_action_(climate::CLIMATE_ACTION_OFF);
|
this->switch_to_action_(climate::CLIMATE_ACTION_OFF);
|
||||||
@@ -123,6 +135,7 @@ void BangBangClimate::compute_state_() {
|
|||||||
|
|
||||||
this->switch_to_action_(target_action);
|
this->switch_to_action_(target_action);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BangBangClimate::switch_to_action_(climate::ClimateAction action) {
|
void BangBangClimate::switch_to_action_(climate::ClimateAction action) {
|
||||||
if (action == this->action) {
|
if (action == this->action) {
|
||||||
// already in target mode
|
// already in target mode
|
||||||
@@ -167,6 +180,7 @@ void BangBangClimate::switch_to_action_(climate::ClimateAction action) {
|
|||||||
this->prev_trigger_ = trig;
|
this->prev_trigger_ = trig;
|
||||||
this->publish_state();
|
this->publish_state();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BangBangClimate::change_away_(bool away) {
|
void BangBangClimate::change_away_(bool away) {
|
||||||
if (!away) {
|
if (!away) {
|
||||||
this->target_temperature_low = this->normal_config_.default_temperature_low;
|
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;
|
this->preset = away ? climate::CLIMATE_PRESET_AWAY : climate::CLIMATE_PRESET_HOME;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BangBangClimate::set_normal_config(const BangBangClimateTargetTempConfig &normal_config) {
|
void BangBangClimate::set_normal_config(const BangBangClimateTargetTempConfig &normal_config) {
|
||||||
this->normal_config_ = normal_config;
|
this->normal_config_ = normal_config;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BangBangClimate::set_away_config(const BangBangClimateTargetTempConfig &away_config) {
|
void BangBangClimate::set_away_config(const BangBangClimateTargetTempConfig &away_config) {
|
||||||
this->supports_away_ = true;
|
this->supports_away_ = true;
|
||||||
this->away_config_ = away_config;
|
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_sensor(sensor::Sensor *sensor) { this->sensor_ = sensor; }
|
||||||
void BangBangClimate::set_humidity_sensor(sensor::Sensor *humidity_sensor) { this->humidity_sensor_ = humidity_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_idle_trigger() const { return this->idle_trigger_; }
|
||||||
Trigger<> *BangBangClimate::get_cool_trigger() const { return this->cool_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_; }
|
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::set_supports_heat(bool supports_heat) { this->supports_heat_ = supports_heat; }
|
||||||
|
|
||||||
void BangBangClimate::dump_config() {
|
void BangBangClimate::dump_config() {
|
||||||
LOG_CLIMATE("", "Bang Bang Climate", this);
|
LOG_CLIMATE("", "Bang Bang Climate", this);
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG,
|
||||||
|
|||||||
@@ -25,14 +25,15 @@ class BangBangClimate : public climate::Climate, public Component {
|
|||||||
|
|
||||||
void set_sensor(sensor::Sensor *sensor);
|
void set_sensor(sensor::Sensor *sensor);
|
||||||
void set_humidity_sensor(sensor::Sensor *humidity_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);
|
void set_supports_cool(bool supports_cool);
|
||||||
Trigger<> *get_heat_trigger() const;
|
|
||||||
void set_supports_heat(bool supports_heat);
|
void set_supports_heat(bool supports_heat);
|
||||||
void set_normal_config(const BangBangClimateTargetTempConfig &normal_config);
|
void set_normal_config(const BangBangClimateTargetTempConfig &normal_config);
|
||||||
void set_away_config(const BangBangClimateTargetTempConfig &away_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:
|
protected:
|
||||||
/// Override control to change settings of the climate device.
|
/// Override control to change settings of the climate device.
|
||||||
void control(const climate::ClimateCall &call) override;
|
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.
|
* 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.
|
/** The trigger to call when the controller should switch to cooling mode.
|
||||||
*/
|
*/
|
||||||
Trigger<> *cool_trigger_;
|
Trigger<> *cool_trigger_{nullptr};
|
||||||
/** 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};
|
|
||||||
/** The trigger to call when the controller should switch to heating mode.
|
/** 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
|
* 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.
|
* (blinds open) is possible.
|
||||||
*/
|
*/
|
||||||
Trigger<> *heat_trigger_{nullptr};
|
Trigger<> *heat_trigger_{nullptr};
|
||||||
bool supports_heat_{false};
|
|
||||||
/** A reference to the trigger that was previously active.
|
/** A reference to the trigger that was previously active.
|
||||||
*
|
*
|
||||||
* This is so that the previous trigger can be stopped before enabling a new one.
|
* This is so that the previous trigger can be stopped before enabling a new one.
|
||||||
*/
|
*/
|
||||||
Trigger<> *prev_trigger_{nullptr};
|
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};
|
bool supports_away_{false};
|
||||||
|
|
||||||
|
BangBangClimateTargetTempConfig normal_config_{};
|
||||||
BangBangClimateTargetTempConfig away_config_{};
|
BangBangClimateTargetTempConfig away_config_{};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user