ESPHome  2023.11.6
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);
72  ClimateCall &set_fan_mode(const std::string &fan_mode);
80  ClimateCall &set_swing_mode(const std::string &swing_mode);
86  ClimateCall &set_preset(const std::string &preset);
89 
90  void perform();
91 
92  const optional<ClimateMode> &get_mode() const;
99  const optional<ClimatePreset> &get_preset() const;
101 
102  protected:
103  void validate_();
104 
105  Climate *const parent_;
115 };
116 
121  bool uses_custom_fan_mode{false};
122  union {
125  };
126  bool uses_custom_preset{false};
127  union {
129  uint8_t custom_preset;
130  };
132  union {
134  struct {
137  };
138  };
139 
141  ClimateCall to_call(Climate *climate);
143  void apply(Climate *climate);
144 } __attribute__((packed));
145 
161 class Climate : public EntityBase {
162  public:
168  float current_temperature{NAN};
169 
170  union {
173  struct {
178  };
179  };
180 
183 
186 
189 
192 
195 
201  void add_on_state_callback(std::function<void(Climate &)> &&callback);
202 
209  void add_on_control_callback(std::function<void(ClimateCall &)> &&callback);
210 
215  ClimateCall make_call();
216 
222  void publish_state();
223 
229  ClimateTraits get_traits();
230 
231  void set_visual_min_temperature_override(float visual_min_temperature_override);
232  void set_visual_max_temperature_override(float visual_max_temperature_override);
233  void set_visual_temperature_step_override(float target, float current);
234 
235  protected:
236  friend ClimateCall;
237 
239  bool set_fan_mode_(ClimateFanMode mode);
240 
242  bool set_custom_fan_mode_(const std::string &mode);
243 
245  bool set_preset_(ClimatePreset preset);
246 
248  bool set_custom_preset_(const std::string &preset);
249 
256  virtual ClimateTraits traits() = 0;
257 
266  virtual void control(const ClimateCall &call) = 0;
268  optional<ClimateDeviceRestoreState> restore_state_();
272  void save_state_();
273 
274  void dump_traits_(const char *tag);
275 
279  optional<float> visual_min_temperature_override_{};
280  optional<float> visual_max_temperature_override_{};
281  optional<float> visual_target_temperature_step_override_{};
282  optional<float> visual_current_temperature_step_override_{};
283 };
284 
285 } // namespace climate
286 } // 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:185
float target_temperature_low
Definition: climate.h:545
optional< std::string > custom_preset_
Definition: climate.h:114
ClimatePreset
Enum for all preset modes.
Definition: climate_mode.h:82
float target_temperature
The target temperature of the climate device.
Definition: climate.h:172
const optional< ClimateMode > & get_mode() const
Definition: climate.cpp:265
optional< float > target_temperature_
Definition: climate.h:107
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:119
optional< ClimateFanMode > fan_mode_
Definition: climate.h:110
const optional< float > & get_target_temperature_low() const
Definition: climate.cpp:267
float target_temperature_high
The maximum target temperature of the climate device, for climate devices with split target temperatu...
Definition: climate.h:177
optional< float > target_temperature_high_
Definition: climate.h:109
float target_temperature_high
Definition: climate.h:546
bool uses_custom_fan_mode
Definition: climate.h:531
ClimateCall & set_swing_mode(ClimateSwingMode swing_mode)
Set the swing mode of the climate device.
Definition: climate.cpp:234
ClimateSwingMode swing_mode
Definition: climate.h:541
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:188
ClimateCall & set_target_temperature_low(float target_temperature_low)
Set the low point target temperature of the climate device.
Definition: climate.cpp:257
const optional< std::string > & get_custom_preset() const
Definition: climate.cpp:272
ClimateCall & set_target_temperature(float target_temperature)
Set the target temperature of the climate device.
Definition: climate.cpp:253
optional< ClimateSwingMode > swing_mode_
Definition: climate.h:111
const optional< ClimatePreset > & get_preset() const
Definition: climate.cpp:271
optional< ClimatePreset > preset
The active preset of the climate device.
Definition: climate.h:191
optional< ClimatePreset > preset_
Definition: climate.h:113
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:270
const optional< float > & get_target_temperature() const
Definition: climate.cpp:266
optional< std::string > custom_fan_mode_
Definition: climate.h:112
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:533
optional< std::string > custom_preset
The active custom preset mode of the climate device.
Definition: climate.h:194
ClimateCall & set_target_temperature_high(float target_temperature_high)
Set the high point target temperature of the climate device.
Definition: climate.cpp:261
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition: climate.h:182
ESPPreferenceObject rtc_
Definition: climate.h:278
const optional< ClimateFanMode > & get_fan_mode() const
Definition: climate.cpp:269
ClimateCall & set_mode(ClimateMode mode)
Set the mode of the climate device.
Definition: climate.cpp:130
ClimateMode mode
Definition: climate.h:530
bool uses_custom_preset
Definition: climate.h:536
const optional< ClimateSwingMode > & get_swing_mode() const
Definition: climate.cpp:273
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
optional< ClimateMode > mode_
Definition: climate.h:106
ClimateCall(Climate *parent)
Definition: climate.h:35
const optional< float > & get_target_temperature_high() const
Definition: climate.cpp:268
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:108
float target_temperature
Definition: climate.h:543
float target_temperature_low
The minimum target temperature of the climate device, for climate devices with split target temperatu...
Definition: climate.h:175
ClimatePreset preset
Definition: climate.h:538
ClimateDevice - This is the base class for all climate integrations.
Definition: climate.h:161
Climate *const parent_
Definition: climate.h:105