12static constexpr uint32_t RESPONSE_TIMEOUT_MS = 2000;
14static constexpr size_t REQUEST_PAYLOAD_LEN = 0x10;
15static constexpr size_t HEADER_LEN = 5;
16static constexpr uint8_t PREAMBLE = 0xFC;
17static constexpr uint8_t HEADER_BYTE_1 = 0x01;
18static constexpr uint8_t HEADER_BYTE_2 = 0x30;
20static constexpr uint8_t PACKET_TYPE_CONNECT_REQUEST = 0x5A;
21static constexpr uint8_t PACKET_TYPE_CONNECT_RESPONSE = 0x7A;
22static constexpr std::array<uint8_t, 2> CONNECT_REQUEST_PAYLOAD = {0xCA, 0x01};
24static constexpr uint8_t PACKET_TYPE_STATUS_REQUEST = 0x42;
25static constexpr uint8_t PACKET_TYPE_STATUS_RESPONSE = 0x62;
26static constexpr uint8_t STATUS_MSG_NONE = 0x00;
27static constexpr uint8_t STATUS_MSG_SETTINGS = 0x02;
28static constexpr uint8_t STATUS_MSG_ROOM_TEMPERATURE = 0x03;
29static constexpr uint8_t STATUS_MSG_OPERATING_STATUS = 0x06;
31static constexpr uint8_t PACKET_TYPE_WRITE_SETTINGS_REQUEST = 0x41;
32static constexpr uint8_t PACKET_TYPE_WRITE_SETTINGS_RESPONSE = 0x61;
35static constexpr uint32_t WIND_DOWN_READ_INTERVAL_MS = 10000;
37static constexpr uint8_t
checksum(
const uint8_t *bytes,
size_t length) {
41template<std::
size_t PayloadSize>
42static constexpr auto make_packet(uint8_t
type,
const std::array<uint8_t, PayloadSize> &payload) {
43 const size_t full_len = PayloadSize + HEADER_LEN + 1;
44 std::array<uint8_t, full_len> packet{PREAMBLE,
type, HEADER_BYTE_1, HEADER_BYTE_2,
static_cast<uint8_t
>(PayloadSize)};
45 std::copy_n(payload.begin(), PayloadSize, packet.begin() + HEADER_LEN);
46 packet.back() =
checksum(packet.data(), packet.size() - 1);
50static constexpr auto CONNECT_PACKET = make_packet(PACKET_TYPE_CONNECT_REQUEST, CONNECT_REQUEST_PAYLOAD);
85 ESP_LOGI(TAG,
"No operating status response, climate action stays idle");
182 if (next_msg_type == STATUS_MSG_NONE) {
250 if (to == Mode::AUTO) {
251 return direction != RunDirection::RUN_DIRECTION_NONE;
253 const RunDirection implied = to == Mode::HEAT ? RunDirection::RUN_DIRECTION_HEATING
254 : to == Mode::COOL ? RunDirection::RUN_DIRECTION_COOLING
255 : RunDirection::RUN_DIRECTION_NONE;
256 return implied != RunDirection::RUN_DIRECTION_NONE &&
257 (
direction == implied || (from == Mode::AUTO &&
direction == RunDirection::RUN_DIRECTION_NONE));
263 if (!this->
status_.
operating || !was_on || this->is_operating_status_unsupported_()) {
267 (was_mode == this->status_.mode || continues_run(was_mode, this->
status_.
mode, this->run_direction_))) {
326 case STATUS_MSG_SETTINGS:
329 case STATUS_MSG_ROOM_TEMPERATURE:
332 return STATUS_MSG_NONE;
354 this->
send_packet_(make_packet(PACKET_TYPE_STATUS_REQUEST, payload));
359 case PACKET_TYPE_CONNECT_RESPONSE:
363 case PACKET_TYPE_STATUS_RESPONSE:
366 case PACKET_TYPE_WRITE_SETTINGS_RESPONSE:
371 ESP_LOGVV(TAG,
"RX unknown packet type 0x%02X",
type);
378 ESP_LOGVV(TAG,
"RX status packet too short");
382 const auto previous = this->
status_;
383 const auto msg_type = payload[0];
389 if (power_or_mode_changed) {
399 bool changed = power_or_mode_changed || previous.fan_mode != this->
status_.
fan_mode ||
416 case STATUS_MSG_SETTINGS:
417 if (!decoder.decode_settings(this->status_)) {
418 ESP_LOGVV(TAG,
"RX settings payload too short");
423 case STATUS_MSG_ROOM_TEMPERATURE:
424 if (!decoder.decode_room_temperature(this->status_)) {
425 ESP_LOGVV(TAG,
"RX room temperature payload too short");
430 case STATUS_MSG_OPERATING_STATUS:
431 if (!decoder.decode_operating_status(this->status_)) {
432 ESP_LOGVV(TAG,
"RX operating status payload too short");
438 ESP_LOGV(TAG,
"Ignoring running report during wind-down");
448 ESP_LOGVV(TAG,
"RX unsupported status msg type 0x%02X", msg_type);
455 ESP_LOGD(TAG,
"Ignoring NaN remote temperature");
459 ESP_LOGD(TAG,
"Ignoring out-of-range remote temperature: %.1f",
temperature);
492 ESP_LOGD(TAG,
"Ignoring invalid mode: %u",
static_cast<uint8_t
>(
mode));
498 ESP_LOGD(TAG,
"Ignoring invalid fan mode: %u",
static_cast<uint8_t
>(
fan_mode));
504 ESP_LOGD(TAG,
"Ignoring invalid vane mode: %u",
static_cast<uint8_t
>(vane_mode));
510 ESP_LOGD(TAG,
"Ignoring invalid wide vane mode: %u",
static_cast<uint8_t
>(wide_vane_mode));
515 std::array<uint8_t, REQUEST_PAYLOAD_LEN> payload{};
528 encoder.encode_settings(this->
status_);
531 this->
send_packet_(make_packet(PACKET_TYPE_WRITE_SETTINGS_REQUEST, payload));
537 return LOG_STR(
"Not connected");
539 return LOG_STR(
"Connecting");
541 return LOG_STR(
"Connected");
543 return LOG_STR(
"UpdatingStatus");
545 return LOG_STR(
"StatusUpdated");
547 return LOG_STR(
"DeferredStatusRequest");
549 return LOG_STR(
"ScheduleNextStatusUpdate");
551 return LOG_STR(
"WaitingForScheduledStatusUpdate");
553 return LOG_STR(
"ApplyingSettings");
555 return LOG_STR(
"SettingsApplied");
557 return LOG_STR(
"ReadTimeout");
559 return LOG_STR(
"Unknown");
562template<
typename Callback>
564 uint8_t watchdog = 64;
565 while (device.
available() > 0 && watchdog-- > 0) {
566 uint8_t &
value = this->read_buffer_[this->read_pos_];
568 ESP_LOGW(TAG,
"UART read failed while data available");
572 switch (++this->read_pos_) {
574 if (
value != PREAMBLE) {
583 if (value != HEADER_BYTE_1) {
589 if (value != HEADER_BYTE_2) {
595 static_assert(READ_BUFFER_SIZE > HEADER_LEN);
596 if (this->read_buffer_[HEADER_LEN - 1] >= READ_BUFFER_SIZE - HEADER_LEN) {
605 const size_t len_without_checksum = HEADER_LEN +
static_cast<size_t>(this->read_buffer_[HEADER_LEN - 1]);
606 if (this->read_pos_ <= len_without_checksum) {
610 if (
checksum(this->read_buffer_, len_without_checksum) !=
value) {
616 const bool processed =
617 callback(this->read_buffer_[1], this->read_buffer_ + HEADER_LEN, len_without_checksum - HEADER_LEN);
626 dump_buffer_vv(prefix, this->read_buffer_, this->read_pos_);
631 ESP_LOGVV_HEX(TAG, data,
len, READ_BUFFER_SIZE,
"%s (%zu): %s", prefix,
len);
BedjetMode mode
BedJet operating mode.
void reset_and_dump_buffer_(const char *prefix)
bool read_and_parse(uart::UARTDevice &device, Callback &&callback)
static void dump_buffer_vv(const char *prefix, const uint8_t *data, size_t len)
bool is_refresh_due_() const
bool is_telemetry_interval_elapsed_() const
bool should_request_telemetry_() const
bool take_status_changed_()
uint32_t last_telemetry_update_ms_
bool process_status_packet_(const uint8_t *payload, size_t len)
uint32_t operation_start_ms_
RunDirection run_direction_
static bool should_transition(State from, State to)
uint8_t current_status_msg_type_
static constexpr uint8_t OPERATING_STATUS_MAX_MISSES
void set_state_(State new_state)
PropertyContext property_context_
void set_remote_temperature_half_deg_(uint8_t temperature_half_deg)
void update_run_direction_(bool was_operating)
uint8_t operating_status_misses_
bool unsent_from_power_on_
bool process_rx_packet_(uint8_t type, const uint8_t *payload, size_t len)
uint32_t status_update_wait_credit_ms_
bool is_status_initialized() const
bool is_telemetry_polling_enabled() const
void clear_remote_temperature()
uart::UARTDevice & device_
void did_transition_(State to)
void set_remote_temperature(float temperature)
bool has_timed_out_(uint32_t timeout) const
static constexpr uint8_t REMOTE_TEMPERATURE_DISABLED
bool is_operating_status_unsupported_() const
uint32_t telemetry_request_min_interval_ms_
UpdateFlags pending_updates_
bool hold_if_run_ended_(bool was_on, Mode was_mode)
@ WAITING_FOR_SCHEDULED_STATUS_UPDATE
@ DEFERRED_STATUS_REQUEST
@ SCHEDULE_NEXT_STATUS_UPDATE
void send_packet_(std::span< const uint8_t > packet)
void request_telemetry_refresh_()
void set_power(bool power_on)
static constexpr uint8_t WIND_DOWN_HELD_READS
TelemetryRefresh telemetry_refresh_
static const LogString * state_to_string(State state)
void set_wide_vane_mode(WideVaneMode mode)
bool has_telemetry_update_
void set_target_temperature(float target_temperature)
bool is_early_telemetry_refresh_() const
uint32_t wind_down_read_ms_
uint32_t update_interval_ms_
void set_fan_mode(FanMode fan_mode)
uint8_t next_status_msg_type_() const
bool parse_status_payload_(uint8_t msg_type, const uint8_t *payload, size_t len)
@ TELEMETRY_REFRESH_SPENT
@ TELEMETRY_REFRESH_REQUESTED
uint8_t remote_temperature_half_deg_
FrameParser frame_parser_
void save_unsent_power_and_mode_()
void set_vane_mode(VaneMode vane_mode)
bool read_byte(uint8_t *data)
void write_array(const uint8_t *data, size_t len)
ESPHOME_LOG_TAG(TAG, "mitsubishi_cn105.driver")
uint32_t get_loop_time_ms()
constexpr uint8_t sum_bytes(const uint8_t *data, size_t len, uint8_t sum=0)
Add up the bytes of data with size len, truncated to 8 bits: the checksum many serial and IR protocol...
Lightweight type-erased callback (8 bytes on 32-bit) that avoids std::function overhead.
WideVaneMode wide_vane_mode
bool contains(PropertyId id) const
bool contains_only(PropertyId id) const
static bool validate_and_set(Value value, Status &status, Mask &mask)