ESPHome  2023.3.2
climate.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/helpers.h"
7 #include "esphome/core/log.h"
8 #include "climate_mode.h"
9 #include "climate_traits.h"
10 
11 namespace esphome {
12 namespace climate {
13 
14 #define LOG_CLIMATE(prefix, type, obj) \
15  if ((obj) != nullptr) { \
16  ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
17  }
18 
19 class Climate;
20 
33 class ClimateCall {
34  public:
35  explicit ClimateCall(Climate *parent) : parent_(parent) {}
36 
42  ClimateCall &set_mode(const std::string &mode);
67  ESPDEPRECATED("set_away() is deprecated, please use .set_preset(CLIMATE_PRESET_AWAY) instead", "v1.20")
68  ClimateCall &set_away(bool away);
69  ESPDEPRECATED("set_away() is deprecated, please use .set_preset(CLIMATE_PRESET_AWAY) instead", "v1.20")
70  ClimateCall &set_away(optional<bool> away);
76  ClimateCall &set_fan_mode(const std::string &fan_mode);
84  ClimateCall &set_swing_mode(const std::string &swing_mode);
90  ClimateCall &set_preset(const std::string &preset);
93 
94  void perform();
95 
96  const optional<ClimateMode> &get_mode() const;
100  ESPDEPRECATED("get_away() is deprecated, please use .get_preset() instead", "v1.20")
101  optional<bool> get_away() const;
102  const optional<ClimateFanMode> &get_fan_mode() const;
105  const optional<ClimatePreset> &get_preset() const;
107 
108  protected:
109  void validate_();
110 
111  Climate *const parent_;
121 };
122 
127  bool uses_custom_fan_mode{false};
128  union {
131  };
132  bool uses_custom_preset{false};
133  union {
135  uint8_t custom_preset;
136  };
138  union {
140  struct {
143  };
144  };
145 
147  ClimateCall to_call(Climate *climate);
149  void apply(Climate *climate);
150 } __attribute__((packed));
151 
167 class Climate : public EntityBase {
168  public:
170  Climate();
172  Climate(const std::string &name);
173 
179  float current_temperature{NAN};
180 
181  union {
184  struct {
189  };
190  };
191 
197  ESPDEPRECATED("away is deprecated, use preset instead", "v1.20")
198  bool away{false};
199 
202 
205 
208 
211 
214 
220  void add_on_state_callback(std::function<void()> &&callback);
221 
228  void add_on_control_callback(std::function<void()> &&callback);
229 
234  ClimateCall make_call();
235 
241  void publish_state();
242 
248  ClimateTraits get_traits();
249 
250  void set_visual_min_temperature_override(float visual_min_temperature_override);
251  void set_visual_max_temperature_override(float visual_max_temperature_override);
252  void set_visual_temperature_step_override(float target, float current);
253 
254  protected:
255  friend ClimateCall;
256 
258  bool set_fan_mode_(ClimateFanMode mode);
259 
261  bool set_custom_fan_mode_(const std::string &mode);
262 
264  bool set_preset_(ClimatePreset preset);
265 
267  bool set_custom_preset_(const std::string &preset);
268 
275  virtual ClimateTraits traits() = 0;
276 
285  virtual void control(const ClimateCall &call) = 0;
287  optional<ClimateDeviceRestoreState> restore_state_();
291  void save_state_();
292 
293  void dump_traits_(const char *tag);
294 
295  CallbackManager<void()> state_callback_{};
296  CallbackManager<void()> control_callback_{};
298  optional<float> visual_min_temperature_override_{};
299  optional<float> visual_max_temperature_override_{};
300  optional<float> visual_target_temperature_step_override_{};
301  optional<float> visual_current_temperature_step_override_{};
302 };
303 
304 } // namespace climate
305 } // namespace esphome
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
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition: climate.h:204
float target_temperature_low
Definition: climate.h:562
const char * name
Definition: stm32flash.h:78
optional< std::string > custom_preset_
Definition: climate.h:120
ClimatePreset
Enum for all preset modes.
Definition: climate_mode.h:82
float target_temperature
The target temperature of the climate device.
Definition: climate.h:183
const optional< ClimateMode > & get_mode() const
Definition: climate.cpp:263
optional< float > target_temperature_
Definition: climate.h:113
This class contains all static data for climate devices.
void apply(Climate *climate)
Apply these settings to the climate device.
Struct used to save the state of the climate device in restore memory.
Definition: climate.h:125
optional< ClimateFanMode > fan_mode_
Definition: climate.h:116
const optional< float > & get_target_temperature_low() const
Definition: climate.cpp:265
float target_temperature_high
The maximum target temperature of the climate device, for climate devices with split target temperatu...
Definition: climate.h:188
optional< float > target_temperature_high_
Definition: climate.h:115
float target_temperature_high
Definition: climate.h:563
bool uses_custom_fan_mode
Definition: climate.h:548
ClimateCall & set_swing_mode(ClimateSwingMode swing_mode)
Set the swing mode of the climate device.
Definition: climate.cpp:232
ClimateSwingMode swing_mode
Definition: climate.h:558
ClimateSwingMode
Enum for all modes a climate swing can be in.
Definition: climate_mode.h:70
optional< std::string > custom_fan_mode
The active custom fan mode of the climate device.
Definition: climate.h:207
ClimateCall & set_target_temperature_low(float target_temperature_low)
Set the low point target temperature of the climate device.
Definition: climate.cpp:255
const optional< std::string > & get_custom_preset() const
Definition: climate.cpp:275
ClimateCall & set_target_temperature(float target_temperature)
Set the target temperature of the climate device.
Definition: climate.cpp:251
optional< ClimateSwingMode > swing_mode_
Definition: climate.h:117
const optional< ClimatePreset > & get_preset() const
Definition: climate.cpp:274
ESPDEPRECATED("away is deprecated, use preset instead", "v1.20") bool away
Whether the climate device is in away mode.
Definition: climate.h:197
optional< ClimatePreset > preset
The active preset of the climate device.
Definition: climate.h:210
optional< ClimatePreset > preset_
Definition: climate.h:119
ClimateCall & set_preset(ClimatePreset preset)
Set the preset of the climate device.
Definition: climate.cpp:196
ClimateAction
Enum for the current action of the climate device. Values match those of ClimateMode.
Definition: climate_mode.h:31
ClimateCall & set_fan_mode(ClimateFanMode fan_mode)
Set the fan mode of the climate device.
Definition: climate.cpp:154
const optional< std::string > & get_custom_fan_mode() const
Definition: climate.cpp:273
const optional< float > & get_target_temperature() const
Definition: climate.cpp:264
optional< std::string > custom_fan_mode_
Definition: climate.h:118
ClimateMode
Enum for all modes a climate device can be in.
Definition: climate_mode.h:10
The climate device is off.
Definition: climate_mode.h:12
ClimateFanMode fan_mode
Definition: climate.h:550
optional< std::string > custom_preset
The active custom preset mode of the climate device.
Definition: climate.h:213
ClimateCall & set_target_temperature_high(float target_temperature_high)
Set the high point target temperature of the climate device.
Definition: climate.cpp:259
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition: climate.h:198
ESPPreferenceObject rtc_
Definition: climate.h:297
const optional< ClimateFanMode > & get_fan_mode() const
Definition: climate.cpp:272
ClimateCall & set_mode(ClimateMode mode)
Set the mode of the climate device.
Definition: climate.cpp:130
ClimateMode mode
Definition: climate.h:547
bool uses_custom_preset
Definition: climate.h:553
const optional< ClimateSwingMode > & get_swing_mode() const
Definition: climate.cpp:276
Definition: a4988.cpp:4
optional< ClimateMode > mode_
Definition: climate.h:112
ClimateCall(Climate *parent)
Definition: climate.h:35
const optional< float > & get_target_temperature_high() const
Definition: climate.cpp:266
ESPDEPRECATED("set_away() is deprecated, please use .set_preset(CLIMATE_PRESET_AWAY) instead", "v1.20") ClimateCall &set_away(bool away)
ClimateCall to_call(Climate *climate)
Convert this struct to a climate call that can be performed.
esphome::climate::Climate __attribute__
optional< float > target_temperature_low_
Definition: climate.h:114
float target_temperature
Definition: climate.h:560
float target_temperature_low
The minimum target temperature of the climate device, for climate devices with split target temperatu...
Definition: climate.h:186
ClimatePreset preset
Definition: climate.h:555
ClimateDevice - This is the base class for all climate integrations.
Definition: climate.h:167
Climate *const parent_
Definition: climate.h:111