ESPHome 2026.5.1
Loading...
Searching...
No Matches
midea_ir.cpp
Go to the documentation of this file.
1#include "midea_ir.h"
2#include "midea_data.h"
4#include "esphome/core/log.h"
6
7namespace esphome::midea_ir {
8
9static const char *const TAG = "midea_ir.climate";
10
11void ControlData::set_temp(float temp) {
12 uint8_t min;
13 if (this->get_fahrenheit()) {
14 min = MIDEA_TEMPF_MIN;
15 temp = esphome::clamp<float>(celsius_to_fahrenheit(temp), MIDEA_TEMPF_MIN, MIDEA_TEMPF_MAX);
16 } else {
17 min = MIDEA_TEMPC_MIN;
18 temp = esphome::clamp<float>(temp, MIDEA_TEMPC_MIN, MIDEA_TEMPC_MAX);
19 }
20 this->set_value_(2, lroundf(temp) - min, 31);
21}
22
23float ControlData::get_temp() const {
24 const uint8_t temp = this->get_value_(2, 31);
25 if (this->get_fahrenheit())
26 return fahrenheit_to_celsius(static_cast<float>(temp + MIDEA_TEMPF_MIN));
27 return static_cast<float>(temp + MIDEA_TEMPC_MIN);
28}
29
31 // In FAN_AUTO, modes COOL, HEAT and FAN_ONLY bit #5 in byte #1 must be set
32 const uint8_t value = this->get_value_(1, 31);
33 if (value == 0 || value == 3 || value == 4)
34 this->set_mask_(1, true, 32);
35 // In FAN_ONLY mode we need to set all temperature bits
36 if (this->get_mode_() == MODE_FAN_ONLY)
37 this->set_mask_(2, true, 31);
38}
39
41 switch (mode) {
43 this->set_power_(false);
44 return;
46 this->set_mode_(MODE_COOL);
47 break;
49 this->set_mode_(MODE_DRY);
50 break;
53 break;
55 this->set_mode_(MODE_HEAT);
56 break;
57 default:
58 this->set_mode_(MODE_AUTO);
59 break;
60 }
61 this->set_power_(true);
62}
63
80
82 switch (mode) {
85 break;
88 break;
91 break;
92 default:
94 break;
95 }
96}
97
99 switch (this->get_fan_mode_()) {
100 case FAN_LOW:
102 case FAN_MEDIUM:
104 case FAN_HIGH:
106 default:
108 }
109}
110
112 // swing and preset resets after unit powered off
113 if (call.get_mode() == climate::CLIMATE_MODE_OFF) {
116 } else {
117 auto swing = call.get_swing_mode();
118 if (swing.has_value() &&
119 ((*swing == climate::CLIMATE_SWING_OFF && this->swing_mode == climate::CLIMATE_SWING_VERTICAL) ||
120 (*swing == climate::CLIMATE_SWING_VERTICAL && this->swing_mode == climate::CLIMATE_SWING_OFF))) {
121 this->swing_ = true;
122 } else {
123 auto preset = call.get_preset();
124 if (preset.has_value() &&
127 this->boost_ = true;
128 }
129 }
130 }
132}
133
135 data.finalize();
136 auto transmit = this->transmitter_->transmit();
137 remote_base::MideaProtocol().encode(transmit.get_data(), data);
138 transmit.perform();
139}
140
142 if (this->swing_) {
144 this->transmit_(data);
145 this->swing_ = false;
146 return;
147 }
148 if (this->boost_) {
150 this->transmit_(data);
151 this->boost_ = false;
152 return;
153 }
154 ControlData data;
155 data.set_fahrenheit(this->fahrenheit_);
156 data.set_temp(this->target_temperature);
157 data.set_mode(this->mode);
160 data.fix();
161 this->transmit_(data);
162}
163
165 auto midea = remote_base::MideaProtocol().decode(data);
166 if (midea.has_value())
167 return this->on_midea_(*midea);
168 return coolix::CoolixClimate::on_coolix(this, data);
169}
170
171bool MideaIR::on_midea_(const MideaData &data) {
172 char buf[MideaData::TO_STR_BUFFER_SIZE];
173 ESP_LOGV(TAG, "Decoded Midea IR data: %s", data.to_str(buf));
174 if (data.type() == MideaData::MIDEA_TYPE_CONTROL) {
175 const ControlData status = data;
176 if (status.get_mode() != climate::CLIMATE_MODE_FAN_ONLY)
177 this->target_temperature = status.get_temp();
178 this->mode = status.get_mode();
179 this->fan_mode = status.get_fan_mode();
180 if (status.get_sleep_preset()) {
182 } else if (this->preset == climate::CLIMATE_PRESET_SLEEP) {
184 }
185 this->publish_state();
186 return true;
187 }
188 if (data.type() == MideaData::MIDEA_TYPE_SPECIAL) {
189 switch (data[1]) {
193 break;
197 break;
198 }
199 this->publish_state();
200 return true;
201 }
202
203 return false;
204}
205
206} // namespace esphome::midea_ir
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
This class is used to encode all control actions on a climate device.
Definition climate.h:34
const optional< ClimatePreset > & get_preset() const
Definition climate.cpp:315
ClimateMode mode
The active mode of the climate device.
Definition climate.h:293
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:287
float target_temperature
The target temperature of the climate device.
Definition climate.h:274
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition climate.h:299
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:437
optional< ClimatePreset > preset
The active preset of the climate device.
Definition climate.h:290
void control(const climate::ClimateCall &call) override
Override control to change settings of the climate device.
static bool on_coolix(climate::Climate *parent, remote_base::RemoteReceiveData data)
This static method can be used in other climate components that accept the Coolix protocol.
Definition coolix.cpp:107
void set_mode(ClimateMode mode)
Definition midea_ir.cpp:40
void set_sleep_preset(bool value)
Definition midea_data.h:28
ClimateMode get_mode() const
Definition midea_ir.cpp:64
FanMode get_fan_mode_() const
Definition midea_data.h:51
void set_fan_mode(ClimateFanMode mode)
Definition midea_ir.cpp:81
void set_power_(bool value)
Definition midea_data.h:54
ClimateFanMode get_fan_mode() const
Definition midea_ir.cpp:98
void set_fan_mode_(FanMode mode)
Definition midea_data.h:50
bool on_midea_(const MideaData &data)
Definition midea_ir.cpp:171
void transmit_state() override
Transmit via IR the state of this climate controller.
Definition midea_ir.cpp:141
void control(const climate::ClimateCall &call) override
Override control to change settings of the climate device.
Definition midea_ir.cpp:111
bool on_receive(remote_base::RemoteReceiveData data) override
Handle received IR Buffer.
Definition midea_ir.cpp:164
void transmit_(MideaData &data)
Definition midea_ir.cpp:134
static const uint8_t TURBO_TOGGLE
Definition midea_data.h:87
static const uint8_t VSWING_TOGGLE
Definition midea_data.h:86
void set_value_(uint8_t idx, uint8_t value, uint8_t mask=255, uint8_t shift=0)
uint8_t get_value_(uint8_t idx, uint8_t mask=255, uint8_t shift=0) const
void set_mask_(uint8_t idx, bool state, uint8_t mask=255)
void encode(RemoteTransmitData *dst, const MideaData &src) override
optional< MideaData > decode(RemoteReceiveData src) override
@ CLIMATE_PRESET_NONE
No preset is active.
@ CLIMATE_PRESET_BOOST
Device is in boost preset.
@ CLIMATE_PRESET_SLEEP
Device is prepared for sleep.
@ CLIMATE_SWING_OFF
The swing mode is set to Off.
@ CLIMATE_SWING_VERTICAL
The fan mode is set to Vertical.
ClimateMode
Enum for all modes a climate device can be in.
@ CLIMATE_MODE_DRY
The climate device is set to dry/humidity mode.
@ CLIMATE_MODE_FAN_ONLY
The climate device only has the fan enabled, no heating or cooling is taking place.
@ CLIMATE_MODE_HEAT
The climate device is set to heat to reach the target temperature.
@ CLIMATE_MODE_COOL
The climate device is set to cool to reach the target temperature.
@ CLIMATE_MODE_HEAT_COOL
The climate device is set to heat/cool to reach the target temperature.
@ CLIMATE_MODE_OFF
The climate device is off.
ClimateFanMode
NOTE: If adding values, update ClimateFanModeMask in climate_traits.h to use the new last value.
@ CLIMATE_FAN_MEDIUM
The fan mode is set to Medium.
@ CLIMATE_FAN_AUTO
The fan mode is set to Auto.
@ CLIMATE_FAN_LOW
The fan mode is set to Low.
@ CLIMATE_FAN_HIGH
The fan mode is set to High.
const uint8_t MIDEA_TEMPC_MAX
Definition midea_ir.h:10
const uint8_t MIDEA_TEMPF_MAX
Definition midea_ir.h:12
const uint8_t MIDEA_TEMPC_MIN
Definition midea_ir.h:9
const uint8_t MIDEA_TEMPF_MIN
Definition midea_ir.h:11
constexpr float celsius_to_fahrenheit(float value)
Convert degrees Celsius to degrees Fahrenheit.
Definition helpers.h:1610
constexpr float fahrenheit_to_celsius(float value)
Convert degrees Fahrenheit to degrees Celsius.
Definition helpers.h:1612