ESPHome 2026.2.4
Loading...
Searching...
No Matches
template_water_heater.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
3
4namespace esphome::template_ {
5
6static const char *const TAG = "template.water_heater";
7
9
13 auto restore = this->restore_state_();
14
15 if (restore.has_value()) {
16 restore->perform();
17 }
18 }
19 if (!this->current_temperature_f_.has_value() && !this->target_temperature_f_.has_value() &&
20 !this->mode_f_.has_value() && !this->away_f_.has_value() && !this->is_on_f_.has_value())
21 this->disable_loop();
22}
23
43
45 bool changed = false;
46
47 auto curr_temp = this->current_temperature_f_.call();
48 if (curr_temp.has_value()) {
49 if (*curr_temp != this->current_temperature_) {
50 this->current_temperature_ = *curr_temp;
51 changed = true;
52 }
53 }
54
55 auto target_temp = this->target_temperature_f_.call();
56 if (target_temp.has_value()) {
57 if (*target_temp != this->target_temperature_) {
58 this->target_temperature_ = *target_temp;
59 changed = true;
60 }
61 }
62
63 auto new_mode = this->mode_f_.call();
64 if (new_mode.has_value()) {
65 if (*new_mode != this->mode_) {
66 this->mode_ = *new_mode;
67 changed = true;
68 }
69 }
70
71 auto away = this->away_f_.call();
72 if (away.has_value()) {
73 if (*away != this->is_away()) {
75 changed = true;
76 }
77 }
78
79 auto is_on = this->is_on_f_.call();
80 if (is_on.has_value()) {
81 if (*is_on != this->is_on()) {
83 changed = true;
84 }
85 }
86
87 if (changed) {
88 this->publish_state();
89 }
90}
91
93 LOG_WATER_HEATER("", "Template Water Heater", this);
94 ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
95}
96
98
102
104 if (call.get_mode().has_value()) {
105 if (this->optimistic_) {
106 this->mode_ = *call.get_mode();
107 }
108 }
109 if (!std::isnan(call.get_target_temperature())) {
110 if (this->optimistic_) {
112 }
113 }
114
115 if (call.get_away().has_value()) {
116 if (this->optimistic_) {
118 }
119 }
120 if (call.get_on().has_value()) {
121 if (this->optimistic_) {
123 }
124 }
125
126 this->set_trigger_.trigger();
127
128 if (this->optimistic_) {
129 this->publish_state();
130 }
131}
132
133} // namespace esphome::template_
void disable_loop()
Disable this component's loop.
constexpr bool empty() const
Check if the set is empty.
optional< T > call(Args &&...args)
Alias for operator() for compatibility.
bool has_value() const
Check if a lambda is set.
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
Definition automation.h:279
TemplateWaterHeaterRestoreMode restore_mode_
void control(const water_heater::WaterHeaterCall &call) override
water_heater::WaterHeaterCallInternal make_call() override
water_heater::WaterHeaterTraits traits() override
TemplateLambda< water_heater::WaterHeaterMode > mode_f_
water_heater::WaterHeaterModeMask supported_modes_
optional< bool > get_away() const
const optional< WaterHeaterMode > & get_mode() const
void set_state_flag_(uint32_t flag, bool value)
Set or clear a state flag. Should only be called from control().
optional< WaterHeaterCall > restore_state_()
Restore the state of the water heater, call this from your setup() method.
bool is_on() const
Check if the water heater is on.
bool is_away() const
Check if away mode is currently active.
void set_supported_modes(WaterHeaterModeMask modes)
void add_feature_flags(uint32_t flags)
Get/set feature flags (see WaterHeaterFeature enum)
void set_supports_current_temperature(bool supports)
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:83
const char *const TAG
Definition spi.cpp:7
@ WATER_HEATER_STATE_ON
Water heater is on (not in standby)
@ WATER_HEATER_STATE_AWAY
Away/vacation mode is currently active.
@ WATER_HEATER_SUPPORTS_TARGET_TEMPERATURE
The water heater supports a target temperature.
@ WATER_HEATER_SUPPORTS_ON_OFF
The water heater can be turned on/off.