ESPHome 2026.2.2
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);
77 ClimateCall &set_fan_mode(const std::string &fan_mode);
83 ClimateCall &set_fan_mode(const char *custom_fan_mode, size_t len);
89 ClimateCall &set_swing_mode(const std::string &swing_mode);
95 ClimateCall &set_preset(const std::string &preset);
101 ClimateCall &set_preset(const char *custom_preset, size_t len);
102
103 void perform();
104
109
110 const optional<ClimateMode> &get_mode() const;
113 const optional<ClimatePreset> &get_preset() const;
114 StringRef get_custom_fan_mode() const { return StringRef::from_maybe_nullptr(this->custom_fan_mode_); }
115 StringRef get_custom_preset() const { return StringRef::from_maybe_nullptr(this->custom_preset_); }
116 bool has_custom_fan_mode() const { return this->custom_fan_mode_ != nullptr; }
117 bool has_custom_preset() const { return this->custom_preset_ != nullptr; }
118
119 protected:
120 void validate_();
121
131
132 private:
133 const char *custom_fan_mode_{nullptr};
134 const char *custom_preset_{nullptr};
135};
136
142 union {
145 };
147 union {
150 };
160
162 ClimateCall to_call(Climate *climate);
164 void apply(Climate *climate);
165} __attribute__((packed));
166
182class Climate : public EntityBase {
183 public:
185
191 void add_on_state_callback(std::function<void(Climate &)> &&callback);
192
199 void add_on_control_callback(std::function<void(ClimateCall &)> &&callback);
200
206
212 void publish_state();
213
220
221#ifdef USE_CLIMATE_VISUAL_OVERRIDES
222 void set_visual_min_temperature_override(float visual_min_temperature_override);
223 void set_visual_max_temperature_override(float visual_max_temperature_override);
224 void set_visual_temperature_step_override(float target, float current);
225 void set_visual_min_humidity_override(float visual_min_humidity_override);
226 void set_visual_max_humidity_override(float visual_max_humidity_override);
227#endif
228
230 bool has_custom_fan_mode() const { return this->custom_fan_mode_ != nullptr; }
231
233 bool has_custom_preset() const { return this->custom_preset_ != nullptr; }
234
237
240
241 union {
244 struct {
249 };
250 };
251
254
257
260
263
266
269
271 StringRef get_custom_fan_mode() const { return StringRef::from_maybe_nullptr(this->custom_fan_mode_); }
272
274 StringRef get_custom_preset() const { return StringRef::from_maybe_nullptr(this->custom_preset_); }
275
276 protected:
279
282
284 bool set_custom_fan_mode_(const char *mode) { return this->set_custom_fan_mode_(mode, strlen(mode)); }
285 bool set_custom_fan_mode_(const char *mode, size_t len);
286 bool set_custom_fan_mode_(StringRef mode) { return this->set_custom_fan_mode_(mode.c_str(), mode.size()); }
289
292
294 bool set_custom_preset_(const char *preset) { return this->set_custom_preset_(preset, strlen(preset)); }
295 bool set_custom_preset_(const char *preset, size_t len);
296 bool set_custom_preset_(StringRef preset) { return this->set_custom_preset_(preset.c_str(), preset.size()); }
299
301 const char *find_custom_fan_mode_(const char *custom_fan_mode);
302 const char *find_custom_fan_mode_(const char *custom_fan_mode, size_t len);
303
305 const char *find_custom_preset_(const char *custom_preset);
306 const char *find_custom_preset_(const char *custom_preset, size_t len);
307
314 virtual ClimateTraits traits() = 0;
315
324 virtual void control(const ClimateCall &call) = 0;
330 void save_state_();
331
332 void dump_traits_(const char *tag);
333
337#ifdef USE_CLIMATE_VISUAL_OVERRIDES
344#endif
345
346 private:
352 const char *custom_fan_mode_{nullptr};
353
359 const char *custom_preset_{nullptr};
361
362} // 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:306
optional< float > target_temperature_high_
Definition climate.h:125
bool has_custom_fan_mode() const
Definition climate.h:116
const optional< float > & get_target_humidity() const
Definition climate.cpp:302
ClimateCall & set_target_temperature(float target_temperature)
Set the target temperature of the climate device.
Definition climate.cpp:279
bool has_custom_preset() const
Definition climate.h:117
const optional< float > & get_target_temperature_low() const
Definition climate.cpp:300
ClimateCall & set_swing_mode(ClimateSwingMode swing_mode)
Set the swing mode of the climate device.
Definition climate.cpp:263
optional< ClimateFanMode > fan_mode_
Definition climate.h:128
ClimateCall & set_target_temperature_low(float target_temperature_low)
Set the low point target temperature of the climate device.
Definition climate.cpp:284
optional< float > target_temperature_
Definition climate.h:123
const optional< float > & get_target_temperature() const
Definition climate.cpp:299
const optional< ClimatePreset > & get_preset() const
Definition climate.cpp:307
optional< ClimateSwingMode > swing_mode_
Definition climate.h:129
optional< ClimateMode > mode_
Definition climate.h:127
ClimateCall & set_preset(ClimatePreset preset)
Set the preset of the climate device.
Definition climate.cpp:225
const optional< float > & get_target_temperature_high() const
Definition climate.cpp:301
const optional< ClimateFanMode > & get_fan_mode() const
Definition climate.cpp:305
optional< float > target_humidity_
Definition climate.h:126
ClimateCall & set_fan_mode(ClimateFanMode fan_mode)
Set the fan mode of the climate device.
Definition climate.cpp:187
StringRef get_custom_fan_mode() const
Definition climate.h:114
optional< ClimatePreset > preset_
Definition climate.h:130
ClimateCall & set_target_humidity(float target_humidity)
Set the target humidity of the climate device.
Definition climate.cpp:294
ClimateCall(Climate *parent)
Definition climate.h:35
optional< float > target_temperature_low_
Definition climate.h:124
StringRef get_custom_preset() const
Definition climate.h:115
ClimateCall & set_target_temperature_high(float target_temperature_high)
Set the high point target temperature of the climate device.
Definition climate.cpp:289
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:304
ClimateDevice - This is the base class for all climate integrations.
Definition climate.h:182
ClimateMode mode
The active mode of the climate device.
Definition climate.h:262
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:256
ClimateTraits get_traits()
Get the traits of this climate device with all overrides applied.
Definition climate.cpp:486
float target_temperature
The target temperature of the climate device.
Definition climate.h:243
float current_humidity
The current humidity of the climate device, as reported from the integration.
Definition climate.h:239
bool set_custom_fan_mode_(StringRef mode)
Definition climate.h:286
float visual_min_humidity_override_
Definition climate.h:342
float visual_target_temperature_step_override_
Definition climate.h:340
void set_visual_min_humidity_override(float visual_min_humidity_override)
Definition climate.cpp:523
void dump_traits_(const char *tag)
Definition climate.cpp:718
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition climate.h:268
void save_state_()
Internal method to save the state of the climate device to recover memory.
Definition climate.cpp:370
float target_temperature_low
The minimum target temperature of the climate device, for climate devices with split target temperatu...
Definition climate.h:246
void set_visual_max_humidity_override(float visual_max_humidity_override)
Definition climate.cpp:527
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:351
float visual_current_temperature_step_override_
Definition climate.h:341
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:692
bool set_custom_preset_(const char *preset)
Set custom preset. Reset primary preset. Return true if preset has been changed.
Definition climate.h:294
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:702
float visual_min_temperature_override_
Definition climate.h:338
void set_visual_max_temperature_override(float visual_max_temperature_override)
Definition climate.cpp:514
void clear_custom_preset_()
Clear custom preset.
Definition climate.cpp:700
bool set_fan_mode_(ClimateFanMode mode)
Set fan mode. Reset custom fan mode. Return true if fan mode has been changed.
Definition climate.cpp:680
LazyCallbackManager< void(Climate &)> state_callback_
Definition climate.h:334
bool has_custom_preset() const
Check if a custom preset is currently active.
Definition climate.h:233
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:710
void clear_custom_fan_mode_()
Clear custom fan mode.
Definition climate.cpp:690
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:355
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:236
float visual_max_humidity_override_
Definition climate.h:343
ClimateAction action
The active state of the climate device.
Definition climate.h:265
LazyCallbackManager< void(ClimateCall &)> control_callback_
Definition climate.h:335
ClimateCall make_call()
Make a climate device control call, this is used to control the climate device, see the ClimateCall d...
Definition climate.cpp:532
float visual_max_temperature_override_
Definition climate.h:339
StringRef get_custom_preset() const
Get the active custom preset (read-only access). Returns StringRef.
Definition climate.h:274
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:437
void set_visual_temperature_step_override(float target, float current)
Definition climate.cpp:518
bool has_custom_fan_mode() const
Check if a custom fan mode is currently active.
Definition climate.h:230
ESPPreferenceObject rtc_
Definition climate.h:336
optional< ClimatePreset > preset
The active preset of the climate device.
Definition climate.h:259
void set_visual_min_temperature_override(float visual_min_temperature_override)
Definition climate.cpp:510
bool set_custom_preset_(StringRef preset)
Definition climate.h:296
optional< ClimateDeviceRestoreState > restore_state_()
Restore the state of the climate device, call this from your setup() method.
Definition climate.cpp:362
float target_humidity
The target humidity of the climate device.
Definition climate.h:253
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:284
float target_temperature_high
The maximum target temperature of the climate device, for climate devices with split target temperatu...
Definition climate.h:248
StringRef get_custom_fan_mode() const
Get the active custom fan mode (read-only access). Returns StringRef.
Definition climate.h:271
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:692
Struct used to save the state of the climate device in restore memory.
Definition climate.h:139
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:534
void apply(Climate *climate)
Apply these settings to the climate device.
Definition climate.cpp:570