ESPHome 2025.5.0
Loading...
Searching...
No Matches
midea_ir.cpp
Go to the documentation of this file.
1#include "midea_ir.h"
2#include "midea_data.h"
3#include "esphome/core/log.h"
6
7namespace esphome {
8namespace midea_ir {
9
10static const char *const TAG = "midea_ir.climate";
11
12void ControlData::set_temp(float temp) {
13 uint8_t min;
14 if (this->get_fahrenheit()) {
15 min = MIDEA_TEMPF_MIN;
17 } else {
18 min = MIDEA_TEMPC_MIN;
20 }
21 this->set_value_(2, lroundf(temp) - min, 31);
22}
23
24float ControlData::get_temp() const {
25 const uint8_t temp = this->get_value_(2, 31);
26 if (this->get_fahrenheit())
27 return fahrenheit_to_celsius(static_cast<float>(temp + MIDEA_TEMPF_MIN));
28 return static_cast<float>(temp + MIDEA_TEMPC_MIN);
29}
30
32 // In FAN_AUTO, modes COOL, HEAT and FAN_ONLY bit #5 in byte #1 must be set
33 const uint8_t value = this->get_value_(1, 31);
34 if (value == 0 || value == 3 || value == 4)
35 this->set_mask_(1, true, 32);
36 // In FAN_ONLY mode we need to set all temperature bits
37 if (this->get_mode_() == MODE_FAN_ONLY)
38 this->set_mask_(2, true, 31);
39}
40
42 switch (mode) {
44 this->set_power_(false);
45 return;
47 this->set_mode_(MODE_COOL);
48 break;
50 this->set_mode_(MODE_DRY);
51 break;
54 break;
56 this->set_mode_(MODE_HEAT);
57 break;
58 default:
59 this->set_mode_(MODE_AUTO);
60 break;
61 }
62 this->set_power_(true);
63}
64
81
83 switch (mode) {
86 break;
89 break;
92 break;
93 default:
95 break;
96 }
97}
98
100 switch (this->get_fan_mode_()) {
101 case FAN_LOW:
103 case FAN_MEDIUM:
105 case FAN_HIGH:
107 default:
109 }
110}
111
113 // swing and preset resets after unit powered off
114 if (call.get_mode() == climate::CLIMATE_MODE_OFF) {
117 } else if (call.get_swing_mode().has_value() && ((*call.get_swing_mode() == climate::CLIMATE_SWING_OFF &&
118 this->swing_mode == climate::CLIMATE_SWING_VERTICAL) ||
119 (*call.get_swing_mode() == climate::CLIMATE_SWING_VERTICAL &&
120 this->swing_mode == climate::CLIMATE_SWING_OFF))) {
121 this->swing_ = true;
122 } else if (call.get_preset().has_value() &&
123 ((*call.get_preset() == climate::CLIMATE_PRESET_NONE && this->preset == climate::CLIMATE_PRESET_BOOST) ||
124 (*call.get_preset() == climate::CLIMATE_PRESET_BOOST && this->preset == climate::CLIMATE_PRESET_NONE))) {
125 this->boost_ = true;
126 }
128}
129
131 data.finalize();
132 auto transmit = this->transmitter_->transmit();
133 remote_base::MideaProtocol().encode(transmit.get_data(), data);
134 transmit.perform();
135}
136
138 if (this->swing_) {
140 this->transmit_(data);
141 this->swing_ = false;
142 return;
143 }
144 if (this->boost_) {
146 this->transmit_(data);
147 this->boost_ = false;
148 return;
149 }
150 ControlData data;
151 data.set_fahrenheit(this->fahrenheit_);
152 data.set_temp(this->target_temperature);
153 data.set_mode(this->mode);
156 data.fix();
157 this->transmit_(data);
158}
159
161 auto midea = remote_base::MideaProtocol().decode(data);
162 if (midea.has_value())
163 return this->on_midea_(*midea);
164 return coolix::CoolixClimate::on_coolix(this, data);
165}
166
167bool MideaIR::on_midea_(const MideaData &data) {
168 ESP_LOGV(TAG, "Decoded Midea IR data: %s", data.to_string().c_str());
169 if (data.type() == MideaData::MIDEA_TYPE_CONTROL) {
170 const ControlData status = data;
171 if (status.get_mode() != climate::CLIMATE_MODE_FAN_ONLY)
172 this->target_temperature = status.get_temp();
173 this->mode = status.get_mode();
174 this->fan_mode = status.get_fan_mode();
175 if (status.get_sleep_preset()) {
177 } else if (this->preset == climate::CLIMATE_PRESET_SLEEP) {
179 }
180 this->publish_state();
181 return true;
182 }
183 if (data.type() == MideaData::MIDEA_TYPE_SPECIAL) {
184 switch (data[1]) {
188 break;
192 break;
193 }
194 this->publish_state();
195 return true;
196 }
197
198 return false;
199}
200
201} // namespace midea_ir
202} // namespace esphome
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:33
const optional< ClimateSwingMode > & get_swing_mode() const
Definition climate.cpp:282
const optional< ClimatePreset > & get_preset() const
Definition climate.cpp:280
ClimateMode mode
The active mode of the climate device.
Definition climate.h:173
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:199
float target_temperature
The target temperature of the climate device.
Definition climate.h:186
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition climate.h:202
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:395
optional< ClimatePreset > preset
The active preset of the climate device.
Definition climate.h:208
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:108
void set_mode(ClimateMode mode)
Definition midea_ir.cpp:41
void set_sleep_preset(bool value)
Definition midea_data.h:29
void set_fahrenheit(bool value)
Definition midea_data.h:32
ClimateMode get_mode() const
Definition midea_ir.cpp:65
FanMode get_fan_mode_() const
Definition midea_data.h:52
void set_fan_mode(ClimateFanMode mode)
Definition midea_ir.cpp:82
void set_power_(bool value)
Definition midea_data.h:55
ClimateFanMode get_fan_mode() const
Definition midea_ir.cpp:99
void set_fan_mode_(FanMode mode)
Definition midea_data.h:51
bool on_midea_(const MideaData &data)
Definition midea_ir.cpp:167
void transmit_state() override
Transmit via IR the state of this climate controller.
Definition midea_ir.cpp:137
void control(const climate::ClimateCall &call) override
Override control to change settings of the climate device.
Definition midea_ir.cpp:112
bool on_receive(remote_base::RemoteReceiveData data) override
Handle received IR Buffer.
Definition midea_ir.cpp:160
void transmit_(MideaData &data)
Definition midea_ir.cpp:130
static const uint8_t TURBO_TOGGLE
Definition midea_data.h:88
static const uint8_t VSWING_TOGGLE
Definition midea_data.h:87
bool has_value() const
Definition optional.h:87
value_type value_or(U const &v) const
Definition optional.h:93
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.
@ 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:11
const uint8_t MIDEA_TEMPF_MAX
Definition midea_ir.h:13
const uint8_t MIDEA_TEMPC_MIN
Definition midea_ir.h:10
const uint8_t MIDEA_TEMPF_MIN
Definition midea_ir.h:12
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
constexpr float celsius_to_fahrenheit(float value)
Convert degrees Celsius to degrees Fahrenheit.
Definition helpers.h:482
constexpr float fahrenheit_to_celsius(float value)
Convert degrees Fahrenheit to degrees Celsius.
Definition helpers.h:484
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition helpers.h:101