ESPHome  2025.2.0
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 }
94 
96  uint8_t power_mode_cmd;
97  uint8_t operating_mode_cmd;
98  uint8_t temperature_cmd;
99  uint8_t fan_speed_cmd;
100 
101  uint8_t swing_v_cmd;
102  switch (default_vertical_direction_) {
104  swing_v_cmd = VDIR_AUTO;
105  break;
107  swing_v_cmd = VDIR_UP;
108  break;
110  swing_v_cmd = VDIR_MUP;
111  break;
113  swing_v_cmd = VDIR_MIDDLE;
114  break;
116  swing_v_cmd = VDIR_MDOWN;
117  break;
119  swing_v_cmd = VDIR_DOWN;
120  break;
121  default:
122  ESP_LOGE(TAG, "Invalid default vertical direction");
123  return;
124  }
126  swing_v_cmd = VDIR_SWING;
127  }
128 
129  uint8_t swing_h_cmd;
132  swing_h_cmd = HDIR_AUTO;
133  break;
135  swing_h_cmd = HDIR_MIDDLE;
136  break;
138  swing_h_cmd = HDIR_LEFT;
139  break;
141  swing_h_cmd = HDIR_MLEFT;
142  break;
144  swing_h_cmd = HDIR_MRIGHT;
145  break;
147  swing_h_cmd = HDIR_RIGHT;
148  break;
149  default:
150  ESP_LOGE(TAG, "Invalid default horizontal direction");
151  return;
152  }
154  swing_h_cmd = HDIR_SWING;
155  }
156 
159  fan_speed_cmd = FAN_2;
160  break;
162  fan_speed_cmd = FAN_3;
163  break;
165  fan_speed_cmd = FAN_4;
166  break;
168  default:
169  fan_speed_cmd = FAN_AUTO;
170  break;
171  }
172 
173  switch (this->mode) {
175  power_mode_cmd = POWER_ON;
176  operating_mode_cmd = MODE_COOL;
177  break;
179  power_mode_cmd = POWER_ON;
180  operating_mode_cmd = MODE_HEAT;
181  break;
183  power_mode_cmd = POWER_ON;
184  operating_mode_cmd = MODE_AUTO;
185  break;
187  power_mode_cmd = POWER_ON;
188  operating_mode_cmd = MODE_FAN;
189  break;
191  power_mode_cmd = POWER_ON;
192  operating_mode_cmd = MODE_DRY;
193  break;
195  default:
196  power_mode_cmd = POWER_OFF;
197  operating_mode_cmd = MODE_AUTO;
198  break;
199  }
200 
201  temperature_cmd = (uint8_t) clamp(this->target_temperature, this->min_temperature_, this->max_temperature_);
202 
203  IRSenderESPHome esp_sender(this->transmitter_);
204  heatpump_ir_->send(esp_sender, power_mode_cmd, operating_mode_cmd, fan_speed_cmd, temperature_cmd, swing_v_cmd,
205  swing_h_cmd);
206 }
207 
208 } // namespace heatpumpir
209 } // namespace esphome
210 
211 #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:95
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:101
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:294
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