ESPHome  2024.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);
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  const optional<float> &get_target_humidity() const;
101  const optional<ClimateFanMode> &get_fan_mode() const;
104  const optional<ClimatePreset> &get_preset() const;
106 
107  protected:
108  void validate_();
109 
110  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  };
146 
148  ClimateCall to_call(Climate *climate);
150  void apply(Climate *climate);
151 } __attribute__((packed));
152 
168 class Climate : public EntityBase {
169  public:
170  Climate() {}
171 
174 
177 
179  float current_temperature{NAN};
180 
182  float current_humidity{NAN};
183 
184  union {
187  struct {
192  };
193  };
194 
197 
200 
203 
206 
209 
212 
218  void add_on_state_callback(std::function<void(Climate &)> &&callback);
219 
226  void add_on_control_callback(std::function<void(ClimateCall &)> &&callback);
227 
232  ClimateCall make_call();
233 
239  void publish_state();
240 
246  ClimateTraits get_traits();
247 
248  void set_visual_min_temperature_override(float visual_min_temperature_override);
249  void set_visual_max_temperature_override(float visual_max_temperature_override);
250  void set_visual_temperature_step_override(float target, float current);
251  void set_visual_min_humidity_override(float visual_min_humidity_override);
252  void set_visual_max_humidity_override(float visual_max_humidity_override);
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 
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  optional<float> visual_min_humidity_override_{};
303  optional<float> visual_max_humidity_override_{};
304 };
305 
306 } // namespace climate
307 } // 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:202
float target_temperature_low
Definition: climate.h:585
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:186
const optional< ClimateMode > & get_mode() const
Definition: climate.cpp:273
optional< float > target_temperature_
Definition: climate.h:112
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:275
optional< float > target_temperature_high_
Definition: climate.h:114
float target_temperature_high
Definition: climate.h:586
bool uses_custom_fan_mode
Definition: climate.h:571
float target_humidity
The target humidity of the climate device.
Definition: climate.h:196
ClimateCall & set_swing_mode(ClimateSwingMode swing_mode)
Set the swing mode of the climate device.
Definition: climate.cpp:237
float target_humidity
Definition: climate.h:589
ClimateSwingMode swing_mode
Definition: climate.h:581
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:205
ClimateCall & set_target_temperature_low(float target_temperature_low)
Set the low point target temperature of the climate device.
Definition: climate.cpp:260
const optional< std::string > & get_custom_preset() const
Definition: climate.cpp:281
ClimateCall & set_target_temperature(float target_temperature)
Set the target temperature of the climate device.
Definition: climate.cpp:256
optional< ClimateSwingMode > swing_mode_
Definition: climate.h:117
const optional< ClimatePreset > & get_preset() const
Definition: climate.cpp:280
optional< ClimatePreset > preset
The active preset of the climate device.
Definition: climate.h:208
optional< ClimatePreset > preset_
Definition: climate.h:119
ClimateCall & set_preset(ClimatePreset preset)
Set the preset of the climate device.
Definition: climate.cpp:199
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:157
const optional< std::string > & get_custom_fan_mode() const
Definition: climate.cpp:279
const optional< float > & get_target_temperature() const
Definition: climate.cpp:274
optional< std::string > custom_fan_mode_
Definition: climate.h:118
ClimateCall & set_target_humidity(float target_humidity)
Set the target humidity of the climate device.
Definition: climate.cpp:268
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:573
optional< std::string > custom_preset
The active custom preset mode of the climate device.
Definition: climate.h:211
ClimateCall & set_target_temperature_high(float target_temperature_high)
Set the high point target temperature of the climate device.
Definition: climate.cpp:264
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition: climate.h:199
ESPPreferenceObject rtc_
Definition: climate.h:297
const optional< ClimateFanMode > & get_fan_mode() const
Definition: climate.cpp:278
optional< float > target_humidity_
Definition: climate.h:115
ClimateCall & set_mode(ClimateMode mode)
Set the mode of the climate device.
Definition: climate.cpp:133
ClimateMode mode
Definition: climate.h:570
bool uses_custom_preset
Definition: climate.h:576
const optional< ClimateSwingMode > & get_swing_mode() const
Definition: climate.cpp:282
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
optional< ClimateMode > mode_
Definition: climate.h:111
ClimateCall(Climate *parent)
Definition: climate.h:35
const optional< float > & get_target_temperature_high() const
Definition: climate.cpp:276
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:113
float target_temperature
Definition: climate.h:583
const optional< float > & get_target_humidity() const
Definition: climate.cpp:277
ClimatePreset preset
Definition: climate.h:578
ClimateDevice - This is the base class for all climate integrations.
Definition: climate.h:168
Climate *const parent_
Definition: climate.h:110