15using namespace water_heater_protocol;
32#ifdef USE_BINARY_SENSOR
33 PUBLISH_STATE(this->communication_binary_sensor_, connected);
41 const bool ready =
false;
47 ESP_LOGI(TAG,
"Local network status changed: %02X",
status);
52 uint8_t wire[MAX_WIRE_FRAME];
56 ESP_LOGV_HEX(TAG, wire,
length,
sizeof(wire),
"TX type=%02X: %s",
static_cast<uint8_t
>(
type));
76 static constexpr uint8_t VERSION_REQUEST[] = {0x00, 0x07};
77 static constexpr uint8_t STATUS_REQUEST[] = {0x4D, 0x01};
80 sizeof(VERSION_REQUEST), FrameType::FRAME_TYPE_VERSION);
83 FrameType::FRAME_TYPE_DEVICE_ID);
86 sizeof(STATUS_REQUEST), FrameType::FRAME_TYPE_STATUS);
95 ESP_LOGW(TAG,
"Heater communication lost: no valid status for %u ms",
105 ESP_LOGW(TAG,
"No matching response type=%02X in %u ms",
static_cast<uint8_t
>(this->
expected_type_),
112 ESP_LOGW(TAG,
"Control response timed out; discarding remaining commands");
130 this->
commands_[0] = {Parameter::PARAMETER_POWER, 0};
139 FrameType::FRAME_TYPE_CONFIRM);
152 FrameType::FRAME_TYPE_STATUS);
159 while (budget-- > 0 && this->
available() > 0) {
167 if ((this->
parser_.
data()[FRAME_FLAGS_OFFSET] & FRAME_FLAG_ENCRYPTED) != 0) {
168 ESP_LOGW(TAG,
"RX: XOR-encrypted frames are not supported and will be dropped");
170 ESP_LOGW(TAG,
"RX: unsupported flags or invalid checksum/CRC");
185 ESP_LOGV(TAG,
"Response matched type=%02X",
static_cast<uint8_t
>(
type));
189 case FrameType::FRAME_TYPE_GET_NETWORK_STATUS:
192 case FrameType::FRAME_TYPE_CONFIRM:
194 ESP_LOGD(TAG,
"Network status acknowledged");
198 case FrameType::FRAME_TYPE_INVALID:
199 ESP_LOGW(TAG,
"Appliance rejected request");
206 case FrameType::FRAME_TYPE_REPORT:
218 ESP_LOGD(TAG,
"Control confirmed: parameter=%02X value=%02X",
static_cast<uint8_t
>(command.
parameter), command.
value);
226 const auto type =
static_cast<FrameType>(frame[FRAME_TYPE_OFFSET]);
227 ESP_LOGV_HEX(TAG, frame, this->
parser_.
stored_size(), MAX_STORED_FRAME,
"RX decoded type=%02X flags=%02X: %s",
228 static_cast<uint8_t
>(
type), frame[FRAME_FLAGS_OFFSET]);
235 if (
status.mode == HonMode::HON_MODE_UNKNOWN && (recovered || this->reported_mode_ != HonMode::HON_MODE_UNKNOWN)) {
236 const uint8_t *payload = frame + FRAME_PAYLOAD_OFFSET;
238 "Unknown heater mode: payload[38..41]=%02X %02X %02X %02X; using last recognized active mode, or Electric "
240 payload[38], payload[39], payload[40], payload[41]);
251 ESP_LOGI(TAG,
"Heater communication established");
261 case HonMode::HON_MODE_NORMAL:
264 case HonMode::HON_MODE_ECO:
267 case HonMode::HON_MODE_BPS:
270 case HonMode::HON_MODE_UNKNOWN:
276 PUBLISH_STATE(this->primary_temperature_sensor_,
status.current_temperature);
277 PUBLISH_STATE(this->secondary_temperature_sensor_,
278 status.secondary_temperature <= MAX_REPORTED_TEMPERATURE ?
status.secondary_temperature : NAN);
279 PUBLISH_STATE(this->target_temperature_sensor_,
status.target_temperature);
280 PUBLISH_STATE(this->estimated_heating_time_sensor_,
status.estimated_heating_time);
282 PUBLISH_STATE(this->available_water_sensor_,
283 status.available_water <= 3 ?
static_cast<uint8_t
>(
status.available_water * 100 / 3) : NAN);
285#ifdef USE_BINARY_SENSOR
286 PUBLISH_STATE(this->power_binary_sensor_,
status.powered);
287 PUBLISH_STATE(this->heating_binary_sensor_,
status.heating);
289#ifdef USE_TEXT_SENSOR
290 PUBLISH_STATE(this->mode_text_sensor_,
291 HonModeLabels::get_progmem_str(
static_cast<uint8_t
>(
status.mode),
292 static_cast<uint8_t
>(HonMode::HON_MODE_UNKNOWN)));
298 LOG_WATER_HEATER(
"",
"Haier hOn",
this);
300 " Polling interval: %u ms\n"
301 " Communication timeout: %u ms",
304 LOG_SENSOR(
" ",
"Primary temperature", this->primary_temperature_sensor_);
305 LOG_SENSOR(
" ",
"Secondary temperature", this->secondary_temperature_sensor_);
306 LOG_SENSOR(
" ",
"Target temperature", this->target_temperature_sensor_);
307 LOG_SENSOR(
" ",
"Estimated heating time", this->estimated_heating_time_sensor_);
308 LOG_SENSOR(
" ",
"Available water", this->available_water_sensor_);
310#ifdef USE_BINARY_SENSOR
311 LOG_BINARY_SENSOR(
" ",
"Communication", this->communication_binary_sensor_);
312 LOG_BINARY_SENSOR(
" ",
"Power", this->power_binary_sensor_);
313 LOG_BINARY_SENSOR(
" ",
"Heating", this->heating_binary_sensor_);
315#ifdef USE_TEXT_SENSOR
316 LOG_TEXT_SENSOR(
" ",
"Mode", this->mode_text_sensor_);
328 return HonMode::HON_MODE_NORMAL;
330 return HonMode::HON_MODE_ECO;
332 return HonMode::HON_MODE_BPS;
334 return HonMode::HON_MODE_UNKNOWN;
339 using namespace water_heater;
341 const auto on =
call.get_on();
342 const bool off = (on.has_value() && !*on) || (
mode.has_value() && *
mode == WATER_HEATER_MODE_OFF);
356 ESP_LOGW(TAG,
"Control rejected: no recent appliance status");
360 ESP_LOGW(TAG,
"Control busy; wait for the previous request to finish");
372 mode.has_value() ? *
mode == WATER_HEATER_MODE_ELECTRIC : this->
reported_mode_ == HonMode::HON_MODE_NORMAL;
374 ESP_LOGW(TAG,
"Target can only be set in Normal mode");
383 if (!this->
is_on() && ((on.has_value() && *on) ||
mode.has_value()))
384 append(Parameter::PARAMETER_POWER, 1);
387 if (*mode != WATER_HEATER_MODE_ELECTRIC && this->
reported_mode_ != HonMode::HON_MODE_NORMAL)
388 append(Parameter::PARAMETER_ECO, 0);
390 case WATER_HEATER_MODE_ELECTRIC:
391 append(Parameter::PARAMETER_ECO, 0);
393 case WATER_HEATER_MODE_ECO:
394 append(Parameter::PARAMETER_ECO, 1);
396 case WATER_HEATER_MODE_PERFORMANCE:
397 append(Parameter::PARAMETER_BPS, 1);
404 append(Parameter::PARAMETER_TARGET_TEMPERATURE,
static_cast<uint8_t
>(
temperature - TARGET_TEMPERATURE_OFFSET));
408 using namespace water_heater;
412 WATER_HEATER_SUPPORTS_ON_OFF);
414 {WATER_HEATER_MODE_OFF, WATER_HEATER_MODE_ELECTRIC, WATER_HEATER_MODE_ECO, WATER_HEATER_MODE_PERFORMANCE});
BedjetMode mode
BedJet operating mode.
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void status_set_warning()
void set_interval(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a const char* name.
void status_clear_warning()
static constexpr uint32_t POLL_INTERVAL_MS
water_heater::WaterHeaterCallInternal make_call() override
void handle_frame_type_(water_heater_protocol::FrameType type)
static SUB_SENSOR(primary_temperature) SUB_SENSOR(secondary_temperature) SUB_SENSOR(target_temperature) SUB_SENSOR(estimated_heating_time) SUB_SENSOR(available_water) SUB_BINARY_SENSOR(communication) SUB_BINARY_SENSOR(power) SUB_BINARY_SENSOR(heating) SUB_TEXT_SENSOR(mode) protected constexpr uint32_t STATUS_POLL_INTERVAL_ID
void check_timeouts_(uint32_t now)
static constexpr uint8_t POLL_CYCLE
void update_network_status_()
static constexpr uint8_t NETWORK_NOT_READY
static constexpr uint32_t NETWORK_REFRESH_MS
static constexpr uint8_t MAX_BYTES_PER_LOOP
void send_request_(uint32_t now, Awaiting kind, water_heater_protocol::FrameType type, const uint8_t *payload, size_t size, water_heater_protocol::FrameType expected)
water_heater_protocol::FrameType expected_type_
static constexpr uint8_t NETWORK_READY
void publish_status_(const water_heater_protocol::Status &status)
void send_frame_(water_heater_protocol::FrameType type, const uint8_t *payload=nullptr, size_t size=0)
water_heater_protocol::HonMode reported_mode_
static constexpr uint8_t MIN_TARGET_TEMPERATURE
static constexpr uint8_t MAX_TARGET_TEMPERATURE
static constexpr uint32_t RESPONSE_TIMEOUT_MS
void publish_communication_(bool connected)
static constexpr uint32_t NETWORK_ACK_TIMEOUT_MS
void read_frames_(uint32_t now)
water_heater::WaterHeaterMode last_active_mode_
void control(const water_heater::WaterHeaterCall &call) override
void confirm_control_(const water_heater_protocol::Status &status)
static constexpr uint32_t NETWORK_STATUS_INTERVAL_MS
void send_pending_(uint32_t now)
uint32_t communication_timeout_
static constexpr uint32_t CONTROL_STATUS_MAX_AGE_MS
static constexpr uint32_t FRAME_GAP_MS
void dump_config() override
water_heater::WaterHeaterTraits traits() override
void send_next_control_(uint32_t now)
water_heater_protocol::FrameParser parser_
std::array< water_heater_protocol::Command, 4 > commands_
bool feed(uint8_t value)
Returns true once a valid frame is complete. A complete but invalid frame leaves complete() set.
size_t size() const
Logical bytes received, including the checksum and CRC; may exceed MAX_STORED_FRAME.
const uint8_t * data() const
The stored head of the frame, at most MAX_STORED_FRAME bytes; valid until the next feed or reset.
size_t stored_size() const
bool read_byte(uint8_t *data)
void write_array(const uint8_t *data, size_t len)
virtual void publish_state()
void set_state_flag_(uint32_t flag, bool value)
Set or clear a state flag. Should only be called from control().
bool is_on() const
Check if the water heater is on.
float target_temperature_
float current_temperature_
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)
void set_min_temperature(float min_temperature)
void set_target_temperature_step(float target_temperature_step)
void set_max_temperature(float max_temperature)
bool decode_status(const uint8_t *frame, size_t size, Status &status)
Decodes a status report or a polled status; false for any other frame or an implausible one.
bool command_confirmed(const Command &command, const Status &status)
True when a status shows that the command took effect.
size_t encode_frame(FrameType type, const uint8_t *payload, size_t size, uint8_t *wire)
Writes a clear-text frame with up to MAX_PAYLOAD bytes of payload into wire, which must hold MAX_WIRE...
void encode_control(const Command &command, uint8_t *payload)
Fills the four-byte control payload for a command.
PROGMEM_STRING_TABLE(HonModeLabels, "Unknown", "Normal", "ECO", "BPS")
ESPHOME_LOG_TAG(TAG, "haier_water_heater")
ESPHOME_ALWAYS_INLINE bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
@ WATER_HEATER_MODE_ELECTRIC
@ WATER_HEATER_MODE_PERFORMANCE
@ WATER_HEATER_STATE_ON
Water heater is on (not in standby)
Application App
Global storage of Application pointer - only one Application can exist.
uint8_t current_temperature
uint8_t target_temperature