ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
mitsubishi_cn105.h
Go to the documentation of this file.
1#pragma once
2
6
7#include <cmath>
8#include <span>
9
11
13
14// PREAMBLE, packet type, HEADER_BYTE_1, HEADER_BYTE_2, payload length, payload, checksum
15static constexpr size_t MAX_FRAME_SIZE = 32;
16static constexpr size_t HEADER_LEN = 5;
17static constexpr uint8_t PREAMBLE = 0xFC;
18static constexpr uint8_t HEADER_BYTE_1 = 0x01;
19static constexpr uint8_t HEADER_BYTE_2 = 0x30;
20
21// The type byte sits between PREAMBLE and the fixed header bytes, so FrameHeader cannot check them
22inline bool frame_header_valid(const uint8_t *frame) { return frame[2] == HEADER_BYTE_1 && frame[3] == HEADER_BYTE_2; }
23// One checksum byte follows the payload
24using PayloadLength = PayloadLengthU8<LengthField{.offset = HEADER_LEN - 1, .trailer = 1}>;
26
28 public:
29 enum class Mode : uint8_t {
30 HEAT,
31 DRY,
32 COOL,
34 AUTO,
35 UNKNOWN,
36 };
37
38 enum class FanMode : uint8_t {
39 AUTO,
40 QUIET,
41 SPEED_1,
42 SPEED_2,
43 SPEED_3,
44 SPEED_4,
45 UNKNOWN,
46 };
47
48 enum class VaneMode : uint8_t {
49 AUTO,
55 SWING,
56 UNKNOWN,
57 };
58
59 // Positions are numbered from the far left (1) to the far right (5).
60 enum class WideVaneMode : uint8_t {
61 POSITION_1, // far left
62 POSITION_2, // left
63 POSITION_3, // center
64 POSITION_4, // right
65 POSITION_5, // far right
66 SPLIT, // left and right
67 SWING,
68 UNKNOWN,
69 };
70
71 // Direction of the current run. 0x06 does not report it, so in AUTO it is inferred when the run starts.
77
78 struct Status {
80 float room_temperature{NAN};
81 bool power_on{false};
82 bool operating{false}; // held false while the unit winds down after a run ended
87 };
88
89 explicit MitsubishiCN105(uart::UARTDevice &device) : device_(device) {}
90
91 void initialize();
92 bool update();
93
95 void set_update_interval(uint32_t interval_ms) { this->update_interval_ms_ = interval_ms; }
96
100 this->telemetry_request_min_interval_ms_ = interval_ms;
101 }
102
103 const Status &status() const { return this->status_; }
105 return this->is_telemetry_polling_enabled() ? !std::isnan(this->status_.room_temperature)
106 : !std::isnan(this->status_.target_temperature);
107 }
109
110 void set_power(bool power_on);
112 void set_mode(Mode mode);
114 void set_vane_mode(VaneMode vane_mode);
118 RunDirection run_direction() const { return this->run_direction_; }
119
120 protected:
134
135 enum class PropertyId : uint8_t {
136 TEMPERATURE = 0,
137 POWER = 1,
138 MODE = 2,
139 FAN = 3,
140 VANE = 4,
141 WIDE_VANE = 5,
143 };
144
145 struct UpdateFlags {
146 void set(PropertyId id) { this->mask_.insert(id); }
147 void clear(PropertyId id) { this->mask_.erase(id); }
148 bool any() const { return !this->mask_.empty(); }
149 bool contains(PropertyId id) const { return this->mask_.count(id); }
150 bool contains_only(PropertyId id) const { return this->mask_.get_mask() == Mask{id}.get_mask(); }
151
152 protected:
153 using Mask =
156 };
157
158 // An early refresh reads 0x06 after a mode or power change; it is spent until the next regular telemetry read.
164
167 bool set_wide_vane_high_bit : 1 {false};
168 };
169
170 friend struct Property;
171
172 void set_state_(State new_state);
173 void did_transition_(State to);
174 bool process_rx_packet_(uint8_t type, const uint8_t *payload, size_t len);
175 bool process_status_packet_(const uint8_t *payload, size_t len);
176 bool parse_status_payload_(uint8_t msg_type, const uint8_t *payload, size_t len);
177 void send_packet_(std::span<const uint8_t> packet);
178 void update_status_();
179 bool should_request_telemetry_() const;
181 bool is_early_telemetry_refresh_() const;
182 bool is_refresh_due_() const;
184 uint8_t next_status_msg_type_() const;
189 bool hold_if_run_ended_(bool was_on, Mode was_mode);
191 void update_run_direction_(bool was_operating);
192 void apply_settings_();
193 bool has_timed_out_(uint32_t timeout) const { return ((get_loop_time_ms() - this->operation_start_ms_) >= timeout); }
194 void set_remote_temperature_half_deg_(uint8_t temperature_half_deg);
195 static bool should_transition(State from, State to);
196 static const LogString *state_to_string(State state);
197
199 // Default 1s; legacy climate-owned hub compatibility relies on this when update_interval is omitted.
200 // Remove legacy note in 2027.2.0.
204 // Default 60s; legacy climate-owned hub compatibility relies on this when current_temperature_min_interval is
205 // omitted. Remove legacy note in 2027.2.0.
216
217 static constexpr uint8_t REMOTE_TEMPERATURE_DISABLED = 0;
219 bool status_changed_ : 1 {false};
220 bool has_telemetry_update_ : 1 {false}; // last_telemetry_update_ms_ is set
222 // Consecutive 0x06 timeouts; 0x06 is no longer requested once it reaches the maximum.
224 static constexpr uint8_t OPERATING_STATUS_MAX_MISSES = 3;
226 // Non-zero while a "running" 0x06 report is taken as wind-down from the previous mode or power state: one plus the
227 // number of 0x06 reads since the hold started. The last read was at wind_down_read_ms_.
228 uint8_t wind_down_reads_ : 4 {0};
229 // Power and mode before the first local change that has not been sent yet; the hold starts from what is sent.
230 bool unsent_from_power_on_ : 1 {false};
232 static constexpr uint8_t WIND_DOWN_HELD_READS = 9;
233};
234
235} // namespace esphome::mitsubishi_cn105
BedjetMode mode
BedJet operating mode.
constexpr size_t count(ValueType value) const
Check if the set contains a specific value (std::set compatibility) Returns 1 if present,...
constexpr bitmask_t get_mask() const
Get the raw bitmask value for optimized operations.
constexpr void insert(ValueType value)
Add a single value to the set (std::set compatibility)
constexpr void erase(ValueType value)
Remove a value from the set (std::set compatibility)
constexpr bool empty() const
Check if the set is empty.
Collects binary frames that give their own length, of up to N bytes, in one or more FrameFormats; the...
void set_telemetry_request_min_interval(uint32_t interval_ms)
bool process_status_packet_(const uint8_t *payload, size_t len)
static bool should_transition(State from, State to)
static constexpr uint8_t OPERATING_STATUS_MAX_MISSES
void set_remote_temperature_half_deg_(uint8_t temperature_half_deg)
bool process_rx_packet_(uint8_t type, const uint8_t *payload, size_t len)
LengthFrameReader< MAX_FRAME_SIZE, Frame > reader_
bool has_timed_out_(uint32_t timeout) const
static constexpr uint8_t REMOTE_TEMPERATURE_DISABLED
bool hold_if_run_ended_(bool was_on, Mode was_mode)
void send_packet_(std::span< const uint8_t > packet)
static const LogString * state_to_string(State state)
void set_target_temperature(float target_temperature)
bool parse_status_payload_(uint8_t msg_type, const uint8_t *payload, size_t len)
void set_update_interval(uint32_t interval_ms)
float target_temperature
Definition climate.h:0
ClimateFanMode fan_mode
Definition climate.h:3
uint8_t type
bool state
Definition fan.h:2
PayloadLengthU8< LengthField{.offset=HEADER_LEN - 1,.trailer=1}> PayloadLength
bool frame_header_valid(const uint8_t *frame)
const void size_t len
Definition hal.h:68
constexpr uint32_t SCHEDULER_DONT_RUN
Definition component.h:64
static void uint32_t
FrameFormat option: CHECK(frame) runs once the first BYTES bytes are in; a frame it fails is not a fr...
One frame type for LengthFrameReader: a FrameHeader, a length rule from above, then any of the option...
offset: where the length field starts, counted from the first header byte.
uint16_t temperature
Definition sun_gtil2.cpp:12