ESPHome 2025.5.0
Loading...
Searching...
No Matches
air_conditioner.cpp
Go to the documentation of this file.
1#ifdef USE_ARDUINO
2
3#include "esphome/core/log.h"
5#include "air_conditioner.h"
6#include "ac_adapter.h"
7#include <cmath>
8#include <cstdint>
9
10namespace esphome {
11namespace midea {
12namespace ac {
13
14static void set_sensor(Sensor *sensor, float value) {
15 if (sensor != nullptr && (!sensor->has_state() || sensor->get_raw_state() != value))
16 sensor->publish_state(value);
17}
18
19template<typename T> void update_property(T &property, const T &value, bool &flag) {
20 if (property != value) {
21 property = value;
22 flag = true;
23 }
24}
25
27 bool need_publish = false;
28 update_property(this->target_temperature, this->base_.getTargetTemp(), need_publish);
29 update_property(this->current_temperature, this->base_.getIndoorTemp(), need_publish);
30 auto mode = Converters::to_climate_mode(this->base_.getMode());
31 update_property(this->mode, mode, need_publish);
32 auto swing_mode = Converters::to_climate_swing_mode(this->base_.getSwingMode());
33 update_property(this->swing_mode, swing_mode, need_publish);
34 // Preset
35 auto preset = this->base_.getPreset();
38 need_publish = true;
40 need_publish = true;
41 }
42 // Fan mode
43 auto fan_mode = this->base_.getFanMode();
46 need_publish = true;
48 need_publish = true;
49 }
50 if (need_publish)
51 this->publish_state();
52 set_sensor(this->outdoor_sensor_, this->base_.getOutdoorTemp());
53 set_sensor(this->power_sensor_, this->base_.getPowerUsage());
54 set_sensor(this->humidity_sensor_, this->base_.getIndoorHum());
55}
56
58 dudanov::midea::ac::Control ctrl{};
59 if (call.get_target_temperature().has_value())
60 ctrl.targetTemp = call.get_target_temperature().value();
61 if (call.get_swing_mode().has_value())
62 ctrl.swingMode = Converters::to_midea_swing_mode(call.get_swing_mode().value());
63 if (call.get_mode().has_value())
64 ctrl.mode = Converters::to_midea_mode(call.get_mode().value());
65 if (call.get_preset().has_value()) {
66 ctrl.preset = Converters::to_midea_preset(call.get_preset().value());
67 } else if (call.get_custom_preset().has_value()) {
68 ctrl.preset = Converters::to_midea_preset(call.get_custom_preset().value());
69 }
70 if (call.get_fan_mode().has_value()) {
71 ctrl.fanMode = Converters::to_midea_fan_mode(call.get_fan_mode().value());
72 } else if (call.get_custom_fan_mode().has_value()) {
73 ctrl.fanMode = Converters::to_midea_fan_mode(call.get_custom_fan_mode().value());
74 }
75 this->base_.control(ctrl);
76}
77
104
106 ESP_LOGCONFIG(Constants::TAG, "MideaDongle:");
107 ESP_LOGCONFIG(Constants::TAG, " [x] Period: %dms", this->base_.getPeriod());
108 ESP_LOGCONFIG(Constants::TAG, " [x] Response timeout: %dms", this->base_.getTimeout());
109 ESP_LOGCONFIG(Constants::TAG, " [x] Request attempts: %d", this->base_.getNumAttempts());
110#ifdef USE_REMOTE_TRANSMITTER
111 ESP_LOGCONFIG(Constants::TAG, " [x] Using RemoteTransmitter");
112#endif
113 if (this->base_.getAutoconfStatus() == dudanov::midea::AUTOCONF_OK) {
114 this->base_.getCapabilities().dump();
115 } else if (this->base_.getAutoconfStatus() == dudanov::midea::AUTOCONF_ERROR) {
116 ESP_LOGW(Constants::TAG,
117 "Failed to get 0xB5 capabilities report. Suggest to disable it in config and manually set your "
118 "appliance options.");
119 }
121}
122
123/* ACTIONS */
124
125void AirConditioner::do_follow_me(float temperature, bool use_fahrenheit, bool beeper) {
126#ifdef USE_REMOTE_TRANSMITTER
127 // Check if temperature is finite (not NaN or infinite)
128 if (!std::isfinite(temperature)) {
129 ESP_LOGW(Constants::TAG, "Follow me action requires a finite temperature, got: %f", temperature);
130 return;
131 }
132
133 // Round and convert temperature to long, then clamp and convert it to uint8_t
134 uint8_t temp_uint8 =
135 static_cast<uint8_t>(esphome::clamp<long>(std::lroundf(temperature), 0L, static_cast<long>(UINT8_MAX)));
136
137 char temp_symbol = use_fahrenheit ? 'F' : 'C';
138 ESP_LOGD(Constants::TAG, "Follow me action called with temperature: %.5f °%c, rounded to: %u °%c", temperature,
139 temp_symbol, temp_uint8, temp_symbol);
140
141 // Create and transmit the data
142 IrFollowMeData data(temp_uint8, use_fahrenheit, beeper);
143 this->transmitter_.transmit(data);
144#else
145 ESP_LOGW(Constants::TAG, "Action needs remote_transmitter component");
146#endif
147}
148
150#ifdef USE_REMOTE_TRANSMITTER
151 IrSpecialData data(0x01);
152 this->transmitter_.transmit(data);
153#else
154 ESP_LOGW(Constants::TAG, "Action needs remote_transmitter component");
155#endif
156}
157
159 if (this->base_.getCapabilities().supportLightControl()) {
160 this->base_.displayToggle();
161 } else {
162#ifdef USE_REMOTE_TRANSMITTER
163 IrSpecialData data(0x08);
164 this->transmitter_.transmit(data);
165#else
166 ESP_LOGW(Constants::TAG, "Action needs remote_transmitter component");
167#endif
168 }
169}
170
171} // namespace ac
172} // namespace midea
173} // namespace esphome
174
175#endif // USE_ARDUINO
This class is used to encode all control actions on a climate device.
Definition climate.h:33
ClimateMode mode
The active mode of the climate device.
Definition climate.h:173
bool set_custom_fan_mode_(const std::string &mode)
Set custom fan mode. Reset primary fan mode. Return true if fan mode has been changed.
Definition climate.cpp:559
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:199
bool set_custom_preset_(const std::string &preset)
Set custom preset. Reset primary preset. Return true if preset has been changed.
Definition climate.cpp:565
float target_temperature
The target temperature of the climate device.
Definition climate.h:186
void dump_traits_(const char *tag)
Definition climate.cpp:569
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition climate.h:202
bool set_preset_(ClimatePreset preset)
Set preset. Reset custom preset. Return true if preset has been changed.
Definition climate.cpp:563
bool set_fan_mode_(ClimateFanMode mode)
Set fan mode. Reset custom fan mode. Return true if fan mode has been changed.
Definition climate.cpp:555
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:179
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
This class contains all static data for climate devices.
void set_visual_max_temperature(float visual_max_temperature)
void set_supported_modes(std::set< ClimateMode > modes)
const std::set< climate::ClimatePreset > & get_supported_presets() const
void set_supported_custom_fan_modes(std::set< std::string > supported_custom_fan_modes)
void add_supported_fan_mode(ClimateFanMode mode)
void set_supported_custom_presets(std::set< std::string > supported_custom_presets)
void set_visual_temperature_step(float temperature_step)
void add_supported_preset(ClimatePreset preset)
void set_visual_min_temperature(float visual_min_temperature)
void set_supported_swing_modes(std::set< ClimateSwingMode > modes)
void set_supported_presets(std::set< ClimatePreset > presets)
void add_supported_mode(ClimateMode mode)
void set_supports_current_temperature(bool supports_current_temperature)
void add_supported_swing_mode(ClimateSwingMode mode)
const std::set< ClimateMode > & get_supported_modes() const
const std::set< ClimateSwingMode > & get_supported_swing_modes() const
std::set< ClimateMode > supported_modes_
std::set< ClimateSwingMode > supported_swing_modes_
std::set< ClimatePreset > supported_presets_
std::set< std::string > supported_custom_presets_
void do_follow_me(float temperature, bool use_fahrenheit, bool beeper=false)
std::set< std::string > supported_custom_fan_modes_
void control(const ClimateCall &call) override
static const char *const TAG
Definition ac_adapter.h:22
static ClimateFanMode to_climate_fan_mode(MideaFanMode fan_mode)
static ClimateSwingMode to_climate_swing_mode(MideaSwingMode mode)
static const std::string & to_custom_climate_fan_mode(MideaFanMode fan_mode)
static MideaSwingMode to_midea_swing_mode(ClimateSwingMode mode)
static MideaPreset to_midea_preset(ClimatePreset preset)
static ClimateMode to_climate_mode(MideaMode mode)
static MideaFanMode to_midea_fan_mode(ClimateFanMode fan_mode)
static const std::string & to_custom_climate_preset(MideaPreset preset)
static ClimatePreset to_climate_preset(MideaPreset preset)
static bool is_custom_midea_fan_mode(MideaFanMode fan_mode)
static bool is_custom_midea_preset(MideaPreset preset)
static MideaMode to_midea_mode(ClimateMode mode)
static void to_climate_traits(ClimateTraits &traits, const dudanov::midea::ac::Capabilities &capabilities)
@ CLIMATE_PRESET_NONE
No preset is active.
@ CLIMATE_SWING_OFF
The swing mode is set to Off.
@ 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.
void update_property(T &property, const T &value, bool &flag)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition helpers.h:101
uint16_t temperature
Definition sun_gtil2.cpp:12