ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
climate.h
Go to the documentation of this file.
1#pragma once
3#include <vector>
7#include "esphome/core/log.h"
10#include "climate_mode.h"
12
13namespace esphome::climate {
14
15#define LOG_CLIMATE(prefix, type, obj) log_entity_header(TAG, prefix, LOG_STR_LITERAL(type), (obj))
16
17class Climate;
18
32 public:
33 explicit ClimateCall(Climate *parent) : parent_(parent) {}
35
39 ClimateCall &set_mode(optional<ClimateMode> mode);
41 ClimateCall &set_mode(const std::string &mode);
43 ClimateCall &set_mode(const char *mode, size_t len);
75 ClimateCall &set_fan_mode(optional<ClimateFanMode> fan_mode);
77 ClimateCall &set_fan_mode(const std::string &fan_mode);
79 ClimateCall &set_fan_mode(optional<std::string> fan_mode);
84 return this->set_fan_mode(custom_fan_mode.c_str(), custom_fan_mode.size());
85 }
87 ClimateCall &set_fan_mode(const char *custom_fan_mode, size_t len);
91 ClimateCall &set_swing_mode(optional<ClimateSwingMode> swing_mode);
93 ClimateCall &set_swing_mode(const std::string &swing_mode);
95 ClimateCall &set_swing_mode(const char *swing_mode, size_t len);
99 ClimateCall &set_preset(optional<ClimatePreset> preset);
101 ClimateCall &set_preset(const std::string &preset);
103 ClimateCall &set_preset(optional<std::string> preset);
108 return this->set_preset(custom_preset.c_str(), custom_preset.size());
109 }
111 ClimateCall &set_preset(const char *custom_preset, size_t len);
112
113 void perform();
114
115 const optional<float> &get_target_temperature() const;
116 const optional<float> &get_target_temperature_low() const;
117 const optional<float> &get_target_temperature_high() const;
118 const optional<float> &get_target_humidity() const;
119
120 const optional<ClimateMode> &get_mode() const;
121 const optional<ClimateFanMode> &get_fan_mode() const;
122 const optional<ClimateSwingMode> &get_swing_mode() const;
123 const optional<ClimatePreset> &get_preset() const;
124 StringRef get_custom_fan_mode() const { return StringRef::from_maybe_nullptr(this->custom_fan_mode_); }
125 StringRef get_custom_preset() const { return StringRef::from_maybe_nullptr(this->custom_preset_); }
126 bool has_custom_fan_mode() const { return this->custom_fan_mode_ != nullptr; }
127 bool has_custom_preset() const { return this->custom_preset_ != nullptr; }
128
129 protected:
130 void validate_();
131
133 optional<float> target_temperature_;
134 optional<float> target_temperature_low_;
136 optional<float> target_humidity_;
137 optional<ClimateMode> mode_;
138 optional<ClimateFanMode> fan_mode_;
139 optional<ClimateSwingMode> swing_mode_;
140 optional<ClimatePreset> preset_;
141
142 private:
143 const char *custom_fan_mode_{nullptr};
144 const char *custom_preset_{nullptr};
145};
146
152 union {
155 };
157 union {
160 };
170
172 ClimateCall to_call(Climate *climate);
174 void apply(Climate *climate);
175} __attribute__((packed));
176
192class Climate : public EntityBase {
193 public:
195
201 template<typename F> void add_on_state_callback(F &&callback) {
202 this->state_callback_.add(std::forward<F>(callback));
203 }
204
211 template<typename F> void add_on_control_callback(F &&callback) {
212 this->control_callback_.add(std::forward<F>(callback));
213 }
214
220
226 void publish_state();
227
234
235#ifdef USE_CLIMATE_VISUAL_OVERRIDES
236 void set_visual_min_temperature_override(float visual_min_temperature_override) {
237 this->visual_min_temperature_override_ = visual_min_temperature_override;
238 }
239 void set_visual_max_temperature_override(float visual_max_temperature_override) {
240 this->visual_max_temperature_override_ = visual_max_temperature_override;
241 }
242 void set_visual_temperature_step_override(float target, float current) {
245 }
246 void set_visual_min_humidity_override(float visual_min_humidity_override) {
247 this->visual_min_humidity_override_ = visual_min_humidity_override;
248 }
249 void set_visual_max_humidity_override(float visual_max_humidity_override) {
250 this->visual_max_humidity_override_ = visual_max_humidity_override;
251 }
252#endif
253
255 void set_supported_custom_fan_modes(std::initializer_list<const char *> modes) {
256 this->supported_custom_fan_modes_.assign_copy(modes.begin(), modes.size());
257 }
258 void set_supported_custom_fan_modes(const std::vector<const char *> &modes) {
259 this->supported_custom_fan_modes_.assign_copy(modes.data(), modes.size());
260 }
261 template<size_t N> void set_supported_custom_fan_modes(const char *const (&modes)[N]) {
262 this->supported_custom_fan_modes_.assign_copy(modes, N);
263 }
265 void set_supported_custom_fan_modes_static(const char *const *modes, size_t count) {
266 this->supported_custom_fan_modes_.assign_static(modes, count);
267 }
268
270 void set_supported_custom_presets(std::initializer_list<const char *> presets) {
271 this->supported_custom_presets_.assign_copy(presets.begin(), presets.size());
272 }
273 void set_supported_custom_presets(const std::vector<const char *> &presets) {
274 this->supported_custom_presets_.assign_copy(presets.data(), presets.size());
275 }
276 template<size_t N> void set_supported_custom_presets(const char *const (&presets)[N]) {
277 this->supported_custom_presets_.assign_copy(presets, N);
278 }
280 void set_supported_custom_presets_static(const char *const *presets, size_t count) {
281 this->supported_custom_presets_.assign_static(presets, count);
282 }
283
285 bool has_custom_fan_mode() const { return this->custom_fan_mode_ != nullptr; }
286
288 bool has_custom_preset() const { return this->custom_preset_ != nullptr; }
289
292
295
296 union {
299 struct {
304 };
305 };
306
309
311 optional<ClimateFanMode> fan_mode;
312
314 optional<ClimatePreset> preset;
315
318
321
324
326 StringRef get_custom_fan_mode() const { return StringRef::from_maybe_nullptr(this->custom_fan_mode_); }
327
329 StringRef get_custom_preset() const { return StringRef::from_maybe_nullptr(this->custom_preset_); }
330
331 protected:
334
337
339 bool set_custom_fan_mode_(const char *mode) { return this->set_custom_fan_mode_(mode, strlen(mode)); }
340 bool set_custom_fan_mode_(const char *mode, size_t len);
341 bool set_custom_fan_mode_(StringRef mode) { return this->set_custom_fan_mode_(mode.c_str(), mode.size()); }
344
347
349 bool set_custom_preset_(const char *preset) { return this->set_custom_preset_(preset, strlen(preset)); }
350 bool set_custom_preset_(const char *preset, size_t len);
351 bool set_custom_preset_(StringRef preset) { return this->set_custom_preset_(preset.c_str(), preset.size()); }
354
356 const char *find_custom_fan_mode_(const char *custom_fan_mode);
357 const char *find_custom_fan_mode_(const char *custom_fan_mode, size_t len);
358
360 const char *find_custom_preset_(const char *custom_preset);
361 const char *find_custom_preset_(const char *custom_preset, size_t len);
362
369 virtual ClimateTraits traits() = 0;
370
379 virtual void control(const ClimateCall &call) = 0;
381 optional<ClimateDeviceRestoreState> restore_state_();
385 void save_state_(const ClimateTraits &traits);
386 void save_state_() { this->save_state_(this->get_traits()); }
387
388 void dump_traits_(const char *tag);
389
393
394#ifdef USE_CLIMATE_VISUAL_OVERRIDES
401#endif
402
403 private:
404 ClimateCustomModes supported_custom_fan_modes_;
405 ClimateCustomModes supported_custom_presets_;
406
412 const char *custom_fan_mode_{nullptr};
413
419 const char *custom_preset_{nullptr};
421
422} // namespace esphome::climate
BedjetMode mode
BedJet operating mode.
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
constexpr const char * c_str() const
Definition string_ref.h:78
static StringRef from_maybe_nullptr(const char *s)
Definition string_ref.h:58
This class is used to encode all control actions on a climate device.
Definition climate.h:31
const optional< ClimateSwingMode > & get_swing_mode() const
Definition climate.cpp:317
optional< float > target_temperature_high_
Definition climate.h:135
bool has_custom_fan_mode() const
Definition climate.h:126
const optional< float > & get_target_humidity() const
Definition climate.cpp:313
ClimateCall & set_fan_mode(StringRef custom_fan_mode)
Set the custom fan mode of the climate device (StringRef overload to resolve ambiguity,...
Definition climate.h:83
ClimateCall & set_preset(StringRef custom_preset)
Set the custom preset of the climate device (StringRef overload to resolve ambiguity,...
Definition climate.h:107
ClimateCall & set_target_temperature(float target_temperature)
Set the target temperature of the climate device.
Definition climate.cpp:290
bool has_custom_preset() const
Definition climate.h:127
const optional< float > & get_target_temperature_low() const
Definition climate.cpp:311
ClimateCall & set_swing_mode(ClimateSwingMode swing_mode)
Set the swing mode of the climate device.
Definition climate.cpp:268
optional< ClimateFanMode > fan_mode_
Definition climate.h:138
ClimateCall & set_target_temperature_low(float target_temperature_low)
Set the low point target temperature of the climate device.
Definition climate.cpp:295
optional< float > target_temperature_
Definition climate.h:133
const optional< float > & get_target_temperature() const
Definition climate.cpp:310
const optional< ClimatePreset > & get_preset() const
Definition climate.cpp:318
optional< ClimateSwingMode > swing_mode_
Definition climate.h:139
optional< ClimateMode > mode_
Definition climate.h:137
ClimateCall & set_preset(ClimatePreset preset)
Set the preset of the climate device.
Definition climate.cpp:229
const optional< float > & get_target_temperature_high() const
Definition climate.cpp:312
const optional< ClimateFanMode > & get_fan_mode() const
Definition climate.cpp:316
optional< float > target_humidity_
Definition climate.h:136
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:124
optional< ClimatePreset > preset_
Definition climate.h:140
ClimateCall & set_target_humidity(float target_humidity)
Set the target humidity of the climate device.
Definition climate.cpp:305
ClimateCall(Climate *parent)
Definition climate.h:33
optional< float > target_temperature_low_
Definition climate.h:134
StringRef get_custom_preset() const
Definition climate.h:125
ClimateCall & set_target_temperature_high(float target_temperature_high)
Set the high point target temperature of the climate device.
Definition climate.cpp:300
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:315
ClimateDevice - This is the base class for all climate integrations.
Definition climate.h:192
ClimateMode mode
The active mode of the climate device.
Definition climate.h:317
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:311
void set_supported_custom_presets(const std::vector< const char * > &presets)
Definition climate.h:273
void set_supported_custom_fan_modes(std::initializer_list< const char * > modes)
Set the supported custom fan modes; the list is copied (stored on Climate, referenced by ClimateTrait...
Definition climate.h:255
void add_on_control_callback(F &&callback)
Add a callback for the climate device configuration; each time the configuration parameters of a clim...
Definition climate.h:211
ClimateTraits get_traits()
Get the traits of this climate device with all overrides applied.
Definition climate.cpp:482
float target_temperature
The target temperature of the climate device.
Definition climate.h:298
float current_humidity
The current humidity of the climate device, as reported from the integration.
Definition climate.h:294
bool set_custom_fan_mode_(StringRef mode)
Definition climate.h:341
float visual_min_humidity_override_
Definition climate.h:399
float visual_target_temperature_step_override_
Definition climate.h:397
void set_visual_min_humidity_override(float visual_min_humidity_override)
Definition climate.h:246
void set_supported_custom_presets(const char *const (&presets)[N])
Definition climate.h:276
void dump_traits_(const char *tag)
Definition climate.cpp:699
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition climate.h:323
void set_supported_custom_presets_static(const char *const *presets, size_t count)
Points at a static table that outlives the climate (codegen); call before any runtime copy.
Definition climate.h:280
float target_temperature_low
The minimum target temperature of the climate device, for climate devices with split target temperatu...
Definition climate.h:301
void set_visual_max_humidity_override(float visual_max_humidity_override)
Definition climate.h:249
void set_supported_custom_presets(std::initializer_list< const char * > presets)
Set the supported custom presets; the list is copied (stored on Climate, referenced by ClimateTraits)...
Definition climate.h:270
float visual_current_temperature_step_override_
Definition climate.h:398
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:674
bool set_custom_preset_(const char *preset)
Set custom preset. Reset primary preset. Return true if preset has been changed.
Definition climate.h:349
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:683
float visual_min_temperature_override_
Definition climate.h:395
void set_visual_max_temperature_override(float visual_max_temperature_override)
Definition climate.h:239
void clear_custom_preset_()
Clear custom preset.
Definition climate.cpp:681
void add_on_state_callback(F &&callback)
Add a callback for the climate device state, each time the state of the climate device is updated (us...
Definition climate.h:201
bool set_fan_mode_(ClimateFanMode mode)
Set fan mode. Reset custom fan mode. Return true if fan mode has been changed.
Definition climate.cpp:663
LazyCallbackManager< void(Climate &)> state_callback_
Definition climate.h:390
bool has_custom_preset() const
Check if a custom preset is currently active.
Definition climate.h:288
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:691
void clear_custom_fan_mode_()
Clear custom fan mode.
Definition climate.cpp:672
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:291
float visual_max_humidity_override_
Definition climate.h:400
ClimateAction action
The active state of the climate device.
Definition climate.h:320
LazyCallbackManager< void(ClimateCall &)> control_callback_
Definition climate.h:391
void set_supported_custom_fan_modes_static(const char *const *modes, size_t count)
Points at a static table that outlives the climate (codegen); call before any runtime copy.
Definition climate.h:265
ClimateCall make_call()
Make a climate device control call, this is used to control the climate device, see the ClimateCall d...
Definition climate.cpp:508
float visual_max_temperature_override_
Definition climate.h:396
StringRef get_custom_preset() const
Get the active custom preset (read-only access). Returns StringRef.
Definition climate.h:329
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:432
void set_visual_temperature_step_override(float target, float current)
Definition climate.h:242
bool has_custom_fan_mode() const
Check if a custom fan mode is currently active.
Definition climate.h:285
void set_supported_custom_fan_modes(const std::vector< const char * > &modes)
Definition climate.h:258
ESPPreferenceObject rtc_
Definition climate.h:392
optional< ClimatePreset > preset
The active preset of the climate device.
Definition climate.h:314
void set_visual_min_temperature_override(float visual_min_temperature_override)
Definition climate.h:236
bool set_custom_preset_(StringRef preset)
Definition climate.h:351
optional< ClimateDeviceRestoreState > restore_state_()
Restore the state of the climate device, call this from your setup() method.
Definition climate.cpp:365
float target_humidity
The target humidity of the climate device.
Definition climate.h:308
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:339
void set_supported_custom_fan_modes(const char *const (&modes)[N])
Definition climate.h:261
float target_temperature_high
The maximum target temperature of the climate device, for climate devices with split target temperatu...
Definition climate.h:303
StringRef get_custom_fan_mode() const
Get the active custom fan mode (read-only access). Returns StringRef.
Definition climate.h:326
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
unsigned count
Definition gif.cpp:51
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.
const void size_t len
Definition hal.h:68
Struct used to save the state of the climate device in restore memory.
Definition climate.h:149
struct esphome::climate::ClimateDeviceRestoreState::@58::@59 __attribute__
ClimateCall to_call(Climate *climate)
Convert this struct to a climate call that can be performed.
Definition climate.cpp:510
void apply(Climate *climate)
Apply these settings to the climate device.
Definition climate.cpp:546