ESPHome 2025.12.0-dev
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 : idle_trigger_(new Trigger<>()), cool_trigger_(new Trigger<>()), heat_trigger_(new Trigger<>()) {}
11
13 this->sensor_->add_on_state_callback([this](float state) {
15 // control may have changed, recompute
16 this->compute_state_();
17 // current temperature changed, publish state
18 this->publish_state();
19 });
20 this->current_temperature = this->sensor_->state;
21
22 // register for humidity values and get initial state
23 if (this->humidity_sensor_ != nullptr) {
24 this->humidity_sensor_->add_on_state_callback([this](float state) {
25 this->current_humidity = state;
26 this->publish_state();
27 });
29 }
30
31 // restore set points
32 auto restore = this->restore_state_();
33 if (restore.has_value()) {
34 restore->to_call(this).perform();
35 } else {
36 // restore from defaults, change_away handles those for us
37 if (this->supports_cool_ && this->supports_heat_) {
39 } else if (this->supports_cool_) {
41 } else if (this->supports_heat_) {
43 }
44 this->change_away_(false);
45 }
46}
47
49 if (call.get_mode().has_value()) {
50 this->mode = *call.get_mode();
51 }
52 if (call.get_target_temperature_low().has_value()) {
54 }
55 if (call.get_target_temperature_high().has_value()) {
57 }
58 if (call.get_preset().has_value()) {
60 }
61
62 this->compute_state_();
63 this->publish_state();
64}
65
93
95 if (this->mode == climate::CLIMATE_MODE_OFF) {
97 return;
98 }
99 if (std::isnan(this->current_temperature) || std::isnan(this->target_temperature_low) ||
100 std::isnan(this->target_temperature_high)) {
101 // if any control parameters are nan, go to OFF action (not IDLE!)
103 return;
104 }
105 const bool too_cold = this->current_temperature < this->target_temperature_low;
106 const bool too_hot = this->current_temperature > this->target_temperature_high;
107
108 climate::ClimateAction target_action;
109 if (too_cold) {
110 // too cold -> enable heating if possible and enabled, else idle
111 if (this->supports_heat_ &&
113 target_action = climate::CLIMATE_ACTION_HEATING;
114 } else {
115 target_action = climate::CLIMATE_ACTION_IDLE;
116 }
117 } else if (too_hot) {
118 // too hot -> enable cooling if possible and enabled, else idle
119 if (this->supports_cool_ &&
121 target_action = climate::CLIMATE_ACTION_COOLING;
122 } else {
123 target_action = climate::CLIMATE_ACTION_IDLE;
124 }
125 } else {
126 // neither too hot nor too cold -> in range
127 if (this->supports_cool_ && this->supports_heat_ && this->mode == climate::CLIMATE_MODE_HEAT_COOL) {
128 // if supports both ends and both cooling and heating enabled, go to idle action
129 target_action = climate::CLIMATE_ACTION_IDLE;
130 } else {
131 // else use current mode and don't change (hysteresis)
132 target_action = this->action;
133 }
134 }
135
136 this->switch_to_action_(target_action);
137}
138
140 if (action == this->action) {
141 // already in target mode
142 return;
143 }
144
147 // switching from OFF to IDLE or vice-versa
148 // these only have visual difference. OFF means user manually disabled,
149 // IDLE means it's in auto mode but value is in target range.
150 this->action = action;
151 this->publish_state();
152 return;
153 }
154
155 if (this->prev_trigger_ != nullptr) {
156 this->prev_trigger_->stop_action();
157 this->prev_trigger_ = nullptr;
158 }
159 Trigger<> *trig;
160 switch (action) {
163 trig = this->idle_trigger_;
164 break;
166 trig = this->cool_trigger_;
167 break;
169 trig = this->heat_trigger_;
170 break;
171 default:
172 trig = nullptr;
173 }
174 if (trig != nullptr) {
175 trig->trigger();
176 } else {
177 ESP_LOGW(TAG, "trig not set - unsupported action");
178 }
179 this->action = action;
180 this->prev_trigger_ = trig;
181 this->publish_state();
182}
183
194
196 this->normal_config_ = normal_config;
197}
198
200 this->supports_away_ = true;
201 this->away_config_ = away_config;
202}
203
204void BangBangClimate::set_sensor(sensor::Sensor *sensor) { this->sensor_ = sensor; }
205void BangBangClimate::set_humidity_sensor(sensor::Sensor *humidity_sensor) { this->humidity_sensor_ = humidity_sensor; }
206
210
211void BangBangClimate::set_supports_cool(bool supports_cool) { this->supports_cool_ = supports_cool; }
212void BangBangClimate::set_supports_heat(bool supports_heat) { this->supports_heat_ = supports_heat; }
213
215 LOG_CLIMATE("", "Bang Bang Climate", this);
216 ESP_LOGCONFIG(TAG,
217 " Supports HEAT: %s\n"
218 " Supports COOL: %s\n"
219 " Supports AWAY mode: %s\n"
220 " Default Target Temperature Low: %.2f°C\n"
221 " Default Target Temperature High: %.2f°C",
222 YESNO(this->supports_heat_), YESNO(this->supports_cool_), YESNO(this->supports_away_),
223 this->normal_config_.default_temperature_low, this->normal_config_.default_temperature_high);
224}
225
228 float default_temperature_high)
229 : default_temperature_low(default_temperature_low), default_temperature_high(default_temperature_high) {}
230
231} // namespace bang_bang
232} // namespace esphome
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
Definition automation.h:169
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:177
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.
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.
Trigger * idle_trigger_
The trigger to call when the controller should switch to idle mode.
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.
Trigger * cool_trigger_
The trigger to call when the controller should switch to cooling mode.
void switch_to_action_(climate::ClimateAction action)
Switch the climate device to the given climate mode.
Trigger * heat_trigger_
The trigger to call when the controller should switch to heating mode.
BangBangClimateTargetTempConfig normal_config_
sensor::Sensor * humidity_sensor_
The sensor used for getting the current humidity.
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:288
const optional< ClimatePreset > & get_preset() const
Definition climate.cpp:295
const optional< float > & get_target_temperature_high() const
Definition climate.cpp:289
const optional< ClimateMode > & get_mode() const
Definition climate.cpp:292
ClimateMode mode
The active mode of the climate device.
Definition climate.h:256
float current_humidity
The current humidity of the climate device, as reported from the integration.
Definition climate.h:233
float target_temperature_low
The minimum target temperature of the climate device, for climate devices with split target temperatu...
Definition climate.h:240
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:230
ClimateAction action
The active state of the climate device.
Definition climate.h:259
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:426
optional< ClimatePreset > preset
The active preset of the climate device.
Definition climate.h:253
optional< ClimateDeviceRestoreState > restore_state_()
Restore the state of the climate device, call this from your setup() method.
Definition climate.cpp:350
float target_temperature_high
The maximum target temperature of the climate device, for climate devices with split target temperatu...
Definition climate.h:242
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:42
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:90
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:116
bool state
Definition fan.h:0
@ 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