ESPHome 2026.4.0
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) \
16 if ((obj) != nullptr) { \
17 ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
18 }
20class Climate;
21
35 public:
36 explicit ClimateCall(Climate *parent) : parent_(parent) {}
38
42 ClimateCall &set_mode(optional<ClimateMode> mode);
44 ClimateCall &set_mode(const std::string &mode);
46 ClimateCall &set_mode(const char *mode, size_t len);
78 ClimateCall &set_fan_mode(optional<ClimateFanMode> fan_mode);
80 ClimateCall &set_fan_mode(const std::string &fan_mode);
82 ClimateCall &set_fan_mode(optional<std::string> fan_mode);
86 ClimateCall &set_fan_mode(const char *custom_fan_mode, size_t len);
90 ClimateCall &set_swing_mode(optional<ClimateSwingMode> swing_mode);
92 ClimateCall &set_swing_mode(const std::string &swing_mode);
94 ClimateCall &set_swing_mode(const char *swing_mode, size_t len);
98 ClimateCall &set_preset(optional<ClimatePreset> preset);
100 ClimateCall &set_preset(const std::string &preset);
102 ClimateCall &set_preset(optional<std::string> preset);
106 ClimateCall &set_preset(const char *custom_preset, size_t len);
107
108 void perform();
109
110 const optional<float> &get_target_temperature() const;
111 const optional<float> &get_target_temperature_low() const;
112 const optional<float> &get_target_temperature_high() const;
113 const optional<float> &get_target_humidity() const;
114
115 const optional<ClimateMode> &get_mode() const;
116 const optional<ClimateFanMode> &get_fan_mode() const;
117 const optional<ClimateSwingMode> &get_swing_mode() const;
118 const optional<ClimatePreset> &get_preset() const;
119 StringRef get_custom_fan_mode() const { return StringRef::from_maybe_nullptr(this->custom_fan_mode_); }
120 StringRef get_custom_preset() const { return StringRef::from_maybe_nullptr(this->custom_preset_); }
121 bool has_custom_fan_mode() const { return this->custom_fan_mode_ != nullptr; }
122 bool has_custom_preset() const { return this->custom_preset_ != nullptr; }
123
124 protected:
125 void validate_();
126
128 optional<float> target_temperature_;
129 optional<float> target_temperature_low_;
131 optional<float> target_humidity_;
132 optional<ClimateMode> mode_;
133 optional<ClimateFanMode> fan_mode_;
134 optional<ClimateSwingMode> swing_mode_;
135 optional<ClimatePreset> preset_;
136
137 private:
138 const char *custom_fan_mode_{nullptr};
139 const char *custom_preset_{nullptr};
140};
141
147 union {
150 };
152 union {
155 };
165
167 ClimateCall to_call(Climate *climate);
169 void apply(Climate *climate);
170} __attribute__((packed));
171
187class Climate : public EntityBase {
188 public:
190
196 template<typename F> void add_on_state_callback(F &&callback) {
197 this->state_callback_.add(std::forward<F>(callback));
198 }
199
206 template<typename F> void add_on_control_callback(F &&callback) {
207 this->control_callback_.add(std::forward<F>(callback));
208 }
209
215
221 void publish_state();
222
229
230#ifdef USE_CLIMATE_VISUAL_OVERRIDES
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 void set_visual_min_humidity_override(float visual_min_humidity_override);
235 void set_visual_max_humidity_override(float visual_max_humidity_override);
236#endif
237
239 void set_supported_custom_fan_modes(std::initializer_list<const char *> modes) {
240 this->ensure_custom_fan_modes_().assign(modes.begin(), modes.end());
241 }
242 void set_supported_custom_fan_modes(const std::vector<const char *> &modes) {
243 this->ensure_custom_fan_modes_() = modes;
244 }
245 template<size_t N> void set_supported_custom_fan_modes(const char *const (&modes)[N]) {
246 this->ensure_custom_fan_modes_().assign(modes, modes + N);
247 }
248
250 void set_supported_custom_presets(std::initializer_list<const char *> presets) {
251 this->ensure_custom_presets_().assign(presets.begin(), presets.end());
252 }
253 void set_supported_custom_presets(const std::vector<const char *> &presets) {
254 this->ensure_custom_presets_() = presets;
255 }
256 template<size_t N> void set_supported_custom_presets(const char *const (&presets)[N]) {
257 this->ensure_custom_presets_().assign(presets, presets + N);
258 }
259
261 bool has_custom_fan_mode() const { return this->custom_fan_mode_ != nullptr; }
262
264 bool has_custom_preset() const { return this->custom_preset_ != nullptr; }
265
268
271
272 union {
275 struct {
280 };
281 };
282
285
287 optional<ClimateFanMode> fan_mode;
288
290 optional<ClimatePreset> preset;
291
294
297
300
302 StringRef get_custom_fan_mode() const { return StringRef::from_maybe_nullptr(this->custom_fan_mode_); }
303
305 StringRef get_custom_preset() const { return StringRef::from_maybe_nullptr(this->custom_preset_); }
306
307 protected:
310
313
315 bool set_custom_fan_mode_(const char *mode) { return this->set_custom_fan_mode_(mode, strlen(mode)); }
316 bool set_custom_fan_mode_(const char *mode, size_t len);
317 bool set_custom_fan_mode_(StringRef mode) { return this->set_custom_fan_mode_(mode.c_str(), mode.size()); }
320
323
325 bool set_custom_preset_(const char *preset) { return this->set_custom_preset_(preset, strlen(preset)); }
326 bool set_custom_preset_(const char *preset, size_t len);
327 bool set_custom_preset_(StringRef preset) { return this->set_custom_preset_(preset.c_str(), preset.size()); }
330
332 const char *find_custom_fan_mode_(const char *custom_fan_mode);
333 const char *find_custom_fan_mode_(const char *custom_fan_mode, size_t len);
334
336 const char *find_custom_preset_(const char *custom_preset);
337 const char *find_custom_preset_(const char *custom_preset, size_t len);
338
345 virtual ClimateTraits traits() = 0;
346
355 virtual void control(const ClimateCall &call) = 0;
357 optional<ClimateDeviceRestoreState> restore_state_();
361 void save_state_(const ClimateTraits &traits);
362 void save_state_() { this->save_state_(this->get_traits()); }
363
364 void dump_traits_(const char *tag);
365
369
370#ifdef USE_CLIMATE_VISUAL_OVERRIDES
377#endif
378
379 private:
381 std::vector<const char *> &ensure_custom_fan_modes_() {
382 if (!this->supported_custom_fan_modes_) {
383 this->supported_custom_fan_modes_ = new std::vector<const char *>(); // NOLINT
384 }
385 return *this->supported_custom_fan_modes_;
386 }
387 std::vector<const char *> &ensure_custom_presets_() {
388 if (!this->supported_custom_presets_) {
389 this->supported_custom_presets_ = new std::vector<const char *>(); // NOLINT
390 }
391 return *this->supported_custom_presets_;
392 }
393
394 std::vector<const char *> *supported_custom_fan_modes_{nullptr};
395 std::vector<const char *> *supported_custom_presets_{nullptr};
396
402 const char *custom_fan_mode_{nullptr};
403
409 const char *custom_preset_{nullptr};
411
412} // 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:34
const optional< ClimateSwingMode > & get_swing_mode() const
Definition climate.cpp:314
optional< float > target_temperature_high_
Definition climate.h:130
bool has_custom_fan_mode() const
Definition climate.h:121
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:122
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:133
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:128
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:134
optional< ClimateMode > mode_
Definition climate.h:132
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:131
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:119
optional< ClimatePreset > preset_
Definition climate.h:135
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:36
optional< float > target_temperature_low_
Definition climate.h:129
StringRef get_custom_preset() const
Definition climate.h:120
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:187
ClimateMode mode
The active mode of the climate device.
Definition climate.h:293
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:287
void set_supported_custom_presets(const std::vector< const char * > &presets)
Definition climate.h:253
void set_supported_custom_fan_modes(std::initializer_list< const char * > modes)
Set the supported custom fan modes (stored on Climate, referenced by ClimateTraits).
Definition climate.h:239
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:206
ClimateTraits get_traits()
Get the traits of this climate device with all overrides applied.
Definition climate.cpp:485
float target_temperature
The target temperature of the climate device.
Definition climate.h:274
float current_humidity
The current humidity of the climate device, as reported from the integration.
Definition climate.h:270
bool set_custom_fan_mode_(StringRef mode)
Definition climate.h:317
float visual_min_humidity_override_
Definition climate.h:375
float visual_target_temperature_step_override_
Definition climate.h:373
void set_visual_min_humidity_override(float visual_min_humidity_override)
Definition climate.cpp:527
void set_supported_custom_presets(const char *const (&presets)[N])
Definition climate.h:256
void dump_traits_(const char *tag)
Definition climate.cpp:728
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition climate.h:299
float target_temperature_low
The minimum target temperature of the climate device, for climate devices with split target temperatu...
Definition climate.h:277
void set_visual_max_humidity_override(float visual_max_humidity_override)
Definition climate.cpp:531
void set_supported_custom_presets(std::initializer_list< const char * > presets)
Set the supported custom presets (stored on Climate, referenced by ClimateTraits).
Definition climate.h:250
float visual_current_temperature_step_override_
Definition climate.h:374
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:695
bool set_custom_preset_(const char *preset)
Set custom preset. Reset primary preset. Return true if preset has been changed.
Definition climate.h:325
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:704
float visual_min_temperature_override_
Definition climate.h:371
void set_visual_max_temperature_override(float visual_max_temperature_override)
Definition climate.cpp:518
void clear_custom_preset_()
Clear custom preset.
Definition climate.cpp:702
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:196
bool set_fan_mode_(ClimateFanMode mode)
Set fan mode. Reset custom fan mode. Return true if fan mode has been changed.
Definition climate.cpp:684
LazyCallbackManager< void(Climate &)> state_callback_
Definition climate.h:366
bool has_custom_preset() const
Check if a custom preset is currently active.
Definition climate.h:264
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:716
void clear_custom_fan_mode_()
Clear custom fan mode.
Definition climate.cpp:693
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:267
float visual_max_humidity_override_
Definition climate.h:376
ClimateAction action
The active state of the climate device.
Definition climate.h:296
LazyCallbackManager< void(ClimateCall &)> control_callback_
Definition climate.h:367
ClimateCall make_call()
Make a climate device control call, this is used to control the climate device, see the ClimateCall d...
Definition climate.cpp:536
float visual_max_temperature_override_
Definition climate.h:372
StringRef get_custom_preset() const
Get the active custom preset (read-only access). Returns StringRef.
Definition climate.h:305
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:436
void set_visual_temperature_step_override(float target, float current)
Definition climate.cpp:522
bool has_custom_fan_mode() const
Check if a custom fan mode is currently active.
Definition climate.h:261
void set_supported_custom_fan_modes(const std::vector< const char * > &modes)
Definition climate.h:242
ESPPreferenceObject rtc_
Definition climate.h:368
optional< ClimatePreset > preset
The active preset of the climate device.
Definition climate.h:290
void set_visual_min_temperature_override(float visual_min_temperature_override)
Definition climate.cpp:514
bool set_custom_preset_(StringRef preset)
Definition climate.h:327
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:284
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:315
void set_supported_custom_fan_modes(const char *const (&modes)[N])
Definition climate.h:245
float target_temperature_high
The maximum target temperature of the climate device, for climate devices with split target temperatu...
Definition climate.h:279
StringRef get_custom_fan_mode() const
Get the active custom fan mode (read-only access). Returns StringRef.
Definition climate.h:302
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.
const char * tag
Definition log.h:74
std::string size_t len
Definition helpers.h:1045
Struct used to save the state of the climate device in restore memory.
Definition climate.h:144
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:538
void apply(Climate *climate)
Apply these settings to the climate device.
Definition climate.cpp:574