13static constexpr uint32_t RESPONSE_TIMEOUT_MS = 2000;
15static constexpr size_t REQUEST_PAYLOAD_LEN = 0x10;
17static constexpr uint8_t PACKET_TYPE_CONNECT_REQUEST = 0x5A;
18static constexpr uint8_t PACKET_TYPE_CONNECT_RESPONSE = 0x7A;
19static constexpr std::array<uint8_t, 2> CONNECT_REQUEST_PAYLOAD = {0xCA, 0x01};
21static constexpr uint8_t PACKET_TYPE_STATUS_REQUEST = 0x42;
22static constexpr uint8_t PACKET_TYPE_STATUS_RESPONSE = 0x62;
23static constexpr uint8_t STATUS_MSG_NONE = 0x00;
24static constexpr uint8_t STATUS_MSG_SETTINGS = 0x02;
25static constexpr uint8_t STATUS_MSG_ROOM_TEMPERATURE = 0x03;
26static constexpr uint8_t STATUS_MSG_OPERATING_STATUS = 0x06;
28static constexpr uint8_t PACKET_TYPE_WRITE_SETTINGS_REQUEST = 0x41;
29static constexpr uint8_t PACKET_TYPE_WRITE_SETTINGS_RESPONSE = 0x61;
32static constexpr uint32_t WIND_DOWN_READ_INTERVAL_MS = 10000;
34static constexpr uint8_t
checksum(
const uint8_t *bytes,
size_t length) {
38static void dump_buffer_vv(
const char *prefix,
const uint8_t *data,
size_t len) {
39 ESP_LOGVV_HEX(TAG, data,
len, MAX_FRAME_SIZE,
"%s (%zu): %s", prefix,
len);
42template<std::
size_t PayloadSize>
43static constexpr auto make_packet(uint8_t
type,
const std::array<uint8_t, PayloadSize> &payload) {
44 const size_t full_len = PayloadSize + HEADER_LEN + 1;
45 std::array<uint8_t, full_len> packet{PREAMBLE,
type, HEADER_BYTE_1, HEADER_BYTE_2,
static_cast<uint8_t
>(PayloadSize)};
46 std::copy_n(payload.begin(), PayloadSize, packet.begin() + HEADER_LEN);
47 packet.back() =
checksum(packet.data(), packet.size() - 1);
51static constexpr auto CONNECT_PACKET = make_packet(PACKET_TYPE_CONNECT_REQUEST, CONNECT_REQUEST_PAYLOAD);
86 ESP_LOGI(TAG,
"No operating status response, climate action stays idle");
102 bool changed =
false;
103 const auto handle = [
this, &changed](
const uint8_t *frame,
size_t len) {
104 const size_t len_without_checksum =
len - 1;
105 if (
checksum(frame, len_without_checksum) != frame[len_without_checksum]) {
106 dump_buffer_vv(
"RX invalid: checksum mismatch", frame,
len);
109 dump_buffer_vv(
"RX", frame,
len);
110 changed |= this->
process_rx_packet_(frame[1], frame + HEADER_LEN, len_without_checksum - HEADER_LEN);
114 ESP_LOGW(TAG,
"UART read failed while data available");
195 if (next_msg_type == STATUS_MSG_NONE) {
263 if (to == Mode::AUTO) {
264 return direction != RunDirection::RUN_DIRECTION_NONE;
266 const RunDirection implied = to == Mode::HEAT ? RunDirection::RUN_DIRECTION_HEATING
267 : to == Mode::COOL ? RunDirection::RUN_DIRECTION_COOLING
268 : RunDirection::RUN_DIRECTION_NONE;
269 return implied != RunDirection::RUN_DIRECTION_NONE &&
270 (
direction == implied || (from == Mode::AUTO &&
direction == RunDirection::RUN_DIRECTION_NONE));
276 if (!this->
status_.
operating || !was_on || this->is_operating_status_unsupported_()) {
280 (was_mode == this->status_.mode || continues_run(was_mode, this->
status_.
mode, this->run_direction_))) {
339 case STATUS_MSG_SETTINGS:
342 case STATUS_MSG_ROOM_TEMPERATURE:
345 return STATUS_MSG_NONE;
360 dump_buffer_vv(
"TX", packet.data(), packet.size());
367 this->
send_packet_(make_packet(PACKET_TYPE_STATUS_REQUEST, payload));
372 case PACKET_TYPE_CONNECT_RESPONSE:
376 case PACKET_TYPE_STATUS_RESPONSE:
379 case PACKET_TYPE_WRITE_SETTINGS_RESPONSE:
384 ESP_LOGVV(TAG,
"RX unknown packet type 0x%02X",
type);
391 ESP_LOGVV(TAG,
"RX status packet too short");
395 const auto previous = this->
status_;
396 const auto msg_type = payload[0];
402 if (power_or_mode_changed) {
412 bool changed = power_or_mode_changed || previous.fan_mode != this->
status_.
fan_mode ||
429 case STATUS_MSG_SETTINGS:
430 if (!decoder.decode_settings(this->status_)) {
431 ESP_LOGVV(TAG,
"RX settings payload too short");
436 case STATUS_MSG_ROOM_TEMPERATURE:
437 if (!decoder.decode_room_temperature(this->status_)) {
438 ESP_LOGVV(TAG,
"RX room temperature payload too short");
443 case STATUS_MSG_OPERATING_STATUS:
444 if (!decoder.decode_operating_status(this->status_)) {
445 ESP_LOGVV(TAG,
"RX operating status payload too short");
451 ESP_LOGV(TAG,
"Ignoring running report during wind-down");
461 ESP_LOGVV(TAG,
"RX unsupported status msg type 0x%02X", msg_type);
468 ESP_LOGD(TAG,
"Ignoring NaN remote temperature");
472 ESP_LOGD(TAG,
"Ignoring out-of-range remote temperature: %.1f",
temperature);
505 ESP_LOGD(TAG,
"Ignoring invalid mode: %u",
static_cast<uint8_t
>(
mode));
511 ESP_LOGD(TAG,
"Ignoring invalid fan mode: %u",
static_cast<uint8_t
>(
fan_mode));
517 ESP_LOGD(TAG,
"Ignoring invalid vane mode: %u",
static_cast<uint8_t
>(vane_mode));
523 ESP_LOGD(TAG,
"Ignoring invalid wide vane mode: %u",
static_cast<uint8_t
>(wide_vane_mode));
528 std::array<uint8_t, REQUEST_PAYLOAD_LEN> payload{};
541 encoder.encode_settings(this->
status_);
544 this->
send_packet_(make_packet(PACKET_TYPE_WRITE_SETTINGS_REQUEST, payload));
548PROGMEM_STRING_TABLE(StateStrings,
"Not connected",
"Connecting",
"Connected",
"UpdatingStatus",
"StatusUpdated",
549 "DeferredStatusRequest",
"ScheduleNextStatusUpdate",
"WaitingForScheduledStatusUpdate",
550 "ApplyingSettings",
"SettingsApplied",
"ReadTimeout",
"Unknown");
BedjetMode mode
BedJet operating mode.
void reset()
Forget the partial frame, for a request/response protocol starting a new exchange.
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
LengthFrameReader< MAX_FRAME_SIZE, Frame > reader_
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_
void save_unsent_power_and_mode_()
void set_vane_mode(VaneMode vane_mode)
void write_array(const uint8_t *data, size_t len)
bool read_frames(R &reader, F &&handle, uint32_t now, uint32_t &last_rx, uint32_t gap_ms=250)
Feed the waiting bytes to a LengthFrameReader; on a pass with none, gap_ms after last_rx,...
PROGMEM_STRING_TABLE(StateStrings, "Not connected", "Connecting", "Connected", "UpdatingStatus", "StatusUpdated", "DeferredStatusRequest", "ScheduleNextStatusUpdate", "WaitingForScheduledStatusUpdate", "ApplyingSettings", "SettingsApplied", "ReadTimeout", "Unknown")
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...
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)
spi_device_handle_t handle