ESPHome 2026.10.0
Loading...
Searching...
No Matches
template_water_heater.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
3
4#include <cmath>
5
6namespace esphome::template_ {
7
8static const char *const TAG = "template.water_heater";
9
11
15 auto restore = this->restore_state_();
16
17 if (restore.has_value()) {
18 restore->perform();
19 }
20 }
21 if (!this->current_temperature_f_.has_value() && !this->target_temperature_f_.has_value() &&
22 !this->mode_f_.has_value() && !this->away_f_.has_value() && !this->is_on_f_.has_value())
23 this->disable_loop();
24}
25
46
48 bool changed = false;
49
50 // NAN is passed through so a source that has no value yet shows as unknown, but NAN never
51 // equals NAN, so an already-NAN value must not count as a change or it would republish forever.
52 auto curr_temp = this->current_temperature_f_.call();
53 if (curr_temp.has_value()) {
54 if (*curr_temp != this->current_temperature_ &&
55 !(std::isnan(*curr_temp) && std::isnan(this->current_temperature_))) {
56 this->current_temperature_ = *curr_temp;
57 changed = true;
58 }
59 }
60
61 auto target_temp = this->target_temperature_f_.call();
62 if (target_temp.has_value()) {
63 if (*target_temp != this->target_temperature_ &&
64 !(std::isnan(*target_temp) && std::isnan(this->target_temperature_))) {
65 this->target_temperature_ = *target_temp;
66 changed = true;
67 }
68 }
69
70 auto new_mode = this->mode_f_.call();
71 if (new_mode.has_value()) {
72 if (*new_mode != this->mode_) {
73 this->mode_ = *new_mode;
74 changed = true;
75 }
76 }
77
78 auto away = this->away_f_.call();
79 if (away.has_value()) {
80 if (*away != this->is_away()) {
82 changed = true;
83 }
84 }
85
86 auto is_on = this->is_on_f_.call();
87 if (is_on.has_value()) {
88 if (*is_on != this->is_on()) {
90 changed = true;
91 }
92 }
93
94 // The first value always publishes, even one equal to the default (mode OFF, say), or the
95 // entity would report unknown forever.
96 const bool has_value =
97 curr_temp.has_value() || target_temp.has_value() || new_mode.has_value() || away.has_value() || is_on.has_value();
98 if (changed || (has_value && !this->has_state())) {
99 this->publish_state();
100 }
101}
102
104 LOG_WATER_HEATER("", "Template Water Heater", this);
105 ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
106}
107
109
113
115 auto mode_val = call.get_mode();
116 if (mode_val.has_value()) {
117 if (this->optimistic_) {
118 this->mode_ = *mode_val;
119 }
120 }
121 if (!std::isnan(call.get_target_temperature())) {
122 if (this->optimistic_) {
124 }
125 }
126
127 auto away_val = call.get_away();
128 if (away_val.has_value()) {
129 if (this->optimistic_) {
131 }
132 }
133 auto on_val = call.get_on();
134 if (on_val.has_value()) {
135 if (this->optimistic_) {
137 }
138 }
139
140 this->set_trigger_.trigger();
141
142 if (this->optimistic_) {
143 this->publish_state();
144 }
145}
146
147} // namespace esphome::template_
void disable_loop()
Disable this component's loop.
bool has_state() const
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:528
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:43
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.