ESPHome  2021.11.3
air_conditioner.cpp
Go to the documentation of this file.
1 #ifdef USE_ARDUINO
2 
3 #include "esphome/core/log.h"
4 #include "air_conditioner.h"
5 #include "ac_adapter.h"
6 
7 namespace esphome {
8 namespace midea {
9 namespace ac {
10 
11 static void set_sensor(Sensor *sensor, float value) {
12  if (sensor != nullptr && (!sensor->has_state() || sensor->get_raw_state() != value))
13  sensor->publish_state(value);
14 }
15 
16 template<typename T> void update_property(T &property, const T &value, bool &flag) {
17  if (property != value) {
18  property = value;
19  flag = true;
20  }
21 }
22 
24  bool need_publish = false;
25  update_property(this->target_temperature, this->base_.getTargetTemp(), need_publish);
26  update_property(this->current_temperature, this->base_.getIndoorTemp(), need_publish);
27  auto mode = Converters::to_climate_mode(this->base_.getMode());
28  update_property(this->mode, mode, need_publish);
29  auto swing_mode = Converters::to_climate_swing_mode(this->base_.getSwingMode());
30  update_property(this->swing_mode, swing_mode, need_publish);
31  // Preset
32  auto preset = this->base_.getPreset();
35  need_publish = true;
37  need_publish = true;
38  }
39  // Fan mode
40  auto fan_mode = this->base_.getFanMode();
43  need_publish = true;
45  need_publish = true;
46  }
47  if (need_publish)
48  this->publish_state();
49  set_sensor(this->outdoor_sensor_, this->base_.getOutdoorTemp());
50  set_sensor(this->power_sensor_, this->base_.getPowerUsage());
51  set_sensor(this->humidity_sensor_, this->base_.getIndoorHum());
52 }
53 
55  dudanov::midea::ac::Control ctrl{};
56  if (call.get_target_temperature().has_value())
57  ctrl.targetTemp = call.get_target_temperature().value();
58  if (call.get_swing_mode().has_value())
59  ctrl.swingMode = Converters::to_midea_swing_mode(call.get_swing_mode().value());
60  if (call.get_mode().has_value())
61  ctrl.mode = Converters::to_midea_mode(call.get_mode().value());
62  if (call.get_preset().has_value())
63  ctrl.preset = Converters::to_midea_preset(call.get_preset().value());
64  else if (call.get_custom_preset().has_value())
65  ctrl.preset = Converters::to_midea_preset(call.get_custom_preset().value());
66  if (call.get_fan_mode().has_value())
67  ctrl.fanMode = Converters::to_midea_fan_mode(call.get_fan_mode().value());
68  else if (call.get_custom_fan_mode().has_value())
70  this->base_.control(ctrl);
71 }
72 
74  auto traits = ClimateTraits();
84  /* + MINIMAL SET OF CAPABILITIES */
95  if (this->base_.getAutoconfStatus() == dudanov::midea::AUTOCONF_OK)
96  Converters::to_climate_traits(traits, this->base_.getCapabilities());
97  return traits;
98 }
99 
101  ESP_LOGCONFIG(Constants::TAG, "MideaDongle:");
102  ESP_LOGCONFIG(Constants::TAG, " [x] Period: %dms", this->base_.getPeriod());
103  ESP_LOGCONFIG(Constants::TAG, " [x] Response timeout: %dms", this->base_.getTimeout());
104  ESP_LOGCONFIG(Constants::TAG, " [x] Request attempts: %d", this->base_.getNumAttempts());
105 #ifdef USE_REMOTE_TRANSMITTER
106  ESP_LOGCONFIG(Constants::TAG, " [x] Using RemoteTransmitter");
107 #endif
108  if (this->base_.getAutoconfStatus() == dudanov::midea::AUTOCONF_OK) {
109  this->base_.getCapabilities().dump();
110  } else if (this->base_.getAutoconfStatus() == dudanov::midea::AUTOCONF_ERROR) {
111  ESP_LOGW(Constants::TAG,
112  "Failed to get 0xB5 capabilities report. Suggest to disable it in config and manually set your "
113  "appliance options.");
114  }
116 }
117 
118 /* ACTIONS */
119 
120 void AirConditioner::do_follow_me(float temperature, bool beeper) {
121 #ifdef USE_REMOTE_TRANSMITTER
122  IrFollowMeData data(static_cast<uint8_t>(lroundf(temperature)), beeper);
123  this->transmitter_.transmit(data);
124 #else
125  ESP_LOGW(Constants::TAG, "Action needs remote_transmitter component");
126 #endif
127 }
128 
130 #ifdef USE_REMOTE_TRANSMITTER
131  IrSpecialData data(0x01);
132  this->transmitter_.transmit(data);
133 #else
134  ESP_LOGW(Constants::TAG, "Action needs remote_transmitter component");
135 #endif
136 }
137 
139  if (this->base_.getCapabilities().supportLightControl()) {
140  this->base_.displayToggle();
141  } else {
142 #ifdef USE_REMOTE_TRANSMITTER
143  IrSpecialData data(0x08);
144  this->transmitter_.transmit(data);
145 #else
146  ESP_LOGW(Constants::TAG, "Action needs remote_transmitter component");
147 #endif
148  }
149 }
150 
151 } // namespace ac
152 } // namespace midea
153 } // namespace esphome
154 
155 #endif // USE_ARDUINO
This class is used to encode all control actions on a climate device.
Definition: climate.h:33
value_type const & value() const
Definition: optional.h:89
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition: climate.h:204
static ClimateFanMode to_climate_fan_mode(MideaFanMode fan_mode)
Definition: ac_adapter.cpp:88
static const char *const TAG
Definition: ac_adapter.h:22
void set_supported_custom_presets(std::set< std::string > supported_custom_presets)
void set_visual_temperature_step(float temperature_step)
std::set< std::string > supported_custom_presets_
float target_temperature
The target temperature of the climate device.
Definition: climate.h:183
static const std::string & to_custom_climate_fan_mode(MideaFanMode fan_mode)
Definition: ac_adapter.cpp:111
const optional< ClimateMode > & get_mode() const
Definition: climate.cpp:259
This class contains all static data for climate devices.
static bool is_custom_midea_fan_mode(MideaFanMode fan_mode)
Definition: ac_adapter.cpp:101
void set_visual_min_temperature(float visual_min_temperature)
void do_follow_me(float temperature, bool beeper=false)
ClimateMode mode
The active mode of the climate device.
Definition: climate.h:175
static void to_climate_traits(ClimateTraits &traits, const dudanov::midea::ac::Capabilities &capabilities)
Definition: ac_adapter.cpp:158
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition: climate.h:179
bool has_value() const
Definition: optional.h:87
void set_supported_presets(std::set< ClimatePreset > presets)
std::set< ClimateSwingMode > supported_swing_modes_
void add_supported_preset(ClimatePreset preset)
static MideaPreset to_midea_preset(ClimatePreset preset)
Definition: ac_adapter.cpp:126
std::set< ClimatePreset > supported_presets_
void add_supported_swing_mode(ClimateSwingMode mode)
void transmit(IrData &data)
const optional< std::string > & get_custom_preset() const
Definition: climate.cpp:271
const optional< ClimatePreset > & get_preset() const
Definition: climate.cpp:270
optional< ClimatePreset > preset
The active preset of the climate device.
Definition: climate.h:210
static const std::string & to_custom_climate_preset(MideaPreset preset)
Definition: ac_adapter.cpp:154
void set_supported_modes(std::set< ClimateMode > modes)
void control(const ClimateCall &call) override
std::set< std::string > supported_custom_fan_modes_
static bool is_custom_midea_preset(MideaPreset preset)
Definition: ac_adapter.cpp:152
void set_visual_max_temperature(float visual_max_temperature)
const optional< std::string > & get_custom_fan_mode() const
Definition: climate.cpp:269
void add_supported_fan_mode(ClimateFanMode mode)
const optional< float > & get_target_temperature() const
Definition: climate.cpp:260
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition: climate.cpp:380
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:523
static ClimatePreset to_climate_preset(MideaPreset preset)
Definition: ac_adapter.cpp:139
static ClimateSwingMode to_climate_swing_mode(MideaSwingMode mode)
Definition: ac_adapter.cpp:49
std::set< ClimateMode > supported_modes_
static MideaSwingMode to_midea_swing_mode(ClimateSwingMode mode)
Definition: ac_adapter.cpp:62
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition: climate.h:198
const optional< ClimateFanMode > & get_fan_mode() const
Definition: climate.cpp:268
bool set_custom_preset_(const std::string &preset)
Set custom preset. Reset primary preset. Return true if preset has been changed.
Definition: climate.cpp:529
ClimateTraits traits() override
void update_property(T &property, const T &value, bool &flag)
static MideaMode to_midea_mode(ClimateMode mode)
Definition: ac_adapter.cpp:32
const optional< ClimateSwingMode > & get_swing_mode() const
Definition: climate.cpp:272
Library based on https://github.com/miguelbalboa/rfid and adapted to ESPHome by . ...
Definition: a4988.cpp:4
void set_supported_swing_modes(std::set< ClimateSwingMode > modes)
void dump_traits_(const char *tag)
Definition: climate.cpp:533
static ClimateMode to_climate_mode(MideaMode mode)
Definition: ac_adapter.cpp:15
void set_supports_current_temperature(bool supports_current_temperature)
void set_supported_custom_fan_modes(std::set< std::string > supported_custom_fan_modes)
bool set_preset_(ClimatePreset preset)
Set preset. Reset custom preset. Return true if preset has been changed.
Definition: climate.cpp:527
void add_supported_mode(ClimateMode mode)
bool set_fan_mode_(ClimateFanMode mode)
Set fan mode. Reset custom fan mode. Return true if fan mode has been changed.
Definition: climate.cpp:519
static MideaFanMode to_midea_fan_mode(ClimateFanMode fan_mode)
Definition: ac_adapter.cpp:75