ESPHome 2026.2.1
Loading...
Searching...
No Matches
bang_bang_climate.cpp
Go to the documentation of this file.
1#include "bang_bang_climate.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace bang_bang {
6
7static const char *const TAG = "bang_bang.climate";
8
10
12 this->sensor_->add_on_state_callback([this](float state) {
14 // control may have changed, recompute
15 this->compute_state_();
16 // current temperature changed, publish state
17 this->publish_state();
18 });
19 this->current_temperature = this->sensor_->state;
20
21 // register for humidity values and get initial state
22 if (this->humidity_sensor_ != nullptr) {
23 this->humidity_sensor_->add_on_state_callback([this](float state) {
24 this->current_humidity = state;
25 this->publish_state();
26 });
28 }
29
30 // restore set points
31 auto restore = this->restore_state_();
32 if (restore.has_value()) {
33 restore->to_call(this).perform();
34 } else {
35 // restore from defaults, change_away handles those for us
36 if (this->supports_cool_ && this->supports_heat_) {
38 } else if (this->supports_cool_) {
40 } else if (this->supports_heat_) {
42 }
43 this->change_away_(false);
44 }
45}
46
48 if (call.get_mode().has_value()) {
49 this->mode = *call.get_mode();
50 }
51 if (call.get_target_temperature_low().has_value()) {
53 }
54 if (call.get_target_temperature_high().has_value()) {
56 }
57 if (call.get_preset().has_value()) {
59 }
60
61 this->compute_state_();
62 this->publish_state();
63}
64
92
94 if (this->mode == climate::CLIMATE_MODE_OFF) {
96 return;
97 }
98 if (std::isnan(this->current_temperature) || std::isnan(this->target_temperature_low) ||
99 std::isnan(this->target_temperature_high)) {
100 // if any control parameters are nan, go to OFF action (not IDLE!)
102 return;
103 }
104 const bool too_cold = this->current_temperature < this->target_temperature_low;
105 const bool too_hot = this->current_temperature > this->target_temperature_high;
106
107 climate::ClimateAction target_action;
108 if (too_cold) {
109 // too cold -> enable heating if possible and enabled, else idle
110 if (this->supports_heat_ &&
112 target_action = climate::CLIMATE_ACTION_HEATING;
113 } else {
114 target_action = climate::CLIMATE_ACTION_IDLE;
115 }
116 } else if (too_hot) {
117 // too hot -> enable cooling if possible and enabled, else idle
118 if (this->supports_cool_ &&
120 target_action = climate::CLIMATE_ACTION_COOLING;
121 } else {
122 target_action = climate::CLIMATE_ACTION_IDLE;
123 }
124 } else {
125 // neither too hot nor too cold -> in range
126 if (this->supports_cool_ && this->supports_heat_ && this->mode == climate::CLIMATE_MODE_HEAT_COOL) {
127 // if supports both ends and both cooling and heating enabled, go to idle action
128 target_action = climate::CLIMATE_ACTION_IDLE;
129 } else {
130 // else use current mode and don't change (hysteresis)
131 target_action = this->action;
132 }
133 }
134
135 this->switch_to_action_(target_action);
136}
137
139 if (action == this->action) {
140 // already in target mode
141 return;
142 }
143
146 // switching from OFF to IDLE or vice-versa
147 // these only have visual difference. OFF means user manually disabled,
148 // IDLE means it's in auto mode but value is in target range.
149 this->action = action;
150 this->publish_state();
151 return;
152 }
153
154 if (this->prev_trigger_ != nullptr) {
155 this->prev_trigger_->stop_action();
156 this->prev_trigger_ = nullptr;
157 }
158 Trigger<> *trig;
159 switch (action) {
162 trig = &this->idle_trigger_;
163 break;
165 trig = &this->cool_trigger_;
166 break;
168 trig = &this->heat_trigger_;
169 break;
170 default:
171 trig = nullptr;
172 }
173 if (trig != nullptr) {
174 trig->trigger();
175 } else {
176 ESP_LOGW(TAG, "trig not set - unsupported action");
177 }
178 this->action = action;
179 this->prev_trigger_ = trig;
180 this->publish_state();
181}
182
193
195 this->normal_config_ = normal_config;
196}
197
199 this->supports_away_ = true;
200 this->away_config_ = away_config;
201}
202
203void BangBangClimate::set_sensor(sensor::Sensor *sensor) { this->sensor_ = sensor; }
204void BangBangClimate::set_humidity_sensor(sensor::Sensor *humidity_sensor) { this->humidity_sensor_ = humidity_sensor; }
205
209
210void BangBangClimate::set_supports_cool(bool supports_cool) { this->supports_cool_ = supports_cool; }
211void BangBangClimate::set_supports_heat(bool supports_heat) { this->supports_heat_ = supports_heat; }
212
214 LOG_CLIMATE("", "Bang Bang Climate", this);
215 ESP_LOGCONFIG(TAG,
216 " Supports HEAT: %s\n"
217 " Supports COOL: %s\n"
218 " Supports AWAY mode: %s\n"
219 " Default Target Temperature Low: %.2f°C\n"
220 " Default Target Temperature High: %.2f°C",
221 YESNO(this->supports_heat_), YESNO(this->supports_cool_), YESNO(this->supports_away_),
222 this->normal_config_.default_temperature_low, this->normal_config_.default_temperature_high);
223}
224
227 float default_temperature_high)
228 : default_temperature_low(default_temperature_low), default_temperature_high(default_temperature_high) {}
229
230} // namespace bang_bang
231} // namespace esphome
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
Definition automation.h:279
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:287
void compute_state_()
Re-compute the state of this climate controller.
BangBangClimateTargetTempConfig away_config_
void set_away_config(const BangBangClimateTargetTempConfig &away_config)
bool supports_cool_
Whether the controller supports cooling/heating.
void set_supports_heat(bool supports_heat)
climate::ClimateTraits traits() override
Return the traits of this controller.
Trigger idle_trigger_
The trigger to call when the controller should switch to idle mode.
void control(const climate::ClimateCall &call) override
Override control to change settings of the climate device.
void set_sensor(sensor::Sensor *sensor)
void set_supports_cool(bool supports_cool)
void change_away_(bool away)
Change the away setting, will reset target temperatures to defaults.
void set_normal_config(const BangBangClimateTargetTempConfig &normal_config)
Trigger * prev_trigger_
A reference to the trigger that was previously active.
sensor::Sensor * sensor_
The sensor used for getting the current temperature.
void switch_to_action_(climate::ClimateAction action)
Switch the climate device to the given climate mode.
Trigger cool_trigger_
The trigger to call when the controller should switch to cooling mode.
BangBangClimateTargetTempConfig normal_config_
sensor::Sensor * humidity_sensor_
The sensor used for getting the current humidity.
Trigger heat_trigger_
The trigger to call when the controller should switch to heating mode.
void set_humidity_sensor(sensor::Sensor *humidity_sensor)
This class is used to encode all control actions on a climate device.
Definition climate.h:33
const optional< float > & get_target_temperature_low() const
Definition climate.cpp:300
const optional< ClimatePreset > & get_preset() const
Definition climate.cpp:307
const optional< float > & get_target_temperature_high() const
Definition climate.cpp:301
const optional< ClimateMode > & get_mode() const
Definition climate.cpp:304
ClimateMode mode
The active mode of the climate device.
Definition climate.h:262
float current_humidity
The current humidity of the climate device, as reported from the integration.
Definition climate.h:239
float target_temperature_low
The minimum target temperature of the climate device, for climate devices with split target temperatu...
Definition climate.h:246
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:236
ClimateAction action
The active state of the climate device.
Definition climate.h:265
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:437
optional< ClimatePreset > preset
The active preset of the climate device.
Definition climate.h:259
optional< ClimateDeviceRestoreState > restore_state_()
Restore the state of the climate device, call this from your setup() method.
Definition climate.cpp:362
float target_temperature_high
The maximum target temperature of the climate device, for climate devices with split target temperatu...
Definition climate.h:248
void add_feature_flags(uint32_t feature_flags)
void set_supported_presets(ClimatePresetMask presets)
void set_supported_modes(ClimateModeMask modes)
void add_supported_mode(ClimateMode mode)
Base-class for all sensors.
Definition sensor.h:43
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:78
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:117
bool state
Definition fan.h:2
@ CLIMATE_SUPPORTS_CURRENT_HUMIDITY
@ CLIMATE_SUPPORTS_CURRENT_TEMPERATURE
@ CLIMATE_REQUIRES_TWO_POINT_TARGET_TEMPERATURE
@ CLIMATE_PRESET_AWAY
Device is in away preset.
@ CLIMATE_PRESET_HOME
Device is in home preset.
@ CLIMATE_MODE_HEAT
The climate device is set to heat to reach the target temperature.
@ CLIMATE_MODE_COOL
The climate device is set to cool to reach the target temperature.
@ CLIMATE_MODE_HEAT_COOL
The climate device is set to heat/cool to reach the target temperature.
@ CLIMATE_MODE_OFF
The climate device is off.
ClimateAction
Enum for the current action of the climate device. Values match those of ClimateMode.
@ CLIMATE_ACTION_OFF
The climate device is off (inactive or no power)
@ CLIMATE_ACTION_IDLE
The climate device is idle (monitoring climate but no action needed)
@ CLIMATE_ACTION_HEATING
The climate device is actively heating.
@ CLIMATE_ACTION_COOLING
The climate device is actively cooling.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7