ESPHome 2026.10.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) \
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 this->visual_min_temperature_override_ = visual_min_temperature_override;
233 }
234 void set_visual_max_temperature_override(float visual_max_temperature_override) {
235 this->visual_max_temperature_override_ = visual_max_temperature_override;
236 }
237 void set_visual_temperature_step_override(float target, float current) {
240 }
241 void set_visual_min_humidity_override(float visual_min_humidity_override) {
242 this->visual_min_humidity_override_ = visual_min_humidity_override;
243 }
244 void set_visual_max_humidity_override(float visual_max_humidity_override) {
245 this->visual_max_humidity_override_ = visual_max_humidity_override;
246 }
247#endif
248
250 void set_supported_custom_fan_modes(std::initializer_list<const char *> modes) {
251 this->ensure_custom_fan_modes_().assign(modes.begin(), modes.end());
252 }
253 void set_supported_custom_fan_modes(const std::vector<const char *> &modes) {
254 this->ensure_custom_fan_modes_() = modes;
255 }
256 template<size_t N> void set_supported_custom_fan_modes(const char *const (&modes)[N]) {
257 this->ensure_custom_fan_modes_().assign(modes, modes + N);
258 }
259
261 void set_supported_custom_presets(std::initializer_list<const char *> presets) {
262 this->ensure_custom_presets_().assign(presets.begin(), presets.end());
263 }
264 void set_supported_custom_presets(const std::vector<const char *> &presets) {
265 this->ensure_custom_presets_() = presets;
266 }
267 template<size_t N> void set_supported_custom_presets(const char *const (&presets)[N]) {
268 this->ensure_custom_presets_().assign(presets, presets + N);
269 }
270
272 bool has_custom_fan_mode() const { return this->custom_fan_mode_ != nullptr; }
273
275 bool has_custom_preset() const { return this->custom_preset_ != nullptr; }
276
279
282
283 union {
286 struct {
291 };
292 };
293
296
298 optional<ClimateFanMode> fan_mode;
299
301 optional<ClimatePreset> preset;
302
305
308
311
313 StringRef get_custom_fan_mode() const { return StringRef::from_maybe_nullptr(this->custom_fan_mode_); }
314
316 StringRef get_custom_preset() const { return StringRef::from_maybe_nullptr(this->custom_preset_); }
317
318 protected:
321
324
326 bool set_custom_fan_mode_(const char *mode) { return this->set_custom_fan_mode_(mode, strlen(mode)); }
327 bool set_custom_fan_mode_(const char *mode, size_t len);
328 bool set_custom_fan_mode_(StringRef mode) { return this->set_custom_fan_mode_(mode.c_str(), mode.size()); }
331
334
336 bool set_custom_preset_(const char *preset) { return this->set_custom_preset_(preset, strlen(preset)); }
337 bool set_custom_preset_(const char *preset, size_t len);
338 bool set_custom_preset_(StringRef preset) { return this->set_custom_preset_(preset.c_str(), preset.size()); }
341
343 const char *find_custom_fan_mode_(const char *custom_fan_mode);
344 const char *find_custom_fan_mode_(const char *custom_fan_mode, size_t len);
345
347 const char *find_custom_preset_(const char *custom_preset);
348 const char *find_custom_preset_(const char *custom_preset, size_t len);
349
356 virtual ClimateTraits traits() = 0;
357
366 virtual void control(const ClimateCall &call) = 0;
368 optional<ClimateDeviceRestoreState> restore_state_();
372 void save_state_(const ClimateTraits &traits);
373 void save_state_() { this->save_state_(this->get_traits()); }
374
375 void dump_traits_(const char *tag);
376
380
381#ifdef USE_CLIMATE_VISUAL_OVERRIDES
388#endif
389
390 private:
392 std::vector<const char *> &ensure_custom_fan_modes_() {
393 if (!this->supported_custom_fan_modes_) {
394 this->supported_custom_fan_modes_ = new std::vector<const char *>(); // NOLINT
395 }
396 return *this->supported_custom_fan_modes_;
397 }
398 std::vector<const char *> &ensure_custom_presets_() {
399 if (!this->supported_custom_presets_) {
400 this->supported_custom_presets_ = new std::vector<const char *>(); // NOLINT
401 }
402 return *this->supported_custom_presets_;
403 }
404
405 std::vector<const char *> *supported_custom_fan_modes_{nullptr};
406 std::vector<const char *> *supported_custom_presets_{nullptr};
407
413 const char *custom_fan_mode_{nullptr};
414
420 const char *custom_preset_{nullptr};
422
423} // 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:304
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:298
void set_supported_custom_presets(const std::vector< const char * > &presets)
Definition climate.h:264
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:250
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:486
float target_temperature
The target temperature of the climate device.
Definition climate.h:285
float current_humidity
The current humidity of the climate device, as reported from the integration.
Definition climate.h:281
bool set_custom_fan_mode_(StringRef mode)
Definition climate.h:328
float visual_min_humidity_override_
Definition climate.h:386
float visual_target_temperature_step_override_
Definition climate.h:384
void set_visual_min_humidity_override(float visual_min_humidity_override)
Definition climate.h:241
void set_supported_custom_presets(const char *const (&presets)[N])
Definition climate.h:267
void dump_traits_(const char *tag)
Definition climate.cpp:713
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition climate.h:310
float target_temperature_low
The minimum target temperature of the climate device, for climate devices with split target temperatu...
Definition climate.h:288
void set_visual_max_humidity_override(float visual_max_humidity_override)
Definition climate.h:244
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:261
float visual_current_temperature_step_override_
Definition climate.h:385
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:680
bool set_custom_preset_(const char *preset)
Set custom preset. Reset primary preset. Return true if preset has been changed.
Definition climate.h:336
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:689
float visual_min_temperature_override_
Definition climate.h:382
void set_visual_max_temperature_override(float visual_max_temperature_override)
Definition climate.h:234
void clear_custom_preset_()
Clear custom preset.
Definition climate.cpp:687
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:669
LazyCallbackManager< void(Climate &)> state_callback_
Definition climate.h:377
bool has_custom_preset() const
Check if a custom preset is currently active.
Definition climate.h:275
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:701
void clear_custom_fan_mode_()
Clear custom fan mode.
Definition climate.cpp:678
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:278
float visual_max_humidity_override_
Definition climate.h:387
ClimateAction action
The active state of the climate device.
Definition climate.h:307
LazyCallbackManager< void(ClimateCall &)> control_callback_
Definition climate.h:378
ClimateCall make_call()
Make a climate device control call, this is used to control the climate device, see the ClimateCall d...
Definition climate.cpp:514
float visual_max_temperature_override_
Definition climate.h:383
StringRef get_custom_preset() const
Get the active custom preset (read-only access). Returns StringRef.
Definition climate.h:316
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.h:237
bool has_custom_fan_mode() const
Check if a custom fan mode is currently active.
Definition climate.h:272
void set_supported_custom_fan_modes(const std::vector< const char * > &modes)
Definition climate.h:253
ESPPreferenceObject rtc_
Definition climate.h:379
optional< ClimatePreset > preset
The active preset of the climate device.
Definition climate.h:301
void set_visual_min_temperature_override(float visual_min_temperature_override)
Definition climate.h:231
bool set_custom_preset_(StringRef preset)
Definition climate.h:338
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:295
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:326
void set_supported_custom_fan_modes(const char *const (&modes)[N])
Definition climate.h:256
float target_temperature_high
The maximum target temperature of the climate device, for climate devices with split target temperatu...
Definition climate.h:290
StringRef get_custom_fan_mode() const
Get the active custom fan mode (read-only access). Returns StringRef.
Definition climate.h:313
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 void size_t len
Definition hal.h:64
Struct used to save the state of the climate device in restore memory.
Definition climate.h:144
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:516
void apply(Climate *climate)
Apply these settings to the climate device.
Definition climate.cpp:552