ESPHome 2025.5.0
Loading...
Searching...
No Matches
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
10namespace esphome {
11namespace heatpumpir {
12
13static const char *const TAG = "heatpumpir.climate";
14
15const 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_EKE, []() { return new PanasonicEKEHeatpumpIR(); }}, // NOLINT
51 {PROTOCOL_PANASONIC_JKE, []() { return new PanasonicJKEHeatpumpIR(); }}, // NOLINT
52 {PROTOCOL_PANASONIC_LKE, []() { return new PanasonicLKEHeatpumpIR(); }}, // NOLINT
53 {PROTOCOL_PANASONIC_NKE, []() { return new PanasonicNKEHeatpumpIR(); }}, // NOLINT
54 {PROTOCOL_SAMSUNG_AQV, []() { return new SamsungAQVHeatpumpIR(); }}, // NOLINT
55 {PROTOCOL_SAMSUNG_FJM, []() { return new SamsungFJMHeatpumpIR(); }}, // NOLINT
56 {PROTOCOL_SHARP, []() { return new SharpHeatpumpIR(); }}, // NOLINT
57 {PROTOCOL_TOSHIBA_DAISEIKAI, []() { return new ToshibaDaiseikaiHeatpumpIR(); }}, // NOLINT
58 {PROTOCOL_TOSHIBA, []() { return new ToshibaHeatpumpIR(); }}, // NOLINT
59 {PROTOCOL_ZHLT01, []() { return new ZHLT01HeatpumpIR(); }}, // NOLINT
60 {PROTOCOL_NIBE, []() { return new NibeHeatpumpIR(); }}, // NOLINT
61 {PROTOCOL_QLIMA_1, []() { return new Qlima1HeatpumpIR(); }}, // NOLINT
62 {PROTOCOL_QLIMA_2, []() { return new Qlima2HeatpumpIR(); }}, // NOLINT
63 {PROTOCOL_SAMSUNG_AQV12MSAN, []() { return new SamsungAQV12MSANHeatpumpIR(); }}, // NOLINT
64 {PROTOCOL_ZHJG01, []() { return new ZHJG01HeatpumpIR(); }}, // NOLINT
65 {PROTOCOL_AIRWAY, []() { return new AIRWAYHeatpumpIR(); }}, // NOLINT
66 {PROTOCOL_BGH_AUD, []() { return new BGHHeatpumpIR(); }}, // NOLINT
67 {PROTOCOL_PANASONIC_ALTDKE, []() { return new PanasonicAltDKEHeatpumpIR(); }}, // NOLINT
68 {PROTOCOL_VAILLANTVAI8, []() { return new VaillantHeatpumpIR(); }}, // NOLINT
69 {PROTOCOL_R51M, []() { return new R51MHeatpumpIR(); }}, // NOLINT
70};
71
73 auto protocol_constructor = PROTOCOL_CONSTRUCTOR_MAP.find(protocol_);
74 if (protocol_constructor == PROTOCOL_CONSTRUCTOR_MAP.end()) {
75 ESP_LOGE(TAG, "Invalid protocol");
76 return;
77 }
78 this->heatpump_ir_ = protocol_constructor->second();
80 if (this->sensor_) {
81 this->sensor_->add_on_state_callback([this](float state) {
83
84 IRSenderESPHome esp_sender(this->transmitter_);
85 this->heatpump_ir_->send(esp_sender, uint8_t(lround(this->current_temperature + 0.5)));
86
87 // current temperature changed, publish state
88 this->publish_state();
89 });
90 this->current_temperature = this->sensor_->state;
91 } else {
92 this->current_temperature = NAN;
93 }
94}
95
97 uint8_t power_mode_cmd;
98 uint8_t operating_mode_cmd;
99 uint8_t temperature_cmd;
100 uint8_t fan_speed_cmd;
101
102 uint8_t swing_v_cmd;
105 swing_v_cmd = VDIR_AUTO;
106 break;
108 swing_v_cmd = VDIR_UP;
109 break;
111 swing_v_cmd = VDIR_MUP;
112 break;
114 swing_v_cmd = VDIR_MIDDLE;
115 break;
117 swing_v_cmd = VDIR_MDOWN;
118 break;
120 swing_v_cmd = VDIR_DOWN;
121 break;
122 default:
123 ESP_LOGE(TAG, "Invalid default vertical direction");
124 return;
125 }
127 swing_v_cmd = VDIR_SWING;
128 }
129
130 uint8_t swing_h_cmd;
133 swing_h_cmd = HDIR_AUTO;
134 break;
136 swing_h_cmd = HDIR_MIDDLE;
137 break;
139 swing_h_cmd = HDIR_LEFT;
140 break;
142 swing_h_cmd = HDIR_MLEFT;
143 break;
145 swing_h_cmd = HDIR_MRIGHT;
146 break;
148 swing_h_cmd = HDIR_RIGHT;
149 break;
150 default:
151 ESP_LOGE(TAG, "Invalid default horizontal direction");
152 return;
153 }
155 swing_h_cmd = HDIR_SWING;
156 }
157
160 fan_speed_cmd = FAN_2;
161 break;
163 fan_speed_cmd = FAN_3;
164 break;
166 fan_speed_cmd = FAN_4;
167 break;
169 default:
170 fan_speed_cmd = FAN_AUTO;
171 break;
172 }
173
174 switch (this->mode) {
176 power_mode_cmd = POWER_ON;
177 operating_mode_cmd = MODE_COOL;
178 break;
180 power_mode_cmd = POWER_ON;
181 operating_mode_cmd = MODE_HEAT;
182 break;
184 power_mode_cmd = POWER_ON;
185 operating_mode_cmd = MODE_AUTO;
186 break;
188 power_mode_cmd = POWER_ON;
189 operating_mode_cmd = MODE_FAN;
190 break;
192 power_mode_cmd = POWER_ON;
193 operating_mode_cmd = MODE_DRY;
194 break;
196 default:
197 power_mode_cmd = POWER_OFF;
198 operating_mode_cmd = MODE_AUTO;
199 break;
200 }
201
202 temperature_cmd = (uint8_t) clamp(this->target_temperature, this->min_temperature_, this->max_temperature_);
203
204 IRSenderESPHome esp_sender(this->transmitter_);
205 heatpump_ir_->send(esp_sender, power_mode_cmd, operating_mode_cmd, fan_speed_cmd, temperature_cmd, swing_v_cmd,
206 swing_h_cmd);
207}
208
209} // namespace heatpumpir
210} // namespace esphome
211
212#endif
ClimateMode mode
The active mode of the climate device.
Definition climate.h:173
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:199
float target_temperature
The target temperature of the climate device.
Definition climate.h:186
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition climate.h:202
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:179
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:395
VerticalDirection default_vertical_direction_
Definition heatpumpir.h:123
void transmit_state() override
Transmit via IR the state of this climate controller.
HorizontalDirection default_horizontal_direction_
Definition heatpumpir.h:122
value_type value_or(U const &v) const
Definition optional.h:93
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
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:131
bool state
Definition fan.h:0
@ 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_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
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition helpers.h:101