ESPHome  2024.11.1
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_GREEYAP, []() { return new GreeYAPHeatpumpIR(); }}, // NOLINT
32  {PROTOCOL_HISENSE_AUD, []() { return new HisenseHeatpumpIR(); }}, // NOLINT
33  {PROTOCOL_HITACHI, []() { return new HitachiHeatpumpIR(); }}, // NOLINT
34  {PROTOCOL_HYUNDAI, []() { return new HyundaiHeatpumpIR(); }}, // NOLINT
35  {PROTOCOL_IVT, []() { return new IVTHeatpumpIR(); }}, // NOLINT
36  {PROTOCOL_MIDEA, []() { return new MideaHeatpumpIR(); }}, // NOLINT
37  {PROTOCOL_MITSUBISHI_FA, []() { return new MitsubishiFAHeatpumpIR(); }}, // NOLINT
38  {PROTOCOL_MITSUBISHI_FD, []() { return new MitsubishiFDHeatpumpIR(); }}, // NOLINT
39  {PROTOCOL_MITSUBISHI_FE, []() { return new MitsubishiFEHeatpumpIR(); }}, // NOLINT
40  {PROTOCOL_MITSUBISHI_HEAVY_FDTC, []() { return new MitsubishiHeavyFDTCHeatpumpIR(); }}, // NOLINT
41  {PROTOCOL_MITSUBISHI_HEAVY_ZJ, []() { return new MitsubishiHeavyZJHeatpumpIR(); }}, // NOLINT
42  {PROTOCOL_MITSUBISHI_HEAVY_ZM, []() { return new MitsubishiHeavyZMHeatpumpIR(); }}, // NOLINT
43  {PROTOCOL_MITSUBISHI_HEAVY_ZMP, []() { return new MitsubishiHeavyZMPHeatpumpIR(); }}, // NOLINT
44  {PROTOCOL_MITSUBISHI_KJ, []() { return new MitsubishiKJHeatpumpIR(); }}, // NOLINT
45  {PROTOCOL_MITSUBISHI_MSC, []() { return new MitsubishiMSCHeatpumpIR(); }}, // NOLINT
46  {PROTOCOL_MITSUBISHI_MSY, []() { return new MitsubishiMSYHeatpumpIR(); }}, // NOLINT
47  {PROTOCOL_MITSUBISHI_SEZ, []() { return new MitsubishiSEZKDXXHeatpumpIR(); }}, // NOLINT
48  {PROTOCOL_PANASONIC_CKP, []() { return new PanasonicCKPHeatpumpIR(); }}, // NOLINT
49  {PROTOCOL_PANASONIC_DKE, []() { return new PanasonicDKEHeatpumpIR(); }}, // NOLINT
50  {PROTOCOL_PANASONIC_JKE, []() { return new PanasonicJKEHeatpumpIR(); }}, // NOLINT
51  {PROTOCOL_PANASONIC_LKE, []() { return new PanasonicLKEHeatpumpIR(); }}, // NOLINT
52  {PROTOCOL_PANASONIC_NKE, []() { return new PanasonicNKEHeatpumpIR(); }}, // NOLINT
53  {PROTOCOL_SAMSUNG_AQV, []() { return new SamsungAQVHeatpumpIR(); }}, // NOLINT
54  {PROTOCOL_SAMSUNG_FJM, []() { return new SamsungFJMHeatpumpIR(); }}, // NOLINT
55  {PROTOCOL_SHARP, []() { return new SharpHeatpumpIR(); }}, // NOLINT
56  {PROTOCOL_TOSHIBA_DAISEIKAI, []() { return new ToshibaDaiseikaiHeatpumpIR(); }}, // NOLINT
57  {PROTOCOL_TOSHIBA, []() { return new ToshibaHeatpumpIR(); }}, // NOLINT
58  {PROTOCOL_ZHLT01, []() { return new ZHLT01HeatpumpIR(); }}, // NOLINT
59  {PROTOCOL_NIBE, []() { return new NibeHeatpumpIR(); }}, // NOLINT
60  {PROTOCOL_QLIMA_1, []() { return new Qlima1HeatpumpIR(); }}, // NOLINT
61  {PROTOCOL_QLIMA_2, []() { return new Qlima2HeatpumpIR(); }}, // NOLINT
62  {PROTOCOL_SAMSUNG_AQV12MSAN, []() { return new SamsungAQV12MSANHeatpumpIR(); }}, // NOLINT
63  {PROTOCOL_ZHJG01, []() { return new ZHJG01HeatpumpIR(); }}, // NOLINT
64  {PROTOCOL_AIRWAY, []() { return new AIRWAYHeatpumpIR(); }}, // NOLINT
65  {PROTOCOL_BGH_AUD, []() { return new BGHHeatpumpIR(); }}, // NOLINT
66  {PROTOCOL_PANASONIC_ALTDKE, []() { return new PanasonicAltDKEHeatpumpIR(); }}, // NOLINT
67  {PROTOCOL_VAILLANTVAI8, []() { return new VaillantHeatpumpIR(); }}, // NOLINT
68  {PROTOCOL_R51M, []() { return new R51MHeatpumpIR(); }}, // NOLINT
69 };
70 
72  auto protocol_constructor = PROTOCOL_CONSTRUCTOR_MAP.find(protocol_);
73  if (protocol_constructor == PROTOCOL_CONSTRUCTOR_MAP.end()) {
74  ESP_LOGE(TAG, "Invalid protocol");
75  return;
76  }
77  this->heatpump_ir_ = protocol_constructor->second();
79  if (this->sensor_) {
80  this->sensor_->add_on_state_callback([this](float state) {
81  this->current_temperature = state;
82 
83  IRSenderESPHome esp_sender(this->transmitter_);
84  this->heatpump_ir_->send(esp_sender, uint8_t(lround(this->current_temperature + 0.5)));
85 
86  // current temperature changed, publish state
87  this->publish_state();
88  });
89  this->current_temperature = this->sensor_->state;
90  } else
91  this->current_temperature = NAN;
92 }
93 
95  uint8_t power_mode_cmd;
96  uint8_t operating_mode_cmd;
97  uint8_t temperature_cmd;
98  uint8_t fan_speed_cmd;
99 
100  uint8_t swing_v_cmd;
101  switch (default_vertical_direction_) {
103  swing_v_cmd = VDIR_AUTO;
104  break;
106  swing_v_cmd = VDIR_UP;
107  break;
109  swing_v_cmd = VDIR_MUP;
110  break;
112  swing_v_cmd = VDIR_MIDDLE;
113  break;
115  swing_v_cmd = VDIR_MDOWN;
116  break;
118  swing_v_cmd = VDIR_DOWN;
119  break;
120  default:
121  ESP_LOGE(TAG, "Invalid default vertical direction");
122  return;
123  }
125  swing_v_cmd = VDIR_SWING;
126  }
127 
128  uint8_t swing_h_cmd;
131  swing_h_cmd = HDIR_AUTO;
132  break;
134  swing_h_cmd = HDIR_MIDDLE;
135  break;
137  swing_h_cmd = HDIR_LEFT;
138  break;
140  swing_h_cmd = HDIR_MLEFT;
141  break;
143  swing_h_cmd = HDIR_MRIGHT;
144  break;
146  swing_h_cmd = HDIR_RIGHT;
147  break;
148  default:
149  ESP_LOGE(TAG, "Invalid default horizontal direction");
150  return;
151  }
153  swing_h_cmd = HDIR_SWING;
154  }
155 
158  fan_speed_cmd = FAN_2;
159  break;
161  fan_speed_cmd = FAN_3;
162  break;
164  fan_speed_cmd = FAN_4;
165  break;
167  default:
168  fan_speed_cmd = FAN_AUTO;
169  break;
170  }
171 
172  switch (this->mode) {
174  power_mode_cmd = POWER_ON;
175  operating_mode_cmd = MODE_COOL;
176  break;
178  power_mode_cmd = POWER_ON;
179  operating_mode_cmd = MODE_HEAT;
180  break;
182  power_mode_cmd = POWER_ON;
183  operating_mode_cmd = MODE_AUTO;
184  break;
186  power_mode_cmd = POWER_ON;
187  operating_mode_cmd = MODE_FAN;
188  break;
190  power_mode_cmd = POWER_ON;
191  operating_mode_cmd = MODE_DRY;
192  break;
194  default:
195  power_mode_cmd = POWER_OFF;
196  operating_mode_cmd = MODE_AUTO;
197  break;
198  }
199 
200  temperature_cmd = (uint8_t) clamp(this->target_temperature, this->min_temperature_, this->max_temperature_);
201 
202  IRSenderESPHome esp_sender(this->transmitter_);
203  heatpump_ir_->send(esp_sender, power_mode_cmd, operating_mode_cmd, fan_speed_cmd, temperature_cmd, swing_v_cmd,
204  swing_h_cmd);
205 }
206 
207 } // namespace heatpumpir
208 } // namespace esphome
209 
210 #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:94
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:93
BedJet is in Dry mode (high speed, no heat)
Definition: bedjet_const.h:30
HorizontalDirection default_horizontal_direction_
Definition: heatpumpir.h:121
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:122
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
Implementation of SPI Controller mode.
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