ESPHome  2022.6.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 };
57 
59  auto protocol_constructor = PROTOCOL_CONSTRUCTOR_MAP.find(protocol_);
60  if (protocol_constructor == PROTOCOL_CONSTRUCTOR_MAP.end()) {
61  ESP_LOGE(TAG, "Invalid protocol");
62  return;
63  }
64  this->heatpump_ir_ = protocol_constructor->second();
66  if (this->sensor_) {
67  this->sensor_->add_on_state_callback([this](float state) {
68  this->current_temperature = state;
69 
70  IRSenderESPHome esp_sender(this->transmitter_);
71  this->heatpump_ir_->send(esp_sender, uint8_t(lround(this->current_temperature + 0.5)));
72 
73  // current temperature changed, publish state
74  this->publish_state();
75  });
76  this->current_temperature = this->sensor_->state;
77  } else
78  this->current_temperature = NAN;
79 }
80 
82  uint8_t power_mode_cmd;
83  uint8_t operating_mode_cmd;
84  uint8_t temperature_cmd;
85  uint8_t fan_speed_cmd;
86 
87  uint8_t swing_v_cmd;
90  swing_v_cmd = VDIR_AUTO;
91  break;
93  swing_v_cmd = VDIR_UP;
94  break;
96  swing_v_cmd = VDIR_MUP;
97  break;
99  swing_v_cmd = VDIR_MIDDLE;
100  break;
102  swing_v_cmd = VDIR_MDOWN;
103  break;
105  swing_v_cmd = VDIR_DOWN;
106  break;
107  default:
108  ESP_LOGE(TAG, "Invalid default vertical direction");
109  return;
110  }
112  swing_v_cmd = VDIR_SWING;
113  }
114 
115  uint8_t swing_h_cmd;
118  swing_h_cmd = HDIR_AUTO;
119  break;
121  swing_h_cmd = HDIR_MIDDLE;
122  break;
124  swing_h_cmd = HDIR_LEFT;
125  break;
127  swing_h_cmd = HDIR_MLEFT;
128  break;
130  swing_h_cmd = HDIR_MRIGHT;
131  break;
133  swing_h_cmd = HDIR_RIGHT;
134  break;
135  default:
136  ESP_LOGE(TAG, "Invalid default horizontal direction");
137  return;
138  }
140  swing_h_cmd = HDIR_SWING;
141  }
142 
145  fan_speed_cmd = FAN_2;
146  break;
148  fan_speed_cmd = FAN_3;
149  break;
151  fan_speed_cmd = FAN_4;
152  break;
154  default:
155  fan_speed_cmd = FAN_AUTO;
156  break;
157  }
158 
159  switch (this->mode) {
161  power_mode_cmd = POWER_ON;
162  operating_mode_cmd = MODE_COOL;
163  break;
165  power_mode_cmd = POWER_ON;
166  operating_mode_cmd = MODE_HEAT;
167  break;
169  power_mode_cmd = POWER_ON;
170  operating_mode_cmd = MODE_AUTO;
171  break;
173  power_mode_cmd = POWER_ON;
174  operating_mode_cmd = MODE_FAN;
175  break;
177  power_mode_cmd = POWER_ON;
178  operating_mode_cmd = MODE_DRY;
179  break;
181  default:
182  power_mode_cmd = POWER_OFF;
183  operating_mode_cmd = MODE_AUTO;
184  break;
185  }
186 
187  temperature_cmd = (uint8_t) clamp(this->target_temperature, this->min_temperature_, this->max_temperature_);
188 
189  IRSenderESPHome esp_sender(this->transmitter_);
190  heatpump_ir_->send(esp_sender, power_mode_cmd, operating_mode_cmd, fan_speed_cmd, temperature_cmd, swing_v_cmd,
191  swing_h_cmd);
192 }
193 
194 } // namespace heatpumpir
195 } // namespace esphome
196 
197 #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:204
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:83
The fan mode is set to Both.
Definition: climate_mode.h:72
float target_temperature
The target temperature of the climate device.
Definition: climate.h:183
The climate device is set to heat to reach the target temperature.
Definition: climate_mode.h:18
ClimateMode mode
The active mode of the climate device.
Definition: climate.h:175
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:81
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:84
BedJet is in Dry mode (high speed, no heat)
Definition: bedjet_const.h:22
HorizontalDirection default_horizontal_direction_
Definition: heatpumpir.h:108
BedJet is in Cool mode (actually "Fan only" mode)
Definition: bedjet_const.h:20
The fan mode is set to Horizontal.
Definition: climate_mode.h:76
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:132
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:14
const uint32_t FAN_AUTO
The climate device is adjusting the temperatre dynamically.
Definition: climate_mode.h:27
The fan mode is set to Vertical.
Definition: climate_mode.h:74
VerticalDirection default_vertical_direction_
Definition: heatpumpir.h:109
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition: climate.cpp:384
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:198
const std::map< Protocol, std::function< HeatpumpIR *()> > PROTOCOL_CONSTRUCTOR_MAP
Definition: heatpumpir.cpp:15
Definition: a4988.cpp:4
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