8static const char *
const TAG =
"mitsubishi_cn105.driver";
10static constexpr uint32_t WRITE_TIMEOUT_MS = 2000;
12static constexpr uint8_t TARGET_TEMPERATURE_ENC_A_OFFSET = 31;
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_SETTINGS = 0x02;
27static constexpr uint8_t STATUS_MSG_ROOM_TEMP = 0x03;
29static constexpr uint8_t PACKET_TYPE_WRITE_SETTINGS_REQUEST = 0x41;
30static constexpr uint8_t PACKET_TYPE_WRITE_SETTINGS_RESPONSE = 0x61;
32static constexpr std::array<std::optional<MitsubishiCN105::Mode>, 9> PROTOCOL_MODE_MAP = {
44static constexpr std::array<std::optional<MitsubishiCN105::FanMode>, 7> PROTOCOL_FAN_MODE_MAP = {
54template<
typename T,
size_t N>
55static constexpr std::optional<T> lookup(
const std::array<std::optional<T>, N> &table, uint8_t value) {
56 return (value < N) ? table[value] : std::nullopt;
59template<
typename T,
size_t N>
60static constexpr bool reverse_lookup(
const std::array<std::optional<T>, N> &table, T value, uint8_t &placeholder) {
61 for (
size_t i = 0; i < N; ++i) {
62 const auto &table_value = table[i];
63 if (table_value.has_value() && table_value == value) {
71static constexpr uint8_t
checksum(
const uint8_t *bytes,
size_t length) {
72 return static_cast<uint8_t
>(0xFC - std::accumulate(bytes, bytes +
length, uint8_t{0}));
75template<std::
size_t PayloadSize>
76static constexpr auto make_packet(uint8_t
type,
const std::array<uint8_t, PayloadSize> &payload) {
77 const size_t full_len = PayloadSize + HEADER_LEN + 1;
78 std::array<uint8_t, full_len> packet{PREAMBLE,
type, HEADER_BYTE_1, HEADER_BYTE_2,
static_cast<uint8_t
>(PayloadSize)};
79 std::copy_n(payload.begin(), PayloadSize, packet.begin() + HEADER_LEN);
80 packet.back() =
checksum(packet.data(), packet.size() - 1);
84static float decode_temperature(
int temp_a,
int temp_b,
int delta) {
85 return temp_b != 0 ? (temp_b - 128) / 2.0f : delta + temp_a;
88static constexpr auto CONNECT_PACKET = make_packet(PACKET_TYPE_CONNECT_REQUEST, CONNECT_REQUEST_PAYLOAD);
238 this->
send_packet_(make_packet(PACKET_TYPE_STATUS_REQUEST, payload));
248 case PACKET_TYPE_CONNECT_RESPONSE:
252 case PACKET_TYPE_STATUS_RESPONSE:
255 case PACKET_TYPE_WRITE_SETTINGS_RESPONSE:
260 ESP_LOGVV(TAG,
"RX unknown packet type 0x%02X",
type);
267 ESP_LOGVV(TAG,
"RX status packet too short");
271 const auto previous = this->
status_;
272 const auto msg_type = payload[0];
294 case STATUS_MSG_SETTINGS:
297 case STATUS_MSG_ROOM_TEMP:
301 ESP_LOGVV(TAG,
"RX unsupported status msg type 0x%02X", msg_type);
308 ESP_LOGVV(TAG,
"RX settings payload too short");
322 const bool i_see = payload[3] > 0x08;
335 ESP_LOGVV(TAG,
"RX room temperature payload too short");
361 if (!reverse_lookup(PROTOCOL_MODE_MAP,
mode, placeholder)) {
362 ESP_LOGD(TAG,
"Setting invalid mode: %u",
static_cast<uint8_t
>(
mode));
371 if (!reverse_lookup(PROTOCOL_FAN_MODE_MAP,
fan_mode, placeholder)) {
372 ESP_LOGD(TAG,
"Setting invalid fan mode: %u",
static_cast<uint8_t
>(
fan_mode));
380 std::array<uint8_t, REQUEST_PAYLOAD_LEN> payload = {0x01};
397 reverse_lookup(PROTOCOL_MODE_MAP, this->
status_.
mode, payload[4])) {
402 reverse_lookup(PROTOCOL_FAN_MODE_MAP, this->
status_.
fan_mode, payload[6])) {
406 this->
send_packet_(make_packet(PACKET_TYPE_WRITE_SETTINGS_REQUEST, payload));
412 return LOG_STR(
"Not connected");
414 return LOG_STR(
"Connecting");
416 return LOG_STR(
"Connected");
418 return LOG_STR(
"UpdatingStatus");
420 return LOG_STR(
"StatusUpdated");
422 return LOG_STR(
"ScheduleNextStatusUpdate");
424 return LOG_STR(
"WaitingForScheduledStatusUpdate");
426 return LOG_STR(
"ApplyingSettings");
428 return LOG_STR(
"SettingsApplied");
430 return LOG_STR(
"ReadTimeout");
432 return LOG_STR(
"Unknown");
435template<
typename Callback>
437 uint8_t watchdog = 64;
438 while (device.
available() > 0 && watchdog-- > 0) {
439 uint8_t &value = this->read_buffer_[this->read_pos_];
441 ESP_LOGW(TAG,
"UART read failed while data available");
445 switch (++this->read_pos_) {
447 if (value != PREAMBLE) {
456 if (value != HEADER_BYTE_1) {
462 if (value != HEADER_BYTE_2) {
468 static_assert(READ_BUFFER_SIZE > HEADER_LEN);
469 if (this->read_buffer_[HEADER_LEN - 1] >= READ_BUFFER_SIZE - HEADER_LEN) {
478 const size_t len_without_checksum = HEADER_LEN +
static_cast<size_t>(this->read_buffer_[HEADER_LEN - 1]);
479 if (this->read_pos_ <= len_without_checksum) {
483 if (
checksum(this->read_buffer_, len_without_checksum) != value) {
489 const bool processed =
490 callback(this->read_buffer_[1], this->read_buffer_ + HEADER_LEN, len_without_checksum - HEADER_LEN);
499 dump_buffer_vv(prefix, this->read_buffer_, this->read_pos_);
504#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
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)
void cancel_waiting_and_transition_to_(State state)
std::optional< uint32_t > last_room_temperature_update_ms_
bool process_status_packet_(const uint8_t *payload, size_t len)
static bool should_transition(State from, State to)
uint8_t current_status_msg_type_
bool should_request_room_temperature_() const
void set_state_(State new_state)
bool process_rx_packet_(uint8_t type, const uint8_t *payload, size_t len)
bool is_status_initialized() const
bool use_temperature_encoding_b_
uart::UARTDevice & device_
void did_transition_(State to)
std::optional< uint32_t > status_update_start_ms_
UpdateFlags pending_updates_
@ WAITING_FOR_SCHEDULED_STATUS_UPDATE
@ SCHEDULE_NEXT_STATUS_UPDATE
std::optional< uint32_t > write_timeout_start_ms_
void set_power(bool power_on)
static const LogString * state_to_string(State state)
void set_target_temperature(float target_temperature)
bool parse_status_settings_(const uint8_t *payload, size_t len)
uint32_t room_temperature_min_interval_ms_
uint32_t update_interval_ms_
void set_fan_mode(FanMode fan_mode)
bool is_room_temperature_enabled() const
void send_packet_(const uint8_t *packet, size_t len)
bool parse_status_payload_(uint8_t msg_type, const uint8_t *payload, size_t len)
FrameParser frame_parser_
bool parse_status_room_temperature_(const uint8_t *payload, size_t len)
bool read_byte(uint8_t *data)
void write_array(const uint8_t *data, size_t len)
uint32_t get_loop_time_ms()
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
Lightweight type-erased callback (8 bytes on 32-bit) that avoids std::function overhead.
bool has(UpdateFlag f) const