ESPHome  2023.8.3
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_HISENSE_AUD, []() { return new HisenseHeatpumpIR(); }}, // NOLINT
31  {PROTOCOL_HITACHI, []() { return new HitachiHeatpumpIR(); }}, // NOLINT
32  {PROTOCOL_HYUNDAI, []() { return new HyundaiHeatpumpIR(); }}, // NOLINT
33  {PROTOCOL_IVT, []() { return new IVTHeatpumpIR(); }}, // NOLINT
34  {PROTOCOL_MIDEA, []() { return new MideaHeatpumpIR(); }}, // NOLINT
35  {PROTOCOL_MITSUBISHI_FA, []() { return new MitsubishiFAHeatpumpIR(); }}, // NOLINT
36  {PROTOCOL_MITSUBISHI_FD, []() { return new MitsubishiFDHeatpumpIR(); }}, // NOLINT
37  {PROTOCOL_MITSUBISHI_FE, []() { return new MitsubishiFEHeatpumpIR(); }}, // NOLINT
38  {PROTOCOL_MITSUBISHI_HEAVY_FDTC, []() { return new MitsubishiHeavyFDTCHeatpumpIR(); }}, // NOLINT
39  {PROTOCOL_MITSUBISHI_HEAVY_ZJ, []() { return new MitsubishiHeavyZJHeatpumpIR(); }}, // NOLINT
40  {PROTOCOL_MITSUBISHI_HEAVY_ZM, []() { return new MitsubishiHeavyZMHeatpumpIR(); }}, // NOLINT
41  {PROTOCOL_MITSUBISHI_HEAVY_ZMP, []() { return new MitsubishiHeavyZMPHeatpumpIR(); }}, // NOLINT
42  {PROTOCOL_MITSUBISHI_KJ, []() { return new MitsubishiKJHeatpumpIR(); }}, // NOLINT
43  {PROTOCOL_MITSUBISHI_MSC, []() { return new MitsubishiMSCHeatpumpIR(); }}, // NOLINT
44  {PROTOCOL_MITSUBISHI_MSY, []() { return new MitsubishiMSYHeatpumpIR(); }}, // NOLINT
45  {PROTOCOL_MITSUBISHI_SEZ, []() { return new MitsubishiSEZKDXXHeatpumpIR(); }}, // NOLINT
46  {PROTOCOL_PANASONIC_CKP, []() { return new PanasonicCKPHeatpumpIR(); }}, // NOLINT
47  {PROTOCOL_PANASONIC_DKE, []() { return new PanasonicDKEHeatpumpIR(); }}, // NOLINT
48  {PROTOCOL_PANASONIC_JKE, []() { return new PanasonicJKEHeatpumpIR(); }}, // NOLINT
49  {PROTOCOL_PANASONIC_LKE, []() { return new PanasonicLKEHeatpumpIR(); }}, // NOLINT
50  {PROTOCOL_PANASONIC_NKE, []() { return new PanasonicNKEHeatpumpIR(); }}, // NOLINT
51  {PROTOCOL_SAMSUNG_AQV, []() { return new SamsungAQVHeatpumpIR(); }}, // NOLINT
52  {PROTOCOL_SAMSUNG_FJM, []() { return new SamsungFJMHeatpumpIR(); }}, // NOLINT
53  {PROTOCOL_SHARP, []() { return new SharpHeatpumpIR(); }}, // NOLINT
54  {PROTOCOL_TOSHIBA_DAISEIKAI, []() { return new ToshibaDaiseikaiHeatpumpIR(); }}, // NOLINT
55  {PROTOCOL_TOSHIBA, []() { return new ToshibaHeatpumpIR(); }}, // NOLINT
56  {PROTOCOL_ZHLT01, []() { return new ZHLT01HeatpumpIR(); }}, // NOLINT
57 };
58 
60  auto protocol_constructor = PROTOCOL_CONSTRUCTOR_MAP.find(protocol_);
61  if (protocol_constructor == PROTOCOL_CONSTRUCTOR_MAP.end()) {
62  ESP_LOGE(TAG, "Invalid protocol");
63  return;
64  }
65  this->heatpump_ir_ = protocol_constructor->second();
67  if (this->sensor_) {
68  this->sensor_->add_on_state_callback([this](float state) {
69  this->current_temperature = state;
70 
71  IRSenderESPHome esp_sender(this->transmitter_);
72  this->heatpump_ir_->send(esp_sender, uint8_t(lround(this->current_temperature + 0.5)));
73 
74  // current temperature changed, publish state
75  this->publish_state();
76  });
77  this->current_temperature = this->sensor_->state;
78  } else
79  this->current_temperature = NAN;
80 }
81 
83  uint8_t power_mode_cmd;
84  uint8_t operating_mode_cmd;
85  uint8_t temperature_cmd;
86  uint8_t fan_speed_cmd;
87 
88  uint8_t swing_v_cmd;
91  swing_v_cmd = VDIR_AUTO;
92  break;
94  swing_v_cmd = VDIR_UP;
95  break;
97  swing_v_cmd = VDIR_MUP;
98  break;
100  swing_v_cmd = VDIR_MIDDLE;
101  break;
103  swing_v_cmd = VDIR_MDOWN;
104  break;
106  swing_v_cmd = VDIR_DOWN;
107  break;
108  default:
109  ESP_LOGE(TAG, "Invalid default vertical direction");
110  return;
111  }
113  swing_v_cmd = VDIR_SWING;
114  }
115 
116  uint8_t swing_h_cmd;
119  swing_h_cmd = HDIR_AUTO;
120  break;
122  swing_h_cmd = HDIR_MIDDLE;
123  break;
125  swing_h_cmd = HDIR_LEFT;
126  break;
128  swing_h_cmd = HDIR_MLEFT;
129  break;
131  swing_h_cmd = HDIR_MRIGHT;
132  break;
134  swing_h_cmd = HDIR_RIGHT;
135  break;
136  default:
137  ESP_LOGE(TAG, "Invalid default horizontal direction");
138  return;
139  }
141  swing_h_cmd = HDIR_SWING;
142  }
143 
146  fan_speed_cmd = FAN_2;
147  break;
149  fan_speed_cmd = FAN_3;
150  break;
152  fan_speed_cmd = FAN_4;
153  break;
155  default:
156  fan_speed_cmd = FAN_AUTO;
157  break;
158  }
159 
160  switch (this->mode) {
162  power_mode_cmd = POWER_ON;
163  operating_mode_cmd = MODE_COOL;
164  break;
166  power_mode_cmd = POWER_ON;
167  operating_mode_cmd = MODE_HEAT;
168  break;
170  power_mode_cmd = POWER_ON;
171  operating_mode_cmd = MODE_AUTO;
172  break;
174  power_mode_cmd = POWER_ON;
175  operating_mode_cmd = MODE_FAN;
176  break;
178  power_mode_cmd = POWER_ON;
179  operating_mode_cmd = MODE_DRY;
180  break;
182  default:
183  power_mode_cmd = POWER_OFF;
184  operating_mode_cmd = MODE_AUTO;
185  break;
186  }
187 
188  temperature_cmd = (uint8_t) clamp(this->target_temperature, this->min_temperature_, this->max_temperature_);
189 
190  IRSenderESPHome esp_sender(this->transmitter_);
191  heatpump_ir_->send(esp_sender, power_mode_cmd, operating_mode_cmd, fan_speed_cmd, temperature_cmd, swing_v_cmd,
192  swing_h_cmd);
193 }
194 
195 } // namespace heatpumpir
196 } // namespace esphome
197 
198 #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:185
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:172
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:164
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition: climate.h:168
void transmit_state() override
Transmit via IR the state of this climate controller.
Definition: heatpumpir.cpp:82
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:89
BedJet is in Dry mode (high speed, no heat)
Definition: bedjet_const.h:30
HorizontalDirection default_horizontal_direction_
Definition: heatpumpir.h:109
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
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:110
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition: climate.cpp:377
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:182
const std::map< Protocol, std::function< HeatpumpIR *()> > PROTOCOL_CONSTRUCTOR_MAP
Definition: heatpumpir.cpp:15
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
remote_transmitter::RemoteTransmitterComponent * transmitter_
Definition: climate_ir.h:67
value_type value_or(U const &v) const
Definition: optional.h:93
sensor::Sensor * sensor_
Definition: climate_ir.h:68
bool state
Definition: fan.h:34