ESPHome 2026.5.0-dev
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
44
46 bool changed = false;
47
48 auto curr_temp = this->current_temperature_f_.call();
49 if (curr_temp.has_value()) {
50 if (*curr_temp != this->current_temperature_) {
51 this->current_temperature_ = *curr_temp;
52 changed = true;
53 }
54 }
55
56 auto target_temp = this->target_temperature_f_.call();
57 if (target_temp.has_value()) {
58 if (*target_temp != this->target_temperature_) {
59 this->target_temperature_ = *target_temp;
60 changed = true;
61 }
62 }
63
64 auto new_mode = this->mode_f_.call();
65 if (new_mode.has_value()) {
66 if (*new_mode != this->mode_) {
67 this->mode_ = *new_mode;
68 changed = true;
69 }
70 }
71
72 auto away = this->away_f_.call();
73 if (away.has_value()) {
74 if (*away != this->is_away()) {
76 changed = true;
77 }
78 }
79
80 auto is_on = this->is_on_f_.call();
81 if (is_on.has_value()) {
82 if (*is_on != this->is_on()) {
84 changed = true;
85 }
86 }
87
88 if (changed) {
89 this->publish_state();
90 }
91}
92
94 LOG_WATER_HEATER("", "Template Water Heater", this);
95 ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
96}
97
99
103
105 auto mode_val = call.get_mode();
106 if (mode_val.has_value()) {
107 if (this->optimistic_) {
108 this->mode_ = *mode_val;
109 }
110 }
111 if (!std::isnan(call.get_target_temperature())) {
112 if (this->optimistic_) {
114 }
115 }
116
117 auto away_val = call.get_away();
118 if (away_val.has_value()) {
119 if (this->optimistic_) {
121 }
122 }
123 auto on_val = call.get_on();
124 if (on_val.has_value()) {
125 if (this->optimistic_) {
127 }
128 }
129
130 this->set_trigger_.trigger();
131
132 if (this->optimistic_) {
133 this->publish_state();
134 }
135}
136
137} // 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) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
Definition automation.h:482
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_
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)
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:40
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_OPERATION_MODE
The water heater supports operation mode selection.
@ WATER_HEATER_SUPPORTS_ON_OFF
The water heater can be turned on/off.