ESPHome  2024.10.2
bang_bang_climate.cpp
Go to the documentation of this file.
1 #include "bang_bang_climate.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace bang_bang {
6 
7 static const char *const TAG = "bang_bang.climate";
8 
10  this->sensor_->add_on_state_callback([this](float state) {
11  this->current_temperature = state;
12  // control may have changed, recompute
13  this->compute_state_();
14  // current temperature changed, publish state
15  this->publish_state();
16  });
17  this->current_temperature = this->sensor_->state;
18 
19  // register for humidity values and get initial state
20  if (this->humidity_sensor_ != nullptr) {
21  this->humidity_sensor_->add_on_state_callback([this](float state) {
22  this->current_humidity = state;
23  this->publish_state();
24  });
26  }
27 
28  // restore set points
29  auto restore = this->restore_state_();
30  if (restore.has_value()) {
31  restore->to_call(this).perform();
32  } else {
33  // restore from defaults, change_away handles those for us
36  } else if (supports_cool_) {
38  } else if (supports_heat_) {
40  }
41  this->change_away_(false);
42  }
43 }
45  if (call.get_mode().has_value())
46  this->mode = *call.get_mode();
51  if (call.get_preset().has_value())
53 
54  this->compute_state_();
55  this->publish_state();
56 }
60  if (this->humidity_sensor_ != nullptr)
64  });
65  if (supports_cool_)
67  if (supports_heat_)
72  if (supports_away_) {
76  });
77  }
79  return traits;
80 }
82  if (this->mode == climate::CLIMATE_MODE_OFF) {
84  return;
85  }
86  if (std::isnan(this->current_temperature) || std::isnan(this->target_temperature_low) ||
87  std::isnan(this->target_temperature_high)) {
88  // if any control parameters are nan, go to OFF action (not IDLE!)
90  return;
91  }
92  const bool too_cold = this->current_temperature < this->target_temperature_low;
93  const bool too_hot = this->current_temperature > this->target_temperature_high;
94 
95  climate::ClimateAction target_action;
96  if (too_cold) {
97  // too cold -> enable heating if possible and enabled, else idle
98  if (this->supports_heat_ &&
100  target_action = climate::CLIMATE_ACTION_HEATING;
101  } else {
102  target_action = climate::CLIMATE_ACTION_IDLE;
103  }
104  } else if (too_hot) {
105  // too hot -> enable cooling if possible and enabled, else idle
106  if (this->supports_cool_ &&
108  target_action = climate::CLIMATE_ACTION_COOLING;
109  } else {
110  target_action = climate::CLIMATE_ACTION_IDLE;
111  }
112  } else {
113  // neither too hot nor too cold -> in range
114  if (this->supports_cool_ && this->supports_heat_ && this->mode == climate::CLIMATE_MODE_HEAT_COOL) {
115  // if supports both ends and both cooling and heating enabled, go to idle action
116  target_action = climate::CLIMATE_ACTION_IDLE;
117  } else {
118  // else use current mode and don't change (hysteresis)
119  target_action = this->action;
120  }
121  }
122 
123  this->switch_to_action_(target_action);
124 }
126  if (action == this->action) {
127  // already in target mode
128  return;
129  }
130 
131  if ((action == climate::CLIMATE_ACTION_OFF && this->action == climate::CLIMATE_ACTION_IDLE) ||
132  (action == climate::CLIMATE_ACTION_IDLE && this->action == climate::CLIMATE_ACTION_OFF)) {
133  // switching from OFF to IDLE or vice-versa
134  // these only have visual difference. OFF means user manually disabled,
135  // IDLE means it's in auto mode but value is in target range.
136  this->action = action;
137  this->publish_state();
138  return;
139  }
140 
141  if (this->prev_trigger_ != nullptr) {
142  this->prev_trigger_->stop_action();
143  this->prev_trigger_ = nullptr;
144  }
145  Trigger<> *trig;
146  switch (action) {
149  trig = this->idle_trigger_;
150  break;
152  trig = this->cool_trigger_;
153  break;
155  trig = this->heat_trigger_;
156  break;
157  default:
158  trig = nullptr;
159  }
160  if (trig != nullptr) {
161  trig->trigger();
162  } else {
163  ESP_LOGW(TAG, "trig not set - unsupported action");
164  }
165  this->action = action;
166  this->prev_trigger_ = trig;
167  this->publish_state();
168 }
170  if (!away) {
173  } else {
176  }
178 }
180  this->normal_config_ = normal_config;
181 }
183  this->supports_away_ = true;
184  this->away_config_ = away_config;
185 }
187  : idle_trigger_(new Trigger<>()), cool_trigger_(new Trigger<>()), heat_trigger_(new Trigger<>()) {}
189 void BangBangClimate::set_humidity_sensor(sensor::Sensor *humidity_sensor) { this->humidity_sensor_ = humidity_sensor; }
192 void BangBangClimate::set_supports_cool(bool supports_cool) { this->supports_cool_ = supports_cool; }
194 void BangBangClimate::set_supports_heat(bool supports_heat) { this->supports_heat_ = supports_heat; }
196  LOG_CLIMATE("", "Bang Bang Climate", this);
197  ESP_LOGCONFIG(TAG, " Supports HEAT: %s", YESNO(this->supports_heat_));
198  ESP_LOGCONFIG(TAG, " Supports COOL: %s", YESNO(this->supports_cool_));
199  ESP_LOGCONFIG(TAG, " Supports AWAY mode: %s", YESNO(this->supports_away_));
200  ESP_LOGCONFIG(TAG, " Default Target Temperature Low: %.2f°C", this->normal_config_.default_temperature_low);
201  ESP_LOGCONFIG(TAG, " Default Target Temperature High: %.2f°C", this->normal_config_.default_temperature_high);
202 }
203 
206  float default_temperature_high)
207  : default_temperature_low(default_temperature_low), default_temperature_high(default_temperature_high) {}
208 
209 } // namespace bang_bang
210 } // namespace esphome
This class is used to encode all control actions on a climate device.
Definition: climate.h:33
sensor::Sensor * sensor_
The sensor used for getting the current temperature.
The climate device is off (inactive or no power)
Definition: climate_mode.h:33
float current_humidity
The current humidity of the climate device, as reported from the integration.
Definition: climate.h:182
void add_on_state_callback(std::function< void(float)> &&callback)
Add a callback that will be called every time a filtered value arrives.
Definition: sensor.cpp:52
Trigger * prev_trigger_
A reference to the trigger that was previously active.
Trigger * heat_trigger_
The trigger to call when the controller should switch to heating mode.
Device is in home preset.
Definition: climate_mode.h:86
void set_normal_config(const BangBangClimateTargetTempConfig &normal_config)
const optional< ClimateMode > & get_mode() const
Definition: climate.cpp:273
void set_supports_current_humidity(bool supports_current_humidity)
This class contains all static data for climate devices.
The climate device is set to heat to reach the target temperature.
Definition: climate_mode.h:18
ClimateMode mode
The active mode of the climate device.
Definition: climate.h:173
const optional< float > & get_target_temperature_low() const
Definition: climate.cpp:275
bool supports_cool_
Whether the controller supports cooling.
float target_temperature_high
The maximum target temperature of the climate device, for climate devices with split target temperatu...
Definition: climate.h:191
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition: climate.h:179
bool has_value() const
Definition: optional.h:87
void set_supports_heat(bool supports_heat)
void set_supported_presets(std::set< ClimatePreset > presets)
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition: automation.h:95
Device is in away preset.
Definition: climate_mode.h:88
Trigger * idle_trigger_
The trigger to call when the controller should switch to idle mode.
BangBangClimateTargetTempConfig away_config_
The climate device is set to cool to reach the target temperature.
Definition: climate_mode.h:16
float state
This member variable stores the last state that has passed through all filters.
Definition: sensor.h:131
void control(const climate::ClimateCall &call) override
Override control to change settings of the climate device.
const optional< ClimatePreset > & get_preset() const
Definition: climate.cpp:280
optional< ClimatePreset > preset
The active preset of the climate device.
Definition: climate.h:208
void set_supported_modes(std::set< ClimateMode > modes)
ClimateAction
Enum for the current action of the climate device. Values match those of ClimateMode.
Definition: climate_mode.h:31
void compute_state_()
Re-compute the state of this climate controller.
The climate device is set to heat/cool to reach the target temperature.
Definition: climate_mode.h:14
The climate device is actively heating.
Definition: climate_mode.h:37
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition: climate.cpp:395
void set_away_config(const BangBangClimateTargetTempConfig &away_config)
The climate device is off.
Definition: climate_mode.h:12
void set_supports_action(bool supports_action)
void change_away_(bool away)
Change the away setting, will reset target temperatures to defaults.
void set_supports_cool(bool supports_cool)
climate::ClimateTraits traits() override
Return the traits of this controller.
void switch_to_action_(climate::ClimateAction action)
Switch the climate device to the given climate mode.
void set_sensor(sensor::Sensor *sensor)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
The climate device is idle (monitoring climate but no action needed)
Definition: climate_mode.h:39
void set_humidity_sensor(sensor::Sensor *humidity_sensor)
void set_supports_two_point_target_temperature(bool supports_two_point_target_temperature)
optional< ClimateDeviceRestoreState > restore_state_()
Restore the state of the climate device, call this from your setup() method.
Definition: climate.cpp:329
void set_supports_current_temperature(bool supports_current_temperature)
const optional< float > & get_target_temperature_high() const
Definition: climate.cpp:276
Base-class for all sensors.
Definition: sensor.h:57
sensor::Sensor * humidity_sensor_
The sensor used for getting the current humidity.
BangBangClimateTargetTempConfig normal_config_
The climate device is actively cooling.
Definition: climate_mode.h:35
void add_supported_mode(ClimateMode mode)
esphome::sensor::Sensor * sensor
Definition: statsd.h:37
float target_temperature_low
The minimum target temperature of the climate device, for climate devices with split target temperatu...
Definition: climate.h:189
void stop_action()
Stop any action connected to this trigger.
Definition: automation.h:103
ClimateAction action
The active state of the climate device.
Definition: climate.h:176
bool state
Definition: fan.h:34
Trigger * cool_trigger_
The trigger to call when the controller should switch to cooling mode.