ESPHome  2022.12.8
thermostat_climate.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include <map>
9 #include <vector>
10 
11 namespace esphome {
12 namespace thermostat {
13 
25 };
26 
27 enum OnBootRestoreFrom : size_t { MEMORY = 0, DEFAULT_PRESET = 1 };
29  const std::string name;
30  bool active;
31  uint32_t time;
32  std::function<void()> func;
33 };
34 
36  public:
38  ThermostatClimateTargetTempConfig(float default_temperature);
39  ThermostatClimateTargetTempConfig(float default_temperature_low, float default_temperature_high);
40 
43  void set_mode(climate::ClimateMode mode) { this->mode_ = mode; }
44 
45  float default_temperature{NAN};
46  float default_temperature_low{NAN};
47  float default_temperature_high{NAN};
48  float cool_deadband_{NAN};
49  float cool_overrun_{NAN};
50  float heat_deadband_{NAN};
51  float heat_overrun_{NAN};
55 };
56 
58  public:
60  void setup() override;
61  void dump_config() override;
62 
63  void set_default_preset(const std::string &custom_preset);
64  void set_default_preset(climate::ClimatePreset preset);
65  void set_on_boot_restore_from(thermostat::OnBootRestoreFrom on_boot_restore_from);
66  void set_set_point_minimum_differential(float differential);
67  void set_cool_deadband(float deadband);
68  void set_cool_overrun(float overrun);
69  void set_heat_deadband(float deadband);
70  void set_heat_overrun(float overrun);
71  void set_supplemental_cool_delta(float delta);
72  void set_supplemental_heat_delta(float delta);
73  void set_cooling_maximum_run_time_in_sec(uint32_t time);
74  void set_heating_maximum_run_time_in_sec(uint32_t time);
75  void set_cooling_minimum_off_time_in_sec(uint32_t time);
76  void set_cooling_minimum_run_time_in_sec(uint32_t time);
77  void set_fan_mode_minimum_switching_time_in_sec(uint32_t time);
78  void set_fanning_minimum_off_time_in_sec(uint32_t time);
79  void set_fanning_minimum_run_time_in_sec(uint32_t time);
80  void set_heating_minimum_off_time_in_sec(uint32_t time);
81  void set_heating_minimum_run_time_in_sec(uint32_t time);
82  void set_idle_minimum_time_in_sec(uint32_t time);
83  void set_sensor(sensor::Sensor *sensor);
84  void set_use_startup_delay(bool use_startup_delay);
85  void set_supports_auto(bool supports_auto);
86  void set_supports_heat_cool(bool supports_heat_cool);
87  void set_supports_cool(bool supports_cool);
88  void set_supports_dry(bool supports_dry);
89  void set_supports_fan_only(bool supports_fan_only);
90  void set_supports_fan_only_action_uses_fan_mode_timer(bool fan_only_action_uses_fan_mode_timer);
91  void set_supports_fan_only_cooling(bool supports_fan_only_cooling);
92  void set_supports_fan_with_cooling(bool supports_fan_with_cooling);
93  void set_supports_fan_with_heating(bool supports_fan_with_heating);
94  void set_supports_heat(bool supports_heat);
95  void set_supports_fan_mode_on(bool supports_fan_mode_on);
96  void set_supports_fan_mode_off(bool supports_fan_mode_off);
97  void set_supports_fan_mode_auto(bool supports_fan_mode_auto);
98  void set_supports_fan_mode_low(bool supports_fan_mode_low);
99  void set_supports_fan_mode_medium(bool supports_fan_mode_medium);
100  void set_supports_fan_mode_high(bool supports_fan_mode_high);
101  void set_supports_fan_mode_middle(bool supports_fan_mode_middle);
102  void set_supports_fan_mode_focus(bool supports_fan_mode_focus);
103  void set_supports_fan_mode_diffuse(bool supports_fan_mode_diffuse);
104  void set_supports_swing_mode_both(bool supports_swing_mode_both);
105  void set_supports_swing_mode_horizontal(bool supports_swing_mode_horizontal);
106  void set_supports_swing_mode_off(bool supports_swing_mode_off);
107  void set_supports_swing_mode_vertical(bool supports_swing_mode_vertical);
108  void set_supports_two_points(bool supports_two_points);
109 
110  void set_preset_config(climate::ClimatePreset preset, const ThermostatClimateTargetTempConfig &config);
111  void set_custom_preset_config(const std::string &name, const ThermostatClimateTargetTempConfig &config);
112 
113  Trigger<> *get_cool_action_trigger() const;
114  Trigger<> *get_supplemental_cool_action_trigger() const;
115  Trigger<> *get_dry_action_trigger() const;
116  Trigger<> *get_fan_only_action_trigger() const;
117  Trigger<> *get_heat_action_trigger() const;
118  Trigger<> *get_supplemental_heat_action_trigger() const;
119  Trigger<> *get_idle_action_trigger() const;
120  Trigger<> *get_auto_mode_trigger() const;
121  Trigger<> *get_cool_mode_trigger() const;
122  Trigger<> *get_dry_mode_trigger() const;
123  Trigger<> *get_fan_only_mode_trigger() const;
124  Trigger<> *get_heat_mode_trigger() const;
125  Trigger<> *get_off_mode_trigger() const;
126  Trigger<> *get_fan_mode_on_trigger() const;
127  Trigger<> *get_fan_mode_off_trigger() const;
128  Trigger<> *get_fan_mode_auto_trigger() const;
129  Trigger<> *get_fan_mode_low_trigger() const;
130  Trigger<> *get_fan_mode_medium_trigger() const;
131  Trigger<> *get_fan_mode_high_trigger() const;
132  Trigger<> *get_fan_mode_middle_trigger() const;
133  Trigger<> *get_fan_mode_focus_trigger() const;
134  Trigger<> *get_fan_mode_diffuse_trigger() const;
135  Trigger<> *get_swing_mode_both_trigger() const;
136  Trigger<> *get_swing_mode_horizontal_trigger() const;
137  Trigger<> *get_swing_mode_off_trigger() const;
138  Trigger<> *get_swing_mode_vertical_trigger() const;
139  Trigger<> *get_temperature_change_trigger() const;
140  Trigger<> *get_preset_change_trigger() const;
142  float cool_deadband();
143  float cool_overrun();
144  float heat_deadband();
145  float heat_overrun();
147  void refresh();
149  bool climate_action_change_delayed();
150  bool fan_mode_change_delayed();
152  climate::ClimateAction delayed_climate_action();
154  climate::ClimateFanMode locked_fan_mode();
156  bool hysteresis_valid(); // returns true if valid
157  void validate_target_temperature();
158  void validate_target_temperatures();
159  void validate_target_temperature_low();
160  void validate_target_temperature_high();
161 
162  protected:
164  void control(const climate::ClimateCall &call) override;
165 
167  void change_preset_(climate::ClimatePreset preset);
169  void change_custom_preset_(const std::string &custom_preset);
170 
174  bool change_preset_internal_(const ThermostatClimateTargetTempConfig &config);
175 
177  climate::ClimateTraits traits() override;
178 
180  climate::ClimateAction compute_action_(bool ignore_timers = false);
181  climate::ClimateAction compute_supplemental_action_();
182 
184  void switch_to_action_(climate::ClimateAction action, bool publish_state = true);
185  void switch_to_supplemental_action_(climate::ClimateAction action);
186  void trigger_supplemental_action_();
187 
189  void switch_to_fan_mode_(climate::ClimateFanMode fan_mode, bool publish_state = true);
190 
192  void switch_to_mode_(climate::ClimateMode mode, bool publish_state = true);
193 
195  void switch_to_swing_mode_(climate::ClimateSwingMode swing_mode, bool publish_state = true);
196 
198  void check_temperature_change_trigger_();
199 
201  bool idle_action_ready_();
202  bool cooling_action_ready_();
203  bool drying_action_ready_();
204  bool fan_mode_ready_();
205  bool fanning_action_ready_();
206  bool heating_action_ready_();
207 
209  void start_timer_(ThermostatClimateTimerIndex timer_index);
210  bool cancel_timer_(ThermostatClimateTimerIndex timer_index);
211  bool timer_active_(ThermostatClimateTimerIndex timer_index);
212  uint32_t timer_duration_(ThermostatClimateTimerIndex timer_index);
213  std::function<void()> timer_cbf_(ThermostatClimateTimerIndex timer_index);
214 
216  void cooling_max_run_time_timer_callback_();
217  void cooling_off_timer_callback_();
218  void cooling_on_timer_callback_();
219  void fan_mode_timer_callback_();
220  void fanning_off_timer_callback_();
221  void fanning_on_timer_callback_();
222  void heating_max_run_time_timer_callback_();
223  void heating_off_timer_callback_();
224  void heating_on_timer_callback_();
225  void idle_on_timer_callback_();
226 
228  bool cooling_required_();
229  bool fanning_required_();
230  bool heating_required_();
231  bool supplemental_cooling_required_();
232  bool supplemental_heating_required_();
233 
234  void dump_preset_config_(const char *preset_name, const ThermostatClimateTargetTempConfig &config,
235  bool is_default_preset);
236 
238  sensor::Sensor *sensor_{nullptr};
239 
244  bool supports_auto_{false};
245  bool supports_heat_cool_{false};
246  bool supports_cool_{false};
247  bool supports_dry_{false};
248  bool supports_fan_only_{false};
249  bool supports_heat_{false};
251  bool supports_fan_only_action_uses_fan_mode_timer_{false};
253  bool supports_fan_only_cooling_{false};
255  bool supports_fan_with_cooling_{false};
256  bool supports_fan_with_heating_{false};
257 
262  bool supports_fan_mode_on_{false};
263  bool supports_fan_mode_off_{false};
264 
269  bool supports_fan_mode_auto_{false};
270 
274  bool supports_fan_mode_low_{false};
275  bool supports_fan_mode_medium_{false};
276  bool supports_fan_mode_high_{false};
277  bool supports_fan_mode_middle_{false};
278  bool supports_fan_mode_focus_{false};
279  bool supports_fan_mode_diffuse_{false};
280 
284  bool supports_swing_mode_both_{false};
285  bool supports_swing_mode_off_{false};
286  bool supports_swing_mode_horizontal_{false};
287  bool supports_swing_mode_vertical_{false};
288 
292  bool supports_two_points_{false};
293 
295  bool cooling_max_runtime_exceeded_{false};
296  bool heating_max_runtime_exceeded_{false};
297 
299  bool use_startup_delay_{false};
300 
302  bool setup_complete_{false};
303 
309  Trigger<> *cool_action_trigger_{nullptr};
310  Trigger<> *supplemental_cool_action_trigger_{nullptr};
311  Trigger<> *cool_mode_trigger_{nullptr};
312 
317  Trigger<> *dry_action_trigger_{nullptr};
318  Trigger<> *dry_mode_trigger_{nullptr};
319 
325  Trigger<> *heat_action_trigger_{nullptr};
326  Trigger<> *supplemental_heat_action_trigger_{nullptr};
327  Trigger<> *heat_mode_trigger_{nullptr};
328 
333  Trigger<> *auto_mode_trigger_{nullptr};
334 
338  Trigger<> *idle_action_trigger_{nullptr};
339  Trigger<> *off_mode_trigger_{nullptr};
340 
345  Trigger<> *fan_only_action_trigger_{nullptr};
346  Trigger<> *fan_only_mode_trigger_{nullptr};
347 
349  Trigger<> *fan_mode_on_trigger_{nullptr};
350 
352  Trigger<> *fan_mode_off_trigger_{nullptr};
353 
355  Trigger<> *fan_mode_auto_trigger_{nullptr};
356 
358  Trigger<> *fan_mode_low_trigger_{nullptr};
359 
361  Trigger<> *fan_mode_medium_trigger_{nullptr};
362 
364  Trigger<> *fan_mode_high_trigger_{nullptr};
365 
367  Trigger<> *fan_mode_middle_trigger_{nullptr};
368 
370  Trigger<> *fan_mode_focus_trigger_{nullptr};
371 
373  Trigger<> *fan_mode_diffuse_trigger_{nullptr};
374 
376  Trigger<> *swing_mode_both_trigger_{nullptr};
377 
379  Trigger<> *swing_mode_off_trigger_{nullptr};
380 
382  Trigger<> *swing_mode_horizontal_trigger_{nullptr};
383 
385  Trigger<> *swing_mode_vertical_trigger_{nullptr};
386 
388  Trigger<> *temperature_change_trigger_{nullptr};
389 
391  Trigger<> *preset_change_trigger_{nullptr};
392 
397  Trigger<> *prev_action_trigger_{nullptr};
398  Trigger<> *prev_fan_mode_trigger_{nullptr};
399  Trigger<> *prev_mode_trigger_{nullptr};
400  Trigger<> *prev_swing_mode_trigger_{nullptr};
401 
409 
413  float prev_target_temperature_{NAN};
414  float prev_target_temperature_low_{NAN};
415  float prev_target_temperature_high_{NAN};
416 
418  float set_point_minimum_differential_{0};
419 
421  float cooling_deadband_{0};
422  float cooling_overrun_{0};
423  float heating_deadband_{0};
424  float heating_overrun_{0};
425 
427  float supplemental_cool_delta_{0};
428  float supplemental_heat_delta_{0};
429 
431  const uint8_t min_timer_duration_{1};
432 
434  std::vector<ThermostatClimateTimer> timer_{
435  {"cool_run", false, 0, std::bind(&ThermostatClimate::cooling_max_run_time_timer_callback_, this)},
436  {"cool_off", false, 0, std::bind(&ThermostatClimate::cooling_off_timer_callback_, this)},
437  {"cool_on", false, 0, std::bind(&ThermostatClimate::cooling_on_timer_callback_, this)},
438  {"fan_mode", false, 0, std::bind(&ThermostatClimate::fan_mode_timer_callback_, this)},
439  {"fan_off", false, 0, std::bind(&ThermostatClimate::fanning_off_timer_callback_, this)},
440  {"fan_on", false, 0, std::bind(&ThermostatClimate::fanning_on_timer_callback_, this)},
441  {"heat_run", false, 0, std::bind(&ThermostatClimate::heating_max_run_time_timer_callback_, this)},
442  {"heat_off", false, 0, std::bind(&ThermostatClimate::heating_off_timer_callback_, this)},
443  {"heat_on", false, 0, std::bind(&ThermostatClimate::heating_on_timer_callback_, this)},
444  {"idle_on", false, 0, std::bind(&ThermostatClimate::idle_on_timer_callback_, this)}};
445 
447  std::map<climate::ClimatePreset, ThermostatClimateTargetTempConfig> preset_config_{};
449  std::map<std::string, ThermostatClimateTargetTempConfig> custom_preset_config_{};
450 
452  climate::ClimatePreset default_preset_{};
454  std::string default_custom_preset_{};
455 
459 };
460 
461 } // namespace thermostat
462 } // 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
ClimatePreset
Enum for all preset modes.
Definition: climate_mode.h:80
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:548
ClimateSwingMode
Enum for all modes a climate swing can be in.
Definition: climate_mode.h:68
void cooling_max_run_time_timer_callback_()
set_timeout() callbacks for various actions (see above)
uint8_t custom_preset
Definition: climate.h:546
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:151
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:70
The climate device is off.
Definition: climate_mode.h:12
ClimateFanMode fan_mode
Definition: climate.h:540
The fan mode is set to On.
Definition: climate_mode.h:48
Definition: a4988.cpp:4
Base-class for all sensors.
Definition: sensor.h:50
ClimatePreset preset
Definition: climate.h:545
ClimateDevice - This is the base class for all climate integrations.
Definition: climate.h:167