ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
water_heater.cpp
Go to the documentation of this file.
1#include "water_heater.h"
2#include "esphome/core/log.h"
3
4#include <cinttypes>
9
10#include <cmath>
11#include <cstring>
12
14
15ESPHOME_LOG_TAG(TAG, "water_heater");
16
17WaterHeaterCall::WaterHeaterCall(WaterHeater *parent) : parent_(parent) {}
18
23
24WaterHeaterCall &WaterHeaterCall::set_mode(const char *mode) { return this->set_mode(mode, strlen(mode)); }
25
27 if (len == 3 && ESPHOME_strncasecmp_P(mode, ESPHOME_PSTR("OFF"), 3) == 0) {
29 } else if (len == 3 && ESPHOME_strncasecmp_P(mode, ESPHOME_PSTR("ECO"), 3) == 0) {
31 } else if (len == 8 && ESPHOME_strncasecmp_P(mode, ESPHOME_PSTR("ELECTRIC"), 8) == 0) {
33 } else if (len == 11 && ESPHOME_strncasecmp_P(mode, ESPHOME_PSTR("PERFORMANCE"), 11) == 0) {
35 } else if (len == 11 && ESPHOME_strncasecmp_P(mode, ESPHOME_PSTR("HIGH_DEMAND"), 11) == 0) {
37 } else if (len == 9 && ESPHOME_strncasecmp_P(mode, ESPHOME_PSTR("HEAT_PUMP"), 9) == 0) {
39 } else if (len == 3 && ESPHOME_strncasecmp_P(mode, ESPHOME_PSTR("GAS"), 3) == 0) {
41 } else {
42 ESP_LOGW(TAG, "'%s' - Unrecognized mode %.*s", LOG_STR_ARG(this->parent_->get_log_name()), (int) len, mode);
43 }
44 return *this;
45}
46
51
56
61
67
73
75 ESP_LOGV(TAG, "'%s' - Setting", LOG_STR_ARG(this->parent_->get_log_name()));
76 this->validate_();
77 if (this->mode_.has_value()) {
78 ESP_LOGV(TAG, " Mode: %s", LOG_STR_ARG(water_heater_mode_to_string(*this->mode_)));
79 }
80 if (!std::isnan(this->target_temperature_)) {
81 ESP_LOGV(TAG, " Target Temperature: %.2f", this->target_temperature_);
82 }
83 if (!std::isnan(this->target_temperature_low_)) {
84 ESP_LOGV(TAG, " Target Temperature Low: %.2f", this->target_temperature_low_);
85 }
86 if (!std::isnan(this->target_temperature_high_)) {
87 ESP_LOGV(TAG, " Target Temperature High: %.2f", this->target_temperature_high_);
88 }
90 ESP_LOGV(TAG, " Away: %s",
91 (this->state_ & WATER_HEATER_STATE_AWAY) ? LOG_STR_LITERAL("YES") : LOG_STR_LITERAL("NO"));
92 }
94 ESP_LOGV(TAG, " On: %s", (this->state_ & WATER_HEATER_STATE_ON) ? LOG_STR_LITERAL("YES") : LOG_STR_LITERAL("NO"));
95 }
96 this->parent_->control(*this);
97}
98
100 auto traits = this->parent_->get_traits();
101 if (this->mode_.has_value()) {
102 if (!traits.supports_mode(*this->mode_)) {
103 ESP_LOGW(TAG, "'%s' - Mode %" PRIu32 " not supported", LOG_STR_ARG(this->parent_->get_log_name()),
104 static_cast<uint32_t>(*this->mode_));
105 this->mode_.reset();
106 }
107 }
108 if (!std::isnan(this->target_temperature_)) {
109 if (traits.get_supports_two_point_target_temperature()) {
110 ESP_LOGW(TAG, "'%s' - Cannot set target temperature for device with two-point target temperature",
111 LOG_STR_ARG(this->parent_->get_log_name()));
112 this->target_temperature_ = NAN;
113 } else if (this->target_temperature_ < traits.get_min_temperature() ||
114 this->target_temperature_ > traits.get_max_temperature()) {
115 ESP_LOGW(TAG, "'%s' - Target temperature %.1f is out of range [%.1f - %.1f]",
116 LOG_STR_ARG(this->parent_->get_log_name()), this->target_temperature_, traits.get_min_temperature(),
117 traits.get_max_temperature());
118 this->target_temperature_ =
119 std::max(traits.get_min_temperature(), std::min(this->target_temperature_, traits.get_max_temperature()));
120 }
121 }
122 if (!std::isnan(this->target_temperature_low_) || !std::isnan(this->target_temperature_high_)) {
123 if (!traits.get_supports_two_point_target_temperature()) {
124 ESP_LOGW(TAG, "'%s' - Cannot set low/high target temperature", LOG_STR_ARG(this->parent_->get_log_name()));
125 this->target_temperature_low_ = NAN;
126 this->target_temperature_high_ = NAN;
127 }
128 }
129 if (!std::isnan(this->target_temperature_low_) && !std::isnan(this->target_temperature_high_)) {
131 ESP_LOGW(TAG, "'%s' - Target temperature low %.2f must be less than high %.2f",
132 LOG_STR_ARG(this->parent_->get_log_name()), this->target_temperature_low_,
133 this->target_temperature_high_);
134 this->target_temperature_low_ = NAN;
135 this->target_temperature_high_ = NAN;
136 }
137 }
138 if (!traits.get_supports_away_mode()) {
139 if (this->state_ & WATER_HEATER_STATE_AWAY) {
140 ESP_LOGW(TAG, "'%s' - Away mode not supported", LOG_STR_ARG(this->parent_->get_log_name()));
141 }
142 this->state_ &= ~WATER_HEATER_STATE_AWAY;
143 this->state_mask_ &= ~WATER_HEATER_STATE_AWAY;
144 }
145 // If ON/OFF not supported, device is always on - clear the flag silently
146 if (!traits.has_feature_flags(WATER_HEATER_SUPPORTS_ON_OFF)) {
147 this->state_ &= ~WATER_HEATER_STATE_ON;
148 this->state_mask_ &= ~WATER_HEATER_STATE_ON;
149 }
150}
151
153 this->set_has_state(true);
154 auto traits = this->get_traits();
155 ESP_LOGV(TAG,
156 "'%s' >>\n"
157 " Mode: %s",
158 LOG_STR_ARG(this->get_log_name()), LOG_STR_ARG(water_heater_mode_to_string(this->mode_)));
159 if (!std::isnan(this->current_temperature_)) {
160 ESP_LOGV(TAG, " Current Temperature: %.2f°C", this->current_temperature_);
161 }
163 ESP_LOGV(TAG, " Target Temperature: Low: %.2f°C High: %.2f°C", this->target_temperature_low_,
165 } else if (!std::isnan(this->target_temperature_)) {
166 ESP_LOGV(TAG, " Target Temperature: %.2f°C", this->target_temperature_);
167 }
168 if (this->state_ & WATER_HEATER_STATE_AWAY) {
169 ESP_LOGV(TAG, " Away: YES");
170 }
172 ESP_LOGV(TAG, " On: %s", (this->state_ & WATER_HEATER_STATE_ON) ? LOG_STR_LITERAL("YES") : LOG_STR_LITERAL("NO"));
173 }
174
175#if defined(USE_WATER_HEATER) && defined(USE_CONTROLLER_REGISTRY)
176 ControllerRegistry::notify_water_heater_update(this);
177#endif
178
179 SavedWaterHeaterState saved{};
180 saved.mode = this->mode_;
182 saved.target_temperature_low = this->target_temperature_low_;
183 saved.target_temperature_high = this->target_temperature_high_;
184 } else {
185 saved.target_temperature = this->target_temperature_;
186 }
187 saved.state = this->state_;
188 this->pref_.save(&saved);
189}
190
191optional<WaterHeaterCall> WaterHeater::restore_state_() {
193 SavedWaterHeaterState recovered{};
194 if (!this->pref_.load(&recovered))
195 return {};
196
197 auto traits = this->get_traits();
198 auto call = this->make_call();
199 call.set_mode(recovered.mode);
201 call.set_target_temperature_low(recovered.target_temperature_low);
202 call.set_target_temperature_high(recovered.target_temperature_high);
203 } else {
204 call.set_target_temperature(recovered.target_temperature);
205 }
206 call.set_away((recovered.state & WATER_HEATER_STATE_AWAY) != 0);
207 call.set_on((recovered.state & WATER_HEATER_STATE_ON) != 0);
208 return call;
209}
210
212 auto traits = this->traits();
213#ifdef USE_WATER_HEATER_VISUAL_OVERRIDES
214 if (!std::isnan(this->visual_min_temperature_override_)) {
216 }
217 if (!std::isnan(this->visual_max_temperature_override_)) {
219 }
220 if (!std::isnan(this->visual_target_temperature_step_override_)) {
222 }
223#endif
224 return traits;
225}
226
227// Water heater mode strings indexed by WaterHeaterMode enum (0-6): OFF, ECO, ELECTRIC, PERFORMANCE, HIGH_DEMAND,
228// HEAT_PUMP, GAS
229PROGMEM_STRING_TABLE(WaterHeaterModeStrings, "OFF", "ECO", "ELECTRIC", "PERFORMANCE", "HIGH_DEMAND", "HEAT_PUMP", "GAS",
230 "UNKNOWN");
231
233 return WaterHeaterModeStrings::get_log_str(static_cast<uint8_t>(mode), WaterHeaterModeStrings::LAST_INDEX);
234}
235
236void WaterHeater::dump_traits_(const char *tag) {
237 auto traits = this->get_traits();
238 ESP_LOGCONFIG(tag,
239 " Min Temperature: %.1f°C\n"
240 " Max Temperature: %.1f°C\n"
241 " Temperature Step: %.1f",
244 ESP_LOGCONFIG(tag, " Supports Two-Point Target Temperature: YES");
245 }
247 ESP_LOGCONFIG(tag, " Supports Away Mode: YES");
248 }
250 ESP_LOGCONFIG(tag, " Supports On/Off: YES");
251 }
253 ESP_LOGCONFIG(tag, " Supported Modes:");
255 ESP_LOGCONFIG(tag, " - %s", LOG_STR_ARG(water_heater_mode_to_string(m)));
256 }
257 }
258}
259
260} // namespace esphome::water_heater
BedjetMode mode
BedJet operating mode.
uint8_t m
Definition bl0906.h:1
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
void set_has_state(bool state)
constexpr bool empty() const
Check if the set is empty.
WaterHeaterCall & set_away(bool away)
WaterHeaterCall & set_target_temperature_high(float temperature)
WaterHeaterCall & set_mode(WaterHeaterMode mode)
WaterHeaterCall & set_target_temperature_low(float temperature)
WaterHeaterCall & set_on(bool on)
optional< WaterHeaterMode > mode_
WaterHeaterCall & set_target_temperature(float temperature)
virtual void control(const WaterHeaterCall &call)=0
optional< WaterHeaterCall > restore_state_()
Restore the state of the water heater, call this from your setup() method.
virtual WaterHeaterCallInternal make_call()=0
void dump_traits_(const char *tag)
Log the traits of this water heater for dump_config().
virtual WaterHeaterTraits get_traits()
virtual WaterHeaterTraits traits()=0
const WaterHeaterModeMask & get_supported_modes() const
void set_min_temperature(float min_temperature)
bool has_feature_flags(uint32_t flags) const
void set_target_temperature_step(float target_temperature_step)
void set_max_temperature(float max_temperature)
PROGMEM_STRING_TABLE(WaterHeaterModeStrings, "OFF", "ECO", "ELECTRIC", "PERFORMANCE", "HIGH_DEMAND", "HEAT_PUMP", "GAS", "UNKNOWN")
@ WATER_HEATER_STATE_ON
Water heater is on (not in standby)
@ WATER_HEATER_STATE_AWAY
Away/vacation mode is currently active.
@ WATER_HEATER_SUPPORTS_ON_OFF
The water heater can be turned on/off.
const LogString * water_heater_mode_to_string(WaterHeaterMode mode)
Convert the given WaterHeaterMode to a human-readable string for logging.
ESPHOME_LOG_TAG(TAG, "water_heater")
ESPHOME_ALWAYS_INLINE constexpr void assign_bits(T &value, std::type_identity_t< T > mask, bool on)
Set the bits of mask in value when on is true, clear them otherwise.
Definition helpers.h:1150
const void size_t len
Definition hal.h:68
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12