ESPHome 2026.9.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 if (changed) {
95 this->publish_state();
96 }
97}
98
100 LOG_WATER_HEATER("", "Template Water Heater", this);
101 ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
102}
103
105
109
111 auto mode_val = call.get_mode();
112 if (mode_val.has_value()) {
113 if (this->optimistic_) {
114 this->mode_ = *mode_val;
115 }
116 }
117 if (!std::isnan(call.get_target_temperature())) {
118 if (this->optimistic_) {
120 }
121 }
122
123 auto away_val = call.get_away();
124 if (away_val.has_value()) {
125 if (this->optimistic_) {
127 }
128 }
129 auto on_val = call.get_on();
130 if (on_val.has_value()) {
131 if (this->optimistic_) {
133 }
134 }
135
136 this->set_trigger_.trigger();
137
138 if (this->optimistic_) {
139 this->publish_state();
140 }
141}
142
143} // 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:461
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.