ESPHome 2026.5.1
Loading...
Searching...
No Matches
heatpumpir.cpp
Go to the documentation of this file.
1#include "heatpumpir.h"
2
3#if defined(USE_ARDUINO) || defined(USE_ESP32)
4
5#include <map>
6#include <IRSender.h>
7#include <HeatpumpIRFactory.h>
9#include "esphome/core/log.h"
10
12
13// IRSenderESPHome - bridge between ESPHome's remote_transmitter and HeatpumpIR library
14// Defined here (not in a header) to isolate HeatpumpIR's headers from the rest of ESPHome,
15// as they define conflicting symbols like millis() in the global namespace.
16class IRSenderESPHome : public IRSender {
17 public:
18 IRSenderESPHome(remote_base::RemoteTransmitterBase *transmitter) : IRSender(0), transmit_(transmitter->transmit()) {}
19
20 void setFrequency(int frequency) override { // NOLINT(readability-identifier-naming)
21 auto *data = this->transmit_.get_data();
22 data->set_carrier_frequency(1000 * frequency);
23 }
24
25 void space(int space_length) override {
26 if (space_length) {
27 auto *data = this->transmit_.get_data();
28 data->space(space_length);
29 } else {
30 this->transmit_.perform();
31 }
32 }
33
34 void mark(int mark_length) override {
35 auto *data = this->transmit_.get_data();
36 data->mark(mark_length);
37 }
38
39 protected:
41};
42
43static const char *const TAG = "heatpumpir.climate";
44
45const std::map<Protocol, std::function<HeatpumpIR *()>> PROTOCOL_CONSTRUCTOR_MAP = {
46 {PROTOCOL_AUX, []() { return new AUXHeatpumpIR(); }}, // NOLINT
47 {PROTOCOL_BALLU, []() { return new BalluHeatpumpIR(); }}, // NOLINT
48 {PROTOCOL_CARRIER_MCA, []() { return new CarrierMCAHeatpumpIR(); }}, // NOLINT
49 {PROTOCOL_CARRIER_NQV, []() { return new CarrierNQVHeatpumpIR(); }}, // NOLINT
50 {PROTOCOL_DAIKIN_ARC417, []() { return new DaikinHeatpumpARC417IR(); }}, // NOLINT
51 {PROTOCOL_DAIKIN_ARC480, []() { return new DaikinHeatpumpARC480A14IR(); }}, // NOLINT
52 {PROTOCOL_DAIKIN, []() { return new DaikinHeatpumpIR(); }}, // NOLINT
53 {PROTOCOL_ELECTROLUXYAL, []() { return new ElectroluxYALHeatpumpIR(); }}, // NOLINT
54 {PROTOCOL_FUEGO, []() { return new FuegoHeatpumpIR(); }}, // NOLINT
55 {PROTOCOL_FUJITSU_AWYZ, []() { return new FujitsuHeatpumpIR(); }}, // NOLINT
56 {PROTOCOL_GREE, []() { return new GreeGenericHeatpumpIR(); }}, // NOLINT
57 {PROTOCOL_GREEYAA, []() { return new GreeYAAHeatpumpIR(); }}, // NOLINT
58 {PROTOCOL_GREEYAN, []() { return new GreeYANHeatpumpIR(); }}, // NOLINT
59 {PROTOCOL_GREEYAC, []() { return new GreeYACHeatpumpIR(); }}, // NOLINT
60 {PROTOCOL_GREEYT, []() { return new GreeYTHeatpumpIR(); }}, // NOLINT
61 {PROTOCOL_GREEYAP, []() { return new GreeYAPHeatpumpIR(); }}, // NOLINT
62 {PROTOCOL_HISENSE_AUD, []() { return new HisenseHeatpumpIR(); }}, // NOLINT
63 {PROTOCOL_HITACHI, []() { return new HitachiHeatpumpIR(); }}, // NOLINT
64 {PROTOCOL_HYUNDAI, []() { return new HyundaiHeatpumpIR(); }}, // NOLINT
65 {PROTOCOL_IVT, []() { return new IVTHeatpumpIR(); }}, // NOLINT
66 {PROTOCOL_MIDEA, []() { return new MideaHeatpumpIR(); }}, // NOLINT
67 {PROTOCOL_MITSUBISHI_FA, []() { return new MitsubishiFAHeatpumpIR(); }}, // NOLINT
68 {PROTOCOL_MITSUBISHI_FD, []() { return new MitsubishiFDHeatpumpIR(); }}, // NOLINT
69 {PROTOCOL_MITSUBISHI_FE, []() { return new MitsubishiFEHeatpumpIR(); }}, // NOLINT
70 {PROTOCOL_MITSUBISHI_HEAVY_FDTC, []() { return new MitsubishiHeavyFDTCHeatpumpIR(); }}, // NOLINT
71 {PROTOCOL_MITSUBISHI_HEAVY_ZJ, []() { return new MitsubishiHeavyZJHeatpumpIR(); }}, // NOLINT
72 {PROTOCOL_MITSUBISHI_HEAVY_ZM, []() { return new MitsubishiHeavyZMHeatpumpIR(); }}, // NOLINT
73 {PROTOCOL_MITSUBISHI_HEAVY_ZMP, []() { return new MitsubishiHeavyZMPHeatpumpIR(); }}, // NOLINT
74 {PROTOCOL_MITSUBISHI_KJ, []() { return new MitsubishiKJHeatpumpIR(); }}, // NOLINT
75 {PROTOCOL_MITSUBISHI_MSC, []() { return new MitsubishiMSCHeatpumpIR(); }}, // NOLINT
76 {PROTOCOL_MITSUBISHI_MSY, []() { return new MitsubishiMSYHeatpumpIR(); }}, // NOLINT
77 {PROTOCOL_MITSUBISHI_SEZ, []() { return new MitsubishiSEZKDXXHeatpumpIR(); }}, // NOLINT
78 {PROTOCOL_PANASONIC_CKP, []() { return new PanasonicCKPHeatpumpIR(); }}, // NOLINT
79 {PROTOCOL_PANASONIC_DKE, []() { return new PanasonicDKEHeatpumpIR(); }}, // NOLINT
80 {PROTOCOL_PANASONIC_EKE, []() { return new PanasonicEKEHeatpumpIR(); }}, // NOLINT
81 {PROTOCOL_PANASONIC_JKE, []() { return new PanasonicJKEHeatpumpIR(); }}, // NOLINT
82 {PROTOCOL_PANASONIC_LKE, []() { return new PanasonicLKEHeatpumpIR(); }}, // NOLINT
83 {PROTOCOL_PANASONIC_NKE, []() { return new PanasonicNKEHeatpumpIR(); }}, // NOLINT
84 {PROTOCOL_SAMSUNG_AQV, []() { return new SamsungAQVHeatpumpIR(); }}, // NOLINT
85 {PROTOCOL_SAMSUNG_FJM, []() { return new SamsungFJMHeatpumpIR(); }}, // NOLINT
86 {PROTOCOL_SHARP, []() { return new SharpHeatpumpIR(); }}, // NOLINT
87 {PROTOCOL_TOSHIBA_DAISEIKAI, []() { return new ToshibaDaiseikaiHeatpumpIR(); }}, // NOLINT
88 {PROTOCOL_TOSHIBA, []() { return new ToshibaHeatpumpIR(); }}, // NOLINT
89 {PROTOCOL_ZHLT01, []() { return new ZHLT01HeatpumpIR(); }}, // NOLINT
90 {PROTOCOL_NIBE, []() { return new NibeHeatpumpIR(); }}, // NOLINT
91 {PROTOCOL_QLIMA_1, []() { return new Qlima1HeatpumpIR(); }}, // NOLINT
92 {PROTOCOL_QLIMA_2, []() { return new Qlima2HeatpumpIR(); }}, // NOLINT
93 {PROTOCOL_SAMSUNG_AQV12MSAN, []() { return new SamsungAQV12MSANHeatpumpIR(); }}, // NOLINT
94 {PROTOCOL_ZHJG01, []() { return new ZHJG01HeatpumpIR(); }}, // NOLINT
95 {PROTOCOL_AIRWAY, []() { return new AIRWAYHeatpumpIR(); }}, // NOLINT
96 {PROTOCOL_BGH_AUD, []() { return new BGHHeatpumpIR(); }}, // NOLINT
97 {PROTOCOL_PANASONIC_ALTDKE, []() { return new PanasonicAltDKEHeatpumpIR(); }}, // NOLINT
98 {PROTOCOL_PHILCO_PHS32, []() { return new PhilcoPHS32HeatpumpIR(); }}, // NOLINT
99 {PROTOCOL_VAILLANTVAI8, []() { return new VaillantHeatpumpIR(); }}, // NOLINT
100 {PROTOCOL_R51M, []() { return new R51MHeatpumpIR(); }}, // NOLINT
101};
102
104 auto protocol_constructor = PROTOCOL_CONSTRUCTOR_MAP.find(protocol_);
105 if (protocol_constructor == PROTOCOL_CONSTRUCTOR_MAP.end()) {
106 ESP_LOGE(TAG, "Invalid protocol");
107 return;
108 }
109 this->heatpump_ir_ = protocol_constructor->second();
111 if (this->sensor_) {
112 this->sensor_->add_on_state_callback([this](float state) {
114
115 IRSenderESPHome esp_sender(this->transmitter_);
116 this->heatpump_ir_->send(esp_sender, uint8_t(lround(this->current_temperature)));
117
118 // current temperature changed, publish state
119 this->publish_state();
120 });
121 this->current_temperature = this->sensor_->state;
122 } else {
123 this->current_temperature = NAN;
124 }
125}
126
128 uint8_t power_mode_cmd;
129 uint8_t operating_mode_cmd;
130 uint8_t temperature_cmd;
131 uint8_t fan_speed_cmd;
132
133 uint8_t swing_v_cmd;
136 swing_v_cmd = VDIR_AUTO;
137 break;
139 swing_v_cmd = VDIR_UP;
140 break;
142 swing_v_cmd = VDIR_MUP;
143 break;
145 swing_v_cmd = VDIR_MIDDLE;
146 break;
148 swing_v_cmd = VDIR_MDOWN;
149 break;
151 swing_v_cmd = VDIR_DOWN;
152 break;
153 default:
154 ESP_LOGE(TAG, "Invalid default vertical direction");
155 return;
156 }
158 swing_v_cmd = VDIR_SWING;
159 }
160
161 uint8_t swing_h_cmd;
164 swing_h_cmd = HDIR_AUTO;
165 break;
167 swing_h_cmd = HDIR_MIDDLE;
168 break;
170 swing_h_cmd = HDIR_LEFT;
171 break;
173 swing_h_cmd = HDIR_MLEFT;
174 break;
176 swing_h_cmd = HDIR_MRIGHT;
177 break;
179 swing_h_cmd = HDIR_RIGHT;
180 break;
181 default:
182 ESP_LOGE(TAG, "Invalid default horizontal direction");
183 return;
184 }
186 swing_h_cmd = HDIR_SWING;
187 }
188
189 switch (this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO)) {
191 fan_speed_cmd = FAN_2;
192 break;
194 fan_speed_cmd = FAN_3;
195 break;
197 fan_speed_cmd = FAN_4;
198 break;
200 default:
201 fan_speed_cmd = FAN_AUTO;
202 break;
203 }
204
205 switch (this->mode) {
207 power_mode_cmd = POWER_ON;
208 operating_mode_cmd = MODE_COOL;
209 break;
211 power_mode_cmd = POWER_ON;
212 operating_mode_cmd = MODE_HEAT;
213 break;
214 // Map HEAT_COOL to hardware AUTO mode (automatic heat/cool changeover based on temperature).
215 // In hardware AUTO mode, the device automatically switches between heating and cooling
216 // based on the current temperature versus the target temperature.
217 // See https://github.com/esphome/esphome/issues/11161 for further discussion.
220 power_mode_cmd = POWER_ON;
221 operating_mode_cmd = MODE_AUTO;
222 break;
224 power_mode_cmd = POWER_ON;
225 operating_mode_cmd = MODE_FAN;
226 break;
228 power_mode_cmd = POWER_ON;
229 operating_mode_cmd = MODE_DRY;
230 break;
232 default:
233 power_mode_cmd = POWER_OFF;
234 operating_mode_cmd = MODE_AUTO;
235 break;
236 }
237
238 temperature_cmd = (uint8_t) clamp(this->target_temperature, this->min_temperature_, this->max_temperature_);
239
240 IRSenderESPHome esp_sender(this->transmitter_);
241 heatpump_ir_->send(esp_sender, power_mode_cmd, operating_mode_cmd, fan_speed_cmd, temperature_cmd, swing_v_cmd,
242 swing_h_cmd);
243}
244
245} // namespace esphome::heatpumpir
246
247#endif
uint16_le_t frequency
Definition bl0942.h:6
ClimateMode mode
The active mode of the climate device.
Definition climate.h:293
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:287
float target_temperature
The target temperature of the climate device.
Definition climate.h:274
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition climate.h:299
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:267
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:437
VerticalDirection default_vertical_direction_
Definition heatpumpir.h:122
void transmit_state() override
Transmit via IR the state of this climate controller.
HorizontalDirection default_horizontal_direction_
Definition heatpumpir.h:121
void add_on_state_callback(F &&callback)
Add a callback that will be called every time a filtered value arrives.
Definition sensor.h:119
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:138
bool state
Definition fan.h:2
@ CLIMATE_SWING_HORIZONTAL
The fan mode is set to Horizontal.
@ CLIMATE_SWING_VERTICAL
The fan mode is set to Vertical.
@ CLIMATE_SWING_BOTH
The fan mode is set to Both.
@ CLIMATE_MODE_DRY
The climate device is set to dry/humidity mode.
@ CLIMATE_MODE_FAN_ONLY
The climate device only has the fan enabled, no heating or cooling is taking place.
@ CLIMATE_MODE_HEAT
The climate device is set to heat to reach the target temperature.
@ CLIMATE_MODE_COOL
The climate device is set to cool to reach the target temperature.
@ CLIMATE_MODE_HEAT_COOL
The climate device is set to heat/cool to reach the target temperature.
@ CLIMATE_MODE_OFF
The climate device is off.
@ CLIMATE_MODE_AUTO
The climate device is adjusting the temperature dynamically.
@ 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.
const std::map< Protocol, std::function< HeatpumpIR *()> > PROTOCOL_CONSTRUCTOR_MAP