ESPHome  2024.5.5
heatpumpir.cpp
Go to the documentation of this file.
1 #include "heatpumpir.h"
2 
3 #ifdef USE_ARDUINO
4 
5 #include <map>
6 #include "ir_sender_esphome.h"
7 #include "HeatpumpIRFactory.h"
8 #include "esphome/core/log.h"
9 
10 namespace esphome {
11 namespace heatpumpir {
12 
13 static const char *const TAG = "heatpumpir.climate";
14 
15 const std::map<Protocol, std::function<HeatpumpIR *()>> PROTOCOL_CONSTRUCTOR_MAP = {
16  {PROTOCOL_AUX, []() { return new AUXHeatpumpIR(); }}, // NOLINT
17  {PROTOCOL_BALLU, []() { return new BalluHeatpumpIR(); }}, // NOLINT
18  {PROTOCOL_CARRIER_MCA, []() { return new CarrierMCAHeatpumpIR(); }}, // NOLINT
19  {PROTOCOL_CARRIER_NQV, []() { return new CarrierNQVHeatpumpIR(); }}, // NOLINT
20  {PROTOCOL_DAIKIN_ARC417, []() { return new DaikinHeatpumpARC417IR(); }}, // NOLINT
21  {PROTOCOL_DAIKIN_ARC480, []() { return new DaikinHeatpumpARC480A14IR(); }}, // NOLINT
22  {PROTOCOL_DAIKIN, []() { return new DaikinHeatpumpIR(); }}, // NOLINT
23  {PROTOCOL_ELECTROLUXYAL, []() { return new ElectroluxYALHeatpumpIR(); }}, // NOLINT
24  {PROTOCOL_FUEGO, []() { return new FuegoHeatpumpIR(); }}, // NOLINT
25  {PROTOCOL_FUJITSU_AWYZ, []() { return new FujitsuHeatpumpIR(); }}, // NOLINT
26  {PROTOCOL_GREE, []() { return new GreeGenericHeatpumpIR(); }}, // NOLINT
27  {PROTOCOL_GREEYAA, []() { return new GreeYAAHeatpumpIR(); }}, // NOLINT
28  {PROTOCOL_GREEYAN, []() { return new GreeYANHeatpumpIR(); }}, // NOLINT
29  {PROTOCOL_GREEYAC, []() { return new GreeYACHeatpumpIR(); }}, // NOLINT
30  {PROTOCOL_GREEYT, []() { return new GreeYTHeatpumpIR(); }}, // NOLINT
31  {PROTOCOL_HISENSE_AUD, []() { return new HisenseHeatpumpIR(); }}, // NOLINT
32  {PROTOCOL_HITACHI, []() { return new HitachiHeatpumpIR(); }}, // NOLINT
33  {PROTOCOL_HYUNDAI, []() { return new HyundaiHeatpumpIR(); }}, // NOLINT
34  {PROTOCOL_IVT, []() { return new IVTHeatpumpIR(); }}, // NOLINT
35  {PROTOCOL_MIDEA, []() { return new MideaHeatpumpIR(); }}, // NOLINT
36  {PROTOCOL_MITSUBISHI_FA, []() { return new MitsubishiFAHeatpumpIR(); }}, // NOLINT
37  {PROTOCOL_MITSUBISHI_FD, []() { return new MitsubishiFDHeatpumpIR(); }}, // NOLINT
38  {PROTOCOL_MITSUBISHI_FE, []() { return new MitsubishiFEHeatpumpIR(); }}, // NOLINT
39  {PROTOCOL_MITSUBISHI_HEAVY_FDTC, []() { return new MitsubishiHeavyFDTCHeatpumpIR(); }}, // NOLINT
40  {PROTOCOL_MITSUBISHI_HEAVY_ZJ, []() { return new MitsubishiHeavyZJHeatpumpIR(); }}, // NOLINT
41  {PROTOCOL_MITSUBISHI_HEAVY_ZM, []() { return new MitsubishiHeavyZMHeatpumpIR(); }}, // NOLINT
42  {PROTOCOL_MITSUBISHI_HEAVY_ZMP, []() { return new MitsubishiHeavyZMPHeatpumpIR(); }}, // NOLINT
43  {PROTOCOL_MITSUBISHI_KJ, []() { return new MitsubishiKJHeatpumpIR(); }}, // NOLINT
44  {PROTOCOL_MITSUBISHI_MSC, []() { return new MitsubishiMSCHeatpumpIR(); }}, // NOLINT
45  {PROTOCOL_MITSUBISHI_MSY, []() { return new MitsubishiMSYHeatpumpIR(); }}, // NOLINT
46  {PROTOCOL_MITSUBISHI_SEZ, []() { return new MitsubishiSEZKDXXHeatpumpIR(); }}, // NOLINT
47  {PROTOCOL_PANASONIC_CKP, []() { return new PanasonicCKPHeatpumpIR(); }}, // NOLINT
48  {PROTOCOL_PANASONIC_DKE, []() { return new PanasonicDKEHeatpumpIR(); }}, // NOLINT
49  {PROTOCOL_PANASONIC_JKE, []() { return new PanasonicJKEHeatpumpIR(); }}, // NOLINT
50  {PROTOCOL_PANASONIC_LKE, []() { return new PanasonicLKEHeatpumpIR(); }}, // NOLINT
51  {PROTOCOL_PANASONIC_NKE, []() { return new PanasonicNKEHeatpumpIR(); }}, // NOLINT
52  {PROTOCOL_SAMSUNG_AQV, []() { return new SamsungAQVHeatpumpIR(); }}, // NOLINT
53  {PROTOCOL_SAMSUNG_FJM, []() { return new SamsungFJMHeatpumpIR(); }}, // NOLINT
54  {PROTOCOL_SHARP, []() { return new SharpHeatpumpIR(); }}, // NOLINT
55  {PROTOCOL_TOSHIBA_DAISEIKAI, []() { return new ToshibaDaiseikaiHeatpumpIR(); }}, // NOLINT
56  {PROTOCOL_TOSHIBA, []() { return new ToshibaHeatpumpIR(); }}, // NOLINT
57  {PROTOCOL_ZHLT01, []() { return new ZHLT01HeatpumpIR(); }}, // NOLINT
58 };
59 
61  auto protocol_constructor = PROTOCOL_CONSTRUCTOR_MAP.find(protocol_);
62  if (protocol_constructor == PROTOCOL_CONSTRUCTOR_MAP.end()) {
63  ESP_LOGE(TAG, "Invalid protocol");
64  return;
65  }
66  this->heatpump_ir_ = protocol_constructor->second();
68  if (this->sensor_) {
69  this->sensor_->add_on_state_callback([this](float state) {
70  this->current_temperature = state;
71 
72  IRSenderESPHome esp_sender(this->transmitter_);
73  this->heatpump_ir_->send(esp_sender, uint8_t(lround(this->current_temperature + 0.5)));
74 
75  // current temperature changed, publish state
76  this->publish_state();
77  });
78  this->current_temperature = this->sensor_->state;
79  } else
80  this->current_temperature = NAN;
81 }
82 
84  uint8_t power_mode_cmd;
85  uint8_t operating_mode_cmd;
86  uint8_t temperature_cmd;
87  uint8_t fan_speed_cmd;
88 
89  uint8_t swing_v_cmd;
92  swing_v_cmd = VDIR_AUTO;
93  break;
95  swing_v_cmd = VDIR_UP;
96  break;
98  swing_v_cmd = VDIR_MUP;
99  break;
101  swing_v_cmd = VDIR_MIDDLE;
102  break;
104  swing_v_cmd = VDIR_MDOWN;
105  break;
107  swing_v_cmd = VDIR_DOWN;
108  break;
109  default:
110  ESP_LOGE(TAG, "Invalid default vertical direction");
111  return;
112  }
114  swing_v_cmd = VDIR_SWING;
115  }
116 
117  uint8_t swing_h_cmd;
120  swing_h_cmd = HDIR_AUTO;
121  break;
123  swing_h_cmd = HDIR_MIDDLE;
124  break;
126  swing_h_cmd = HDIR_LEFT;
127  break;
129  swing_h_cmd = HDIR_MLEFT;
130  break;
132  swing_h_cmd = HDIR_MRIGHT;
133  break;
135  swing_h_cmd = HDIR_RIGHT;
136  break;
137  default:
138  ESP_LOGE(TAG, "Invalid default horizontal direction");
139  return;
140  }
142  swing_h_cmd = HDIR_SWING;
143  }
144 
147  fan_speed_cmd = FAN_2;
148  break;
150  fan_speed_cmd = FAN_3;
151  break;
153  fan_speed_cmd = FAN_4;
154  break;
156  default:
157  fan_speed_cmd = FAN_AUTO;
158  break;
159  }
160 
161  switch (this->mode) {
163  power_mode_cmd = POWER_ON;
164  operating_mode_cmd = MODE_COOL;
165  break;
167  power_mode_cmd = POWER_ON;
168  operating_mode_cmd = MODE_HEAT;
169  break;
171  power_mode_cmd = POWER_ON;
172  operating_mode_cmd = MODE_AUTO;
173  break;
175  power_mode_cmd = POWER_ON;
176  operating_mode_cmd = MODE_FAN;
177  break;
179  power_mode_cmd = POWER_ON;
180  operating_mode_cmd = MODE_DRY;
181  break;
183  default:
184  power_mode_cmd = POWER_OFF;
185  operating_mode_cmd = MODE_AUTO;
186  break;
187  }
188 
189  temperature_cmd = (uint8_t) clamp(this->target_temperature, this->min_temperature_, this->max_temperature_);
190 
191  IRSenderESPHome esp_sender(this->transmitter_);
192  heatpump_ir_->send(esp_sender, power_mode_cmd, operating_mode_cmd, fan_speed_cmd, temperature_cmd, swing_v_cmd,
193  swing_h_cmd);
194 }
195 
196 } // namespace heatpumpir
197 } // namespace esphome
198 
199 #endif
The fan mode is set to Low.
Definition: climate_mode.h:54
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition: climate.h:202
void add_on_state_callback(std::function< void(float)> &&callback)
Add a callback that will be called every time a filtered value arrives.
Definition: sensor.cpp:52
The fan mode is set to Both.
Definition: climate_mode.h:74
float target_temperature
The target temperature of the climate device.
Definition: climate.h:186
The climate device is set to heat to reach the target temperature.
Definition: climate_mode.h:18
const uint32_t POWER_OFF
Definition: whynter.cpp:18
ClimateMode mode
The active mode of the climate device.
Definition: climate.h:173
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition: climate.h:179
void transmit_state() override
Transmit via IR the state of this climate controller.
Definition: heatpumpir.cpp:83
The climate device is set to dry/humidity mode.
Definition: climate_mode.h:22
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition: helpers.h:92
BedJet is in Dry mode (high speed, no heat)
Definition: bedjet_const.h:30
HorizontalDirection default_horizontal_direction_
Definition: heatpumpir.h:110
BedJet is in Cool mode (actually "Fan only" mode)
Definition: bedjet_const.h:28
The fan mode is set to Horizontal.
Definition: climate_mode.h:78
The climate device is set to cool to reach the target temperature.
Definition: climate_mode.h:16
float state
This member variable stores the last state that has passed through all filters.
Definition: sensor.h:131
The fan mode is set to Auto.
Definition: climate_mode.h:52
BedJet is in Heat mode (limited to 4 hours)
Definition: bedjet_const.h:22
RemoteTransmitterBase * transmitter_
Definition: remote_base.h:276
The climate device is adjusting the temperatre dynamically.
Definition: climate_mode.h:27
const uint32_t MODE_FAN
Definition: whynter.cpp:23
The fan mode is set to Vertical.
Definition: climate_mode.h:76
VerticalDirection default_vertical_direction_
Definition: heatpumpir.h:111
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition: climate.cpp:395
The fan mode is set to High.
Definition: climate_mode.h:58
The climate device is off.
Definition: climate_mode.h:12
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition: climate.h:199
const std::map< Protocol, std::function< HeatpumpIR *()> > PROTOCOL_CONSTRUCTOR_MAP
Definition: heatpumpir.cpp:15
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
The fan mode is set to Medium.
Definition: climate_mode.h:56
The climate device only has the fan enabled, no heating or cooling is taking place.
Definition: climate_mode.h:20
value_type value_or(U const &v) const
Definition: optional.h:93
sensor::Sensor * sensor_
Definition: climate_ir.h:67
bool state
Definition: fan.h:34