ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
template_climate.cpp
Go to the documentation of this file.
1#include "template_climate.h"
2#include "esphome/core/log.h"
3
4namespace esphome::template_ {
5
6static const char *const TAG = "template.climate";
7
10 auto restore = this->restore_state_();
11 if (restore.has_value()) {
12 restore->apply(this);
13 }
14 }
15
16 // Sensors publish every reading, not just changes, so only re-publish when the value moved.
17 // NAN means the sensor went unavailable and is passed through rather than dropped; the second
18 // check stops an unavailable sensor re-publishing forever, since NAN never equals NAN.
19#ifdef USE_SENSOR
20 if (this->sensor_ != nullptr) {
21 this->current_temperature = this->sensor_->state;
22 this->sensor_->add_on_state_callback([this](float state) {
23 if (state != this->current_temperature && !(std::isnan(state) && std::isnan(this->current_temperature))) {
25 this->publish_state();
26 }
27 });
28 }
29
30 if (this->humidity_sensor_ != nullptr) {
32 this->humidity_sensor_->add_on_state_callback([this](float state) {
33 if (state != this->current_humidity && !(std::isnan(state) && std::isnan(this->current_humidity))) {
34 this->current_humidity = state;
35 this->publish_state();
36 }
37 });
38 }
39#endif
40}
41
43 LOG_CLIMATE("", "Template Climate", this);
44 ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
45}
46
48 // Each field present fires its set_*_action; on_control sees the whole call. optimistic: true
49 // also applies the values right away, false waits for a climate.template.publish report.
50 if (auto mode = call.get_mode()) {
51 if (this->optimistic_)
52 this->mode = *mode;
54 }
55
56 if (auto target_temp = call.get_target_temperature()) {
57 if (this->optimistic_)
58 this->target_temperature = *target_temp;
59 this->set_target_temperature_trigger_.trigger(*target_temp);
60 }
61
62 if (auto target_temp_low = call.get_target_temperature_low()) {
63 if (this->optimistic_)
64 this->target_temperature_low = *target_temp_low;
65 this->set_target_temperature_low_trigger_.trigger(*target_temp_low);
66 }
67
68 if (auto target_temp_high = call.get_target_temperature_high()) {
69 if (this->optimistic_)
70 this->target_temperature_high = *target_temp_high;
71 this->set_target_temperature_high_trigger_.trigger(*target_temp_high);
72 }
73
74 if (auto target_humidity = call.get_target_humidity()) {
75 if (this->optimistic_)
78 }
79
80 if (auto fan_mode = call.get_fan_mode()) {
81 if (this->optimistic_)
82 this->set_fan_mode_(*fan_mode);
84 }
85
86 if (call.has_custom_fan_mode()) {
87 if (this->optimistic_)
90 }
91
92 if (auto swing_mode = call.get_swing_mode()) {
93 if (this->optimistic_)
94 this->swing_mode = *swing_mode;
96 }
97
98 if (auto preset = call.get_preset()) {
99 if (this->optimistic_)
100 this->set_preset_(*preset);
102 }
103
104 if (call.has_custom_preset()) {
105 if (this->optimistic_)
108 }
109
110 if (this->optimistic_)
111 this->publish_state();
112}
113
114// A climate.template.publish report (and initial_state:) never goes through ClimateCall::validate_(),
115// so check here instead -- otherwise a typo is published as state the receiving end will reject.
117 if (!this->traits_.supports_mode(mode)) {
118 ESP_LOGW(TAG, "'%s' - Unsupported mode %u", this->get_name().c_str(), static_cast<unsigned>(mode));
119 return;
120 }
121 this->mode = mode;
122}
123
125 if (!this->traits_.supports_swing_mode(swing_mode)) {
126 ESP_LOGW(TAG, "'%s' - Unsupported swing mode %u", this->get_name().c_str(), static_cast<unsigned>(swing_mode));
127 return;
128 }
129 this->swing_mode = swing_mode;
130}
131
133 if (!this->traits_.supports_fan_mode(fan_mode)) {
134 ESP_LOGW(TAG, "'%s' - Unsupported fan mode %u", this->get_name().c_str(), static_cast<unsigned>(fan_mode));
135 return;
136 }
137 this->set_fan_mode_(fan_mode);
138}
139
141 if (!this->traits_.supports_preset(preset)) {
142 ESP_LOGW(TAG, "'%s' - Unsupported preset %u", this->get_name().c_str(), static_cast<unsigned>(preset));
143 return;
144 }
145 this->set_preset_(preset);
146}
147
149 if (this->find_custom_fan_mode_(mode.c_str(), mode.size()) == nullptr) {
150 ESP_LOGW(TAG, "'%s' - Unsupported custom fan mode '%s'", this->get_name().c_str(), mode.c_str());
151 return;
152 }
153 this->set_custom_fan_mode_(mode);
154}
155
157 if (this->find_custom_preset_(preset.c_str(), preset.size()) == nullptr) {
158 ESP_LOGW(TAG, "'%s' - Unsupported custom preset '%s'", this->get_name().c_str(), preset.c_str());
159 return;
160 }
161 this->set_custom_preset_(preset);
162}
163
164} // namespace esphome::template_
BedjetMode mode
BedJet operating mode.
const StringRef & get_name() const
Definition entity_base.h:71
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
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
Definition automation.h:461
This class is used to encode all control actions on a climate device.
Definition climate.h:34
bool has_custom_fan_mode() const
Definition climate.h:121
bool has_custom_preset() const
Definition climate.h:122
const optional< ClimatePreset > & get_preset() const
Definition climate.cpp:315
const optional< ClimateFanMode > & get_fan_mode() const
Definition climate.cpp:313
StringRef get_custom_fan_mode() const
Definition climate.h:119
StringRef get_custom_preset() const
Definition climate.h:120
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
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
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
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
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
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
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:278
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:437
optional< ClimatePreset > preset
The active preset of the climate device.
Definition climate.h:301
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
float target_temperature_high
The maximum target temperature of the climate device, for climate devices with split target temperatu...
Definition climate.h:290
bool supports_mode(ClimateMode mode) const
bool supports_fan_mode(ClimateFanMode fan_mode) const
bool supports_preset(ClimatePreset preset) const
bool supports_swing_mode(ClimateSwingMode swing_mode) const
void add_on_state_callback(F &&callback)
Add a callback that will be called every time a filtered value arrives.
Definition sensor.h:119
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:138
Trigger< climate::ClimateMode > set_mode_trigger_
void set_swing_mode(climate::ClimateSwingMode mode)
Trigger< climate::ClimatePreset > set_preset_trigger_
void set_mode(climate::ClimateMode mode)
Trigger< climate::ClimateSwingMode > set_swing_mode_trigger_
void set_preset(climate::ClimatePreset preset)
Trigger< StringRef > set_custom_fan_mode_trigger_
void set_custom_fan_mode(const char *mode)
void control(const climate::ClimateCall &call) override
Trigger< float > set_target_temperature_low_trigger_
TemplateClimateRestoreMode restore_mode_
Trigger< StringRef > set_custom_preset_trigger_
void set_custom_preset(const char *preset)
void set_fan_mode(climate::ClimateFanMode mode)
Trigger< climate::ClimateFanMode > set_fan_mode_trigger_
Trigger< float > set_target_temperature_high_trigger_
ClimateSwingMode swing_mode
Definition climate.h:11
ClimateFanMode fan_mode
Definition climate.h:3
ClimatePreset preset
Definition climate.h:8
bool state
Definition fan.h:2
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...
ClimateMode
Enum for all modes a climate device can be in.
ClimateFanMode
NOTE: If adding values, update ClimateFanModeMask in climate_traits.h to use the new last value.
const char *const TAG
Definition spi.cpp:7