ESPHome  2024.10.2
thermostat_climate.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/hal.h"
8 
9 #include <cinttypes>
10 #include <map>
11 #include <vector>
12 
13 namespace esphome {
14 namespace thermostat {
15 
27 };
28 
29 enum OnBootRestoreFrom : size_t { MEMORY = 0, DEFAULT_PRESET = 1 };
31  bool active;
32  uint32_t time;
33  uint32_t started;
34  std::function<void()> func;
35 };
36 
38  public:
40  ThermostatClimateTargetTempConfig(float default_temperature);
41  ThermostatClimateTargetTempConfig(float default_temperature_low, float default_temperature_high);
42 
45  void set_mode(climate::ClimateMode mode) { this->mode_ = mode; }
46 
47  float default_temperature{NAN};
48  float default_temperature_low{NAN};
49  float default_temperature_high{NAN};
50  float cool_deadband_{NAN};
51  float cool_overrun_{NAN};
52  float heat_deadband_{NAN};
53  float heat_overrun_{NAN};
57 };
58 
60  public:
62  void setup() override;
63  void dump_config() override;
64  void loop() override;
65 
66  void set_default_preset(const std::string &custom_preset);
67  void set_default_preset(climate::ClimatePreset preset);
68  void set_on_boot_restore_from(thermostat::OnBootRestoreFrom on_boot_restore_from);
69  void set_set_point_minimum_differential(float differential);
70  void set_cool_deadband(float deadband);
71  void set_cool_overrun(float overrun);
72  void set_heat_deadband(float deadband);
73  void set_heat_overrun(float overrun);
74  void set_supplemental_cool_delta(float delta);
75  void set_supplemental_heat_delta(float delta);
76  void set_cooling_maximum_run_time_in_sec(uint32_t time);
77  void set_heating_maximum_run_time_in_sec(uint32_t time);
78  void set_cooling_minimum_off_time_in_sec(uint32_t time);
79  void set_cooling_minimum_run_time_in_sec(uint32_t time);
80  void set_fan_mode_minimum_switching_time_in_sec(uint32_t time);
81  void set_fanning_minimum_off_time_in_sec(uint32_t time);
82  void set_fanning_minimum_run_time_in_sec(uint32_t time);
83  void set_heating_minimum_off_time_in_sec(uint32_t time);
84  void set_heating_minimum_run_time_in_sec(uint32_t time);
85  void set_idle_minimum_time_in_sec(uint32_t time);
86  void set_sensor(sensor::Sensor *sensor);
87  void set_humidity_sensor(sensor::Sensor *humidity_sensor);
88  void set_use_startup_delay(bool use_startup_delay);
89  void set_supports_auto(bool supports_auto);
90  void set_supports_heat_cool(bool supports_heat_cool);
91  void set_supports_cool(bool supports_cool);
92  void set_supports_dry(bool supports_dry);
93  void set_supports_fan_only(bool supports_fan_only);
94  void set_supports_fan_only_action_uses_fan_mode_timer(bool fan_only_action_uses_fan_mode_timer);
95  void set_supports_fan_only_cooling(bool supports_fan_only_cooling);
96  void set_supports_fan_with_cooling(bool supports_fan_with_cooling);
97  void set_supports_fan_with_heating(bool supports_fan_with_heating);
98  void set_supports_heat(bool supports_heat);
99  void set_supports_fan_mode_on(bool supports_fan_mode_on);
100  void set_supports_fan_mode_off(bool supports_fan_mode_off);
101  void set_supports_fan_mode_auto(bool supports_fan_mode_auto);
102  void set_supports_fan_mode_low(bool supports_fan_mode_low);
103  void set_supports_fan_mode_medium(bool supports_fan_mode_medium);
104  void set_supports_fan_mode_high(bool supports_fan_mode_high);
105  void set_supports_fan_mode_middle(bool supports_fan_mode_middle);
106  void set_supports_fan_mode_focus(bool supports_fan_mode_focus);
107  void set_supports_fan_mode_diffuse(bool supports_fan_mode_diffuse);
108  void set_supports_fan_mode_quiet(bool supports_fan_mode_quiet);
109  void set_supports_swing_mode_both(bool supports_swing_mode_both);
110  void set_supports_swing_mode_horizontal(bool supports_swing_mode_horizontal);
111  void set_supports_swing_mode_off(bool supports_swing_mode_off);
112  void set_supports_swing_mode_vertical(bool supports_swing_mode_vertical);
113  void set_supports_two_points(bool supports_two_points);
114 
115  void set_preset_config(climate::ClimatePreset preset, const ThermostatClimateTargetTempConfig &config);
116  void set_custom_preset_config(const std::string &name, const ThermostatClimateTargetTempConfig &config);
117 
118  Trigger<> *get_cool_action_trigger() const;
119  Trigger<> *get_supplemental_cool_action_trigger() const;
120  Trigger<> *get_dry_action_trigger() const;
121  Trigger<> *get_fan_only_action_trigger() const;
122  Trigger<> *get_heat_action_trigger() const;
123  Trigger<> *get_supplemental_heat_action_trigger() const;
124  Trigger<> *get_idle_action_trigger() const;
125  Trigger<> *get_auto_mode_trigger() const;
126  Trigger<> *get_cool_mode_trigger() const;
127  Trigger<> *get_dry_mode_trigger() const;
128  Trigger<> *get_fan_only_mode_trigger() const;
129  Trigger<> *get_heat_mode_trigger() const;
130  Trigger<> *get_off_mode_trigger() const;
131  Trigger<> *get_fan_mode_on_trigger() const;
132  Trigger<> *get_fan_mode_off_trigger() const;
133  Trigger<> *get_fan_mode_auto_trigger() const;
134  Trigger<> *get_fan_mode_low_trigger() const;
135  Trigger<> *get_fan_mode_medium_trigger() const;
136  Trigger<> *get_fan_mode_high_trigger() const;
137  Trigger<> *get_fan_mode_middle_trigger() const;
138  Trigger<> *get_fan_mode_focus_trigger() const;
139  Trigger<> *get_fan_mode_diffuse_trigger() const;
140  Trigger<> *get_fan_mode_quiet_trigger() const;
141  Trigger<> *get_swing_mode_both_trigger() const;
142  Trigger<> *get_swing_mode_horizontal_trigger() const;
143  Trigger<> *get_swing_mode_off_trigger() const;
144  Trigger<> *get_swing_mode_vertical_trigger() const;
145  Trigger<> *get_temperature_change_trigger() const;
146  Trigger<> *get_preset_change_trigger() const;
148  float cool_deadband();
149  float cool_overrun();
150  float heat_deadband();
151  float heat_overrun();
153  void refresh();
155  bool climate_action_change_delayed();
156  bool fan_mode_change_delayed();
158  climate::ClimateAction delayed_climate_action();
160  climate::ClimateFanMode locked_fan_mode();
162  bool hysteresis_valid(); // returns true if valid
163  void validate_target_temperature();
164  void validate_target_temperatures();
165  void validate_target_temperature_low();
166  void validate_target_temperature_high();
167 
168  protected:
170  void control(const climate::ClimateCall &call) override;
171 
173  void change_preset_(climate::ClimatePreset preset);
175  void change_custom_preset_(const std::string &custom_preset);
176 
180  bool change_preset_internal_(const ThermostatClimateTargetTempConfig &config);
181 
183  climate::ClimateTraits traits() override;
184 
186  climate::ClimateAction compute_action_(bool ignore_timers = false);
187  climate::ClimateAction compute_supplemental_action_();
188 
190  void switch_to_action_(climate::ClimateAction action, bool publish_state = true);
191  void switch_to_supplemental_action_(climate::ClimateAction action);
192  void trigger_supplemental_action_();
193 
195  void switch_to_fan_mode_(climate::ClimateFanMode fan_mode, bool publish_state = true);
196 
198  void switch_to_mode_(climate::ClimateMode mode, bool publish_state = true);
199 
201  void switch_to_swing_mode_(climate::ClimateSwingMode swing_mode, bool publish_state = true);
202 
204  void check_temperature_change_trigger_();
205 
207  bool idle_action_ready_();
208  bool cooling_action_ready_();
209  bool drying_action_ready_();
210  bool fan_mode_ready_();
211  bool fanning_action_ready_();
212  bool heating_action_ready_();
213 
215  void start_timer_(ThermostatClimateTimerIndex timer_index);
216  bool cancel_timer_(ThermostatClimateTimerIndex timer_index);
217  bool timer_active_(ThermostatClimateTimerIndex timer_index);
218  uint32_t timer_duration_(ThermostatClimateTimerIndex timer_index);
219  std::function<void()> timer_cbf_(ThermostatClimateTimerIndex timer_index);
220 
222  void cooling_max_run_time_timer_callback_();
223  void cooling_off_timer_callback_();
224  void cooling_on_timer_callback_();
225  void fan_mode_timer_callback_();
226  void fanning_off_timer_callback_();
227  void fanning_on_timer_callback_();
228  void heating_max_run_time_timer_callback_();
229  void heating_off_timer_callback_();
230  void heating_on_timer_callback_();
231  void idle_on_timer_callback_();
232 
234  bool cooling_required_();
235  bool fanning_required_();
236  bool heating_required_();
237  bool supplemental_cooling_required_();
238  bool supplemental_heating_required_();
239 
240  void dump_preset_config_(const char *preset_name, const ThermostatClimateTargetTempConfig &config,
241  bool is_default_preset);
242 
244  sensor::Sensor *sensor_{nullptr};
246  sensor::Sensor *humidity_sensor_{nullptr};
247 
252  bool supports_auto_{false};
253  bool supports_heat_cool_{false};
254  bool supports_cool_{false};
255  bool supports_dry_{false};
256  bool supports_fan_only_{false};
257  bool supports_heat_{false};
259  bool supports_fan_only_action_uses_fan_mode_timer_{false};
261  bool supports_fan_only_cooling_{false};
263  bool supports_fan_with_cooling_{false};
264  bool supports_fan_with_heating_{false};
265 
270  bool supports_fan_mode_on_{false};
271  bool supports_fan_mode_off_{false};
272 
277  bool supports_fan_mode_auto_{false};
278 
282  bool supports_fan_mode_low_{false};
283  bool supports_fan_mode_medium_{false};
284  bool supports_fan_mode_high_{false};
285  bool supports_fan_mode_middle_{false};
286  bool supports_fan_mode_focus_{false};
287  bool supports_fan_mode_diffuse_{false};
288  bool supports_fan_mode_quiet_{false};
289 
293  bool supports_swing_mode_both_{false};
294  bool supports_swing_mode_off_{false};
295  bool supports_swing_mode_horizontal_{false};
296  bool supports_swing_mode_vertical_{false};
297 
301  bool supports_two_points_{false};
302 
304  bool cooling_max_runtime_exceeded_{false};
305  bool heating_max_runtime_exceeded_{false};
306 
308  bool use_startup_delay_{false};
309 
311  bool setup_complete_{false};
312 
318  Trigger<> *cool_action_trigger_{nullptr};
319  Trigger<> *supplemental_cool_action_trigger_{nullptr};
320  Trigger<> *cool_mode_trigger_{nullptr};
321 
326  Trigger<> *dry_action_trigger_{nullptr};
327  Trigger<> *dry_mode_trigger_{nullptr};
328 
334  Trigger<> *heat_action_trigger_{nullptr};
335  Trigger<> *supplemental_heat_action_trigger_{nullptr};
336  Trigger<> *heat_mode_trigger_{nullptr};
337 
342  Trigger<> *auto_mode_trigger_{nullptr};
343 
347  Trigger<> *idle_action_trigger_{nullptr};
348  Trigger<> *off_mode_trigger_{nullptr};
349 
354  Trigger<> *fan_only_action_trigger_{nullptr};
355  Trigger<> *fan_only_mode_trigger_{nullptr};
356 
358  Trigger<> *fan_mode_on_trigger_{nullptr};
359 
361  Trigger<> *fan_mode_off_trigger_{nullptr};
362 
364  Trigger<> *fan_mode_auto_trigger_{nullptr};
365 
367  Trigger<> *fan_mode_low_trigger_{nullptr};
368 
370  Trigger<> *fan_mode_medium_trigger_{nullptr};
371 
373  Trigger<> *fan_mode_high_trigger_{nullptr};
374 
376  Trigger<> *fan_mode_middle_trigger_{nullptr};
377 
379  Trigger<> *fan_mode_focus_trigger_{nullptr};
380 
382  Trigger<> *fan_mode_diffuse_trigger_{nullptr};
383 
385  Trigger<> *fan_mode_quiet_trigger_{nullptr};
386 
388  Trigger<> *swing_mode_both_trigger_{nullptr};
389 
391  Trigger<> *swing_mode_off_trigger_{nullptr};
392 
394  Trigger<> *swing_mode_horizontal_trigger_{nullptr};
395 
397  Trigger<> *swing_mode_vertical_trigger_{nullptr};
398 
400  Trigger<> *temperature_change_trigger_{nullptr};
401 
403  Trigger<> *preset_change_trigger_{nullptr};
404 
409  Trigger<> *prev_action_trigger_{nullptr};
410  Trigger<> *prev_fan_mode_trigger_{nullptr};
411  Trigger<> *prev_mode_trigger_{nullptr};
412  Trigger<> *prev_swing_mode_trigger_{nullptr};
413 
421 
425  float prev_target_temperature_{NAN};
426  float prev_target_temperature_low_{NAN};
427  float prev_target_temperature_high_{NAN};
428 
430  float set_point_minimum_differential_{0};
431 
433  float cooling_deadband_{0};
434  float cooling_overrun_{0};
435  float heating_deadband_{0};
436  float heating_overrun_{0};
437 
439  float supplemental_cool_delta_{0};
440  float supplemental_heat_delta_{0};
441 
443  const uint8_t min_timer_duration_{1};
444 
446  std::vector<ThermostatClimateTimer> timer_{
447  {false, 0, 0, std::bind(&ThermostatClimate::cooling_max_run_time_timer_callback_, this)},
448  {false, 0, 0, std::bind(&ThermostatClimate::cooling_off_timer_callback_, this)},
449  {false, 0, 0, std::bind(&ThermostatClimate::cooling_on_timer_callback_, this)},
450  {false, 0, 0, std::bind(&ThermostatClimate::fan_mode_timer_callback_, this)},
451  {false, 0, 0, std::bind(&ThermostatClimate::fanning_off_timer_callback_, this)},
452  {false, 0, 0, std::bind(&ThermostatClimate::fanning_on_timer_callback_, this)},
453  {false, 0, 0, std::bind(&ThermostatClimate::heating_max_run_time_timer_callback_, this)},
454  {false, 0, 0, std::bind(&ThermostatClimate::heating_off_timer_callback_, this)},
455  {false, 0, 0, std::bind(&ThermostatClimate::heating_on_timer_callback_, this)},
456  {false, 0, 0, std::bind(&ThermostatClimate::idle_on_timer_callback_, this)},
457  };
458 
460  std::map<climate::ClimatePreset, ThermostatClimateTargetTempConfig> preset_config_{};
462  std::map<std::string, ThermostatClimateTargetTempConfig> custom_preset_config_{};
463 
465  climate::ClimatePreset default_preset_{};
467  std::string default_custom_preset_{};
468 
472 };
473 
474 } // namespace thermostat
475 } // namespace esphome
void setup()
This class is used to encode all control actions on a climate device.
Definition: climate.h:33
The climate device is off (inactive or no power)
Definition: climate_mode.h:33
const char * name
Definition: stm32flash.h:78
void loop()
ClimatePreset
Enum for all preset modes.
Definition: climate_mode.h:82
void set_fan_mode(climate::ClimateFanMode fan_mode)
This class contains all static data for climate devices.
void set_swing_mode(climate::ClimateSwingMode swing_mode)
ClimateSwingMode swing_mode
Definition: climate.h:581
ClimateSwingMode
Enum for all modes a climate swing can be in.
Definition: climate_mode.h:70
void cooling_max_run_time_timer_callback_()
set_timeout() callbacks for various actions (see above)
uint8_t custom_preset
Definition: climate.h:579
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:183
ClimateAction
Enum for the current action of the climate device. Values match those of ClimateMode.
Definition: climate_mode.h:31
ClimateMode
Enum for all modes a climate device can be in.
Definition: climate_mode.h:10
The swing mode is set to Off.
Definition: climate_mode.h:72
The climate device is off.
Definition: climate_mode.h:12
ClimateFanMode fan_mode
Definition: climate.h:573
The fan mode is set to On.
Definition: climate_mode.h:48
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base-class for all sensors.
Definition: sensor.h:57
esphome::sensor::Sensor * sensor
Definition: statsd.h:37
ClimatePreset preset
Definition: climate.h:578
ClimateDevice - This is the base class for all climate integrations.
Definition: climate.h:168