ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
climate.h
Go to the documentation of this file.
1#pragma once
9#include "climate_mode.h"
10#include "climate_traits.h"
12namespace esphome::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
19class Climate;
20
34 public:
35 explicit ClimateCall(Climate *parent) : parent_(parent) {}
37
43 ClimateCall &set_mode(const std::string &mode);
45 ClimateCall &set_mode(const char *mode, size_t len);
79 ClimateCall &set_fan_mode(const std::string &fan_mode);
85 ClimateCall &set_fan_mode(const char *custom_fan_mode, size_t len);
91 ClimateCall &set_swing_mode(const std::string &swing_mode);
93 ClimateCall &set_swing_mode(const char *swing_mode, size_t len);
99 ClimateCall &set_preset(const std::string &preset);
105 ClimateCall &set_preset(const char *custom_preset, size_t len);
106
107 void perform();
108
113
114 const optional<ClimateMode> &get_mode() const;
117 const optional<ClimatePreset> &get_preset() const;
118 StringRef get_custom_fan_mode() const { return StringRef::from_maybe_nullptr(this->custom_fan_mode_); }
119 StringRef get_custom_preset() const { return StringRef::from_maybe_nullptr(this->custom_preset_); }
120 bool has_custom_fan_mode() const { return this->custom_fan_mode_ != nullptr; }
121 bool has_custom_preset() const { return this->custom_preset_ != nullptr; }
122
123 protected:
124 void validate_();
125
135
136 private:
137 const char *custom_fan_mode_{nullptr};
138 const char *custom_preset_{nullptr};
139};
140
146 union {
149 };
151 union {
154 };
164
166 ClimateCall to_call(Climate *climate);
168 void apply(Climate *climate);
169} __attribute__((packed));
170
186class Climate : public EntityBase {
187 public:
189
195 void add_on_state_callback(std::function<void(Climate &)> &&callback);
196
203 void add_on_control_callback(std::function<void(ClimateCall &)> &&callback);
204
210
216 void publish_state();
217
224
225#ifdef USE_CLIMATE_VISUAL_OVERRIDES
226 void set_visual_min_temperature_override(float visual_min_temperature_override);
227 void set_visual_max_temperature_override(float visual_max_temperature_override);
228 void set_visual_temperature_step_override(float target, float current);
229 void set_visual_min_humidity_override(float visual_min_humidity_override);
230 void set_visual_max_humidity_override(float visual_max_humidity_override);
231#endif
232
234 bool has_custom_fan_mode() const { return this->custom_fan_mode_ != nullptr; }
235
237 bool has_custom_preset() const { return this->custom_preset_ != nullptr; }
238
241
244
245 union {
248 struct {
253 };
254 };
255
258
261
264
267
270
273
275 StringRef get_custom_fan_mode() const { return StringRef::from_maybe_nullptr(this->custom_fan_mode_); }
276
278 StringRef get_custom_preset() const { return StringRef::from_maybe_nullptr(this->custom_preset_); }
279
280 protected:
283
286
288 bool set_custom_fan_mode_(const char *mode) { return this->set_custom_fan_mode_(mode, strlen(mode)); }
289 bool set_custom_fan_mode_(const char *mode, size_t len);
290 bool set_custom_fan_mode_(StringRef mode) { return this->set_custom_fan_mode_(mode.c_str(), mode.size()); }
293
296
298 bool set_custom_preset_(const char *preset) { return this->set_custom_preset_(preset, strlen(preset)); }
299 bool set_custom_preset_(const char *preset, size_t len);
300 bool set_custom_preset_(StringRef preset) { return this->set_custom_preset_(preset.c_str(), preset.size()); }
303
305 const char *find_custom_fan_mode_(const char *custom_fan_mode);
306 const char *find_custom_fan_mode_(const char *custom_fan_mode, size_t len);
307
309 const char *find_custom_preset_(const char *custom_preset);
310 const char *find_custom_preset_(const char *custom_preset, size_t len);
311
318 virtual ClimateTraits traits() = 0;
319
328 virtual void control(const ClimateCall &call) = 0;
334 void save_state_();
335
336 void dump_traits_(const char *tag);
337
341#ifdef USE_CLIMATE_VISUAL_OVERRIDES
348#endif
349
350 private:
356 const char *custom_fan_mode_{nullptr};
357
363 const char *custom_preset_{nullptr};
365
366} // namespace esphome::climate
BedjetMode mode
BedJet operating mode.
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
constexpr const char * c_str() const
Definition string_ref.h:73
static StringRef from_maybe_nullptr(const char *s)
Definition string_ref.h:53
This class is used to encode all control actions on a climate device.
Definition climate.h:33
const optional< ClimateSwingMode > & get_swing_mode() const
Definition climate.cpp:314
optional< float > target_temperature_high_
Definition climate.h:129
bool has_custom_fan_mode() const
Definition climate.h:120
const optional< float > & get_target_humidity() const
Definition climate.cpp:310
ClimateCall & set_target_temperature(float target_temperature)
Set the target temperature of the climate device.
Definition climate.cpp:287
bool has_custom_preset() const
Definition climate.h:121
const optional< float > & get_target_temperature_low() const
Definition climate.cpp:308
ClimateCall & set_swing_mode(ClimateSwingMode swing_mode)
Set the swing mode of the climate device.
Definition climate.cpp:266
optional< ClimateFanMode > fan_mode_
Definition climate.h:132
ClimateCall & set_target_temperature_low(float target_temperature_low)
Set the low point target temperature of the climate device.
Definition climate.cpp:292
optional< float > target_temperature_
Definition climate.h:127
const optional< float > & get_target_temperature() const
Definition climate.cpp:307
const optional< ClimatePreset > & get_preset() const
Definition climate.cpp:315
optional< ClimateSwingMode > swing_mode_
Definition climate.h:133
optional< ClimateMode > mode_
Definition climate.h:131
ClimateCall & set_preset(ClimatePreset preset)
Set the preset of the climate device.
Definition climate.cpp:228
const optional< float > & get_target_temperature_high() const
Definition climate.cpp:309
const optional< ClimateFanMode > & get_fan_mode() const
Definition climate.cpp:313
optional< float > target_humidity_
Definition climate.h:130
ClimateCall & set_fan_mode(ClimateFanMode fan_mode)
Set the fan mode of the climate device.
Definition climate.cpp:190
StringRef get_custom_fan_mode() const
Definition climate.h:118
optional< ClimatePreset > preset_
Definition climate.h:134
ClimateCall & set_target_humidity(float target_humidity)
Set the target humidity of the climate device.
Definition climate.cpp:302
ClimateCall(Climate *parent)
Definition climate.h:35
optional< float > target_temperature_low_
Definition climate.h:128
StringRef get_custom_preset() const
Definition climate.h:119
ClimateCall & set_target_temperature_high(float target_temperature_high)
Set the high point target temperature of the climate device.
Definition climate.cpp:297
ClimateCall & set_mode(ClimateMode mode)
Set the mode of the climate device.
Definition climate.cpp:171
const optional< ClimateMode > & get_mode() const
Definition climate.cpp:312
ClimateDevice - This is the base class for all climate integrations.
Definition climate.h:186
ClimateMode mode
The active mode of the climate device.
Definition climate.h:266
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:260
ClimateTraits get_traits()
Get the traits of this climate device with all overrides applied.
Definition climate.cpp:494
float target_temperature
The target temperature of the climate device.
Definition climate.h:247
float current_humidity
The current humidity of the climate device, as reported from the integration.
Definition climate.h:243
bool set_custom_fan_mode_(StringRef mode)
Definition climate.h:290
float visual_min_humidity_override_
Definition climate.h:346
float visual_target_temperature_step_override_
Definition climate.h:344
void set_visual_min_humidity_override(float visual_min_humidity_override)
Definition climate.cpp:531
void dump_traits_(const char *tag)
Definition climate.cpp:726
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition climate.h:272
void save_state_()
Internal method to save the state of the climate device to recover memory.
Definition climate.cpp:378
float target_temperature_low
The minimum target temperature of the climate device, for climate devices with split target temperatu...
Definition climate.h:250
void set_visual_max_humidity_override(float visual_max_humidity_override)
Definition climate.cpp:535
void add_on_state_callback(std::function< void(Climate &)> &&callback)
Add a callback for the climate device state, each time the state of the climate device is updated (us...
Definition climate.cpp:359
float visual_current_temperature_step_override_
Definition climate.h:345
virtual ClimateTraits traits()=0
Get the default traits of this climate device.
bool set_preset_(ClimatePreset preset)
Set preset. Reset custom preset. Return true if preset has been changed.
Definition climate.cpp:700
bool set_custom_preset_(const char *preset)
Set custom preset. Reset primary preset. Return true if preset has been changed.
Definition climate.h:298
const char * find_custom_fan_mode_(const char *custom_fan_mode)
Find and return the matching custom fan mode pointer from traits, or nullptr if not found.
Definition climate.cpp:710
float visual_min_temperature_override_
Definition climate.h:342
void set_visual_max_temperature_override(float visual_max_temperature_override)
Definition climate.cpp:522
void clear_custom_preset_()
Clear custom preset.
Definition climate.cpp:708
bool set_fan_mode_(ClimateFanMode mode)
Set fan mode. Reset custom fan mode. Return true if fan mode has been changed.
Definition climate.cpp:688
LazyCallbackManager< void(Climate &)> state_callback_
Definition climate.h:338
bool has_custom_preset() const
Check if a custom preset is currently active.
Definition climate.h:237
const char * find_custom_preset_(const char *custom_preset)
Find and return the matching custom preset pointer from traits, or nullptr if not found.
Definition climate.cpp:718
void clear_custom_fan_mode_()
Clear custom fan mode.
Definition climate.cpp:698
void add_on_control_callback(std::function< void(ClimateCall &)> &&callback)
Add a callback for the climate device configuration; each time the configuration parameters of a clim...
Definition climate.cpp:363
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:240
float visual_max_humidity_override_
Definition climate.h:347
ClimateAction action
The active state of the climate device.
Definition climate.h:269
LazyCallbackManager< void(ClimateCall &)> control_callback_
Definition climate.h:339
ClimateCall make_call()
Make a climate device control call, this is used to control the climate device, see the ClimateCall d...
Definition climate.cpp:540
float visual_max_temperature_override_
Definition climate.h:343
StringRef get_custom_preset() const
Get the active custom preset (read-only access). Returns StringRef.
Definition climate.h:278
virtual void control(const ClimateCall &call)=0
Control the climate device, this is a virtual method that each climate integration must implement.
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:445
void set_visual_temperature_step_override(float target, float current)
Definition climate.cpp:526
bool has_custom_fan_mode() const
Check if a custom fan mode is currently active.
Definition climate.h:234
ESPPreferenceObject rtc_
Definition climate.h:340
optional< ClimatePreset > preset
The active preset of the climate device.
Definition climate.h:263
void set_visual_min_temperature_override(float visual_min_temperature_override)
Definition climate.cpp:518
bool set_custom_preset_(StringRef preset)
Definition climate.h:300
optional< ClimateDeviceRestoreState > restore_state_()
Restore the state of the climate device, call this from your setup() method.
Definition climate.cpp:370
float target_humidity
The target humidity of the climate device.
Definition climate.h:257
bool set_custom_fan_mode_(const char *mode)
Set custom fan mode. Reset primary fan mode. Return true if fan mode has been changed.
Definition climate.h:288
float target_temperature_high
The maximum target temperature of the climate device, for climate devices with split target temperatu...
Definition climate.h:252
StringRef get_custom_fan_mode() const
Get the active custom fan mode (read-only access). Returns StringRef.
Definition climate.h:275
float target_temperature_high
Definition climate.h:3
float target_humidity
Definition climate.h:19
ClimateSwingMode swing_mode
Definition climate.h:11
float target_temperature
Definition climate.h:0
uint8_t custom_preset
Definition climate.h:9
ClimateFanMode fan_mode
Definition climate.h:3
ClimatePreset preset
Definition climate.h:8
float target_temperature_low
Definition climate.h:2
uint8_t custom_fan_mode
Definition climate.h:4
ClimatePreset
Enum for all preset modes NOTE: If adding values, update ClimatePresetMask in climate_traits....
ClimateSwingMode
Enum for all modes a climate swing can be in NOTE: If adding values, update ClimateSwingModeMask in c...
@ CLIMATE_SWING_OFF
The swing mode is set to Off.
ClimateMode
Enum for all modes a climate device can be in.
@ CLIMATE_MODE_OFF
The climate device is off.
esphome::climate::Climate __attribute__
ClimateAction
Enum for the current action of the climate device. Values match those of ClimateMode.
@ CLIMATE_ACTION_OFF
The climate device is off (inactive or no power)
ClimateFanMode
NOTE: If adding values, update ClimateFanModeMask in climate_traits.h to use the new last value.
std::string size_t len
Definition helpers.h:817
Struct used to save the state of the climate device in restore memory.
Definition climate.h:143
struct esphome::climate::ClimateDeviceRestoreState::@57::@58 __attribute__
ClimateCall to_call(Climate *climate)
Convert this struct to a climate call that can be performed.
Definition climate.cpp:542
void apply(Climate *climate)
Apply these settings to the climate device.
Definition climate.cpp:578