ESPHome  2024.11.0
hitachi_ac344.cpp
Go to the documentation of this file.
1 #include "hitachi_ac344.h"
2 
3 namespace esphome {
4 namespace hitachi_ac344 {
5 
6 static const char *const TAG = "climate.hitachi_ac344";
7 
8 void set_bits(uint8_t *const dst, const uint8_t offset, const uint8_t nbits, const uint8_t data) {
9  if (offset >= 8 || !nbits)
10  return; // Short circuit as it won't change.
11  // Calculate the mask for the supplied value.
12  uint8_t mask = UINT8_MAX >> (8 - ((nbits > 8) ? 8 : nbits));
13  // Calculate the mask & clear the space for the data.
14  // Clear the destination bits.
15  *dst &= ~(uint8_t) (mask << offset);
16  // Merge in the data.
17  *dst |= ((data & mask) << offset);
18 }
19 
20 void set_bit(uint8_t *const data, const uint8_t position, const bool on) {
21  uint8_t mask = 1 << position;
22  if (on) {
23  *data |= mask;
24  } else {
25  *data &= ~mask;
26  }
27 }
28 
29 uint8_t *invert_byte_pairs(uint8_t *ptr, const uint16_t length) {
30  for (uint16_t i = 1; i < length; i += 2) {
31  // Code done this way to avoid a compiler warning bug.
32  uint8_t inv = ~*(ptr + i - 1);
33  *(ptr + i) = inv;
34  }
35  return ptr;
36 }
37 
39 
43 }
44 
46 
48  uint8_t new_mode = mode;
49  switch (mode) {
50  // Fan mode sets a special temp.
53  break;
57  break;
58  default:
59  new_mode = HITACHI_AC344_MODE_COOL;
60  }
62  if (new_mode != HITACHI_AC344_MODE_FAN)
64  set_fan_(get_fan_()); // Reset the fan speed after the mode change.
65  set_power_(true);
66 }
67 
68 void HitachiClimate::set_temp_(uint8_t celsius, bool set_previous) {
69  uint8_t temp;
70  temp = std::min(celsius, HITACHI_AC344_TEMP_MAX);
71  temp = std::max(temp, HITACHI_AC344_TEMP_MIN);
73  if (previous_temp_ > temp) {
75  } else if (previous_temp_ < temp) {
77  }
78  if (set_previous)
79  previous_temp_ = temp;
80 }
81 
83 
85  uint8_t new_speed = std::max(speed, HITACHI_AC344_FAN_MIN);
86  uint8_t fan_max = HITACHI_AC344_FAN_MAX;
87 
88  // Only 2 x low speeds in Dry mode or Auto
90  fan_max = HITACHI_AC344_FAN_AUTO;
91  } else if (get_mode_() == HITACHI_AC344_MODE_DRY) {
92  fan_max = HITACHI_AC344_FAN_MAX_DRY;
93  } else if (get_mode_() == HITACHI_AC344_MODE_FAN && speed == HITACHI_AC344_FAN_AUTO) {
94  // Fan Mode does not have auto. Set to safe low
95  new_speed = HITACHI_AC344_FAN_MIN;
96  }
97 
98  new_speed = std::min(new_speed, fan_max);
99  // Handle the setting the button value if we are going to change the value.
100  if (new_speed != get_fan_())
102  // Set the values
103 
104  set_bits(&remote_state_[HITACHI_AC344_FAN_BYTE], 4, 4, new_speed);
105  remote_state_[9] = 0x92;
106 
107  // When fan is at min/max, additional bytes seem to be set
108  if (new_speed == HITACHI_AC344_FAN_MIN)
109  remote_state_[9] = 0x98;
110  remote_state_[29] = 0x01;
111 }
112 
114  uint8_t button = get_button_(); // Get the current button value.
115  if (on) {
116  button = HITACHI_AC344_BUTTON_SWINGV; // Set the button to SwingV.
117  } else if (button == HITACHI_AC344_BUTTON_SWINGV) { // Asked to unset it
118  // It was set previous, so use Power as a default
120  }
121  set_button_(button);
122 }
123 
125 
127  set_swing_v_toggle_(on); // Set the button value.
129 }
130 
133 }
134 
136  if (position > HITACHI_AC344_SWINGH_LEFT_MAX)
140 }
141 
144 }
145 
147 
149 
151  switch (this->mode) {
154  break;
157  break;
160  break;
163  break;
166  break;
168  set_power_(false);
169  break;
170  default:
171  ESP_LOGW(TAG, "Unsupported mode: %s", LOG_STR_ARG(climate_mode_to_string(this->mode)));
172  }
173 
174  set_temp_(static_cast<uint8_t>(this->target_temperature));
175 
176  switch (this->fan_mode.value()) {
179  break;
182  break;
185  break;
188  default:
190  }
191 
192  switch (this->swing_mode) {
194  set_swing_v_(true);
196  break;
198  set_swing_v_(true);
200  break;
202  set_swing_v_(false);
204  break;
206  set_swing_v_(false);
208  break;
209  }
210 
211  // TODO: find change value to set button, now always set to power button
213 
215 
216  auto transmit = this->transmitter_->transmit();
217  auto *data = transmit.get_data();
219 
220  uint8_t repeat = 0;
221  for (uint8_t r = 0; r <= repeat; r++) {
222  // Header
224  // Data
225  for (uint8_t i : remote_state_) {
226  for (uint8_t j = 0; j < 8; j++) {
227  data->mark(HITACHI_AC344_BIT_MARK);
228  bool bit = i & (1 << j);
230  }
231  }
232  // Footer
234  }
235  transmit.perform();
236 
237  dump_state_("Sent", remote_state_);
238 }
239 
240 bool HitachiClimate::parse_mode_(const uint8_t remote_state[]) {
241  uint8_t power = remote_state[HITACHI_AC344_POWER_BYTE];
242  ESP_LOGV(TAG, "Power: %02X %02X", remote_state[HITACHI_AC344_POWER_BYTE], power);
243  uint8_t mode = remote_state[HITACHI_AC344_MODE_BYTE] & 0xF;
244  ESP_LOGV(TAG, "Mode: %02X %02X", remote_state[HITACHI_AC344_MODE_BYTE], mode);
245  if (power == HITACHI_AC344_POWER_ON) {
246  switch (mode) {
248  this->mode = climate::CLIMATE_MODE_COOL;
249  break;
251  this->mode = climate::CLIMATE_MODE_DRY;
252  break;
254  this->mode = climate::CLIMATE_MODE_HEAT;
255  break;
257  this->mode = climate::CLIMATE_MODE_HEAT_COOL;
258  break;
260  this->mode = climate::CLIMATE_MODE_FAN_ONLY;
261  break;
262  }
263  } else {
264  this->mode = climate::CLIMATE_MODE_OFF;
265  }
266  return true;
267 }
268 
269 bool HitachiClimate::parse_temperature_(const uint8_t remote_state[]) {
270  uint8_t temperature =
273  ESP_LOGV(TAG, "Temperature: %02X %02u %04f", remote_state[HITACHI_AC344_TEMP_BYTE], temperature,
274  this->target_temperature);
275  return true;
276 }
277 
278 bool HitachiClimate::parse_fan_(const uint8_t remote_state[]) {
279  uint8_t fan_mode = remote_state[HITACHI_AC344_FAN_BYTE] >> 4 & 0xF;
280  ESP_LOGV(TAG, "Fan: %02X %02X", remote_state[HITACHI_AC344_FAN_BYTE], fan_mode);
281  switch (fan_mode) {
284  this->fan_mode = climate::CLIMATE_FAN_LOW;
285  break;
287  this->fan_mode = climate::CLIMATE_FAN_MEDIUM;
288  break;
291  this->fan_mode = climate::CLIMATE_FAN_HIGH;
292  break;
294  this->fan_mode = climate::CLIMATE_FAN_AUTO;
295  break;
296  }
297  return true;
298 }
299 
300 bool HitachiClimate::parse_swing_(const uint8_t remote_state[]) {
301  uint8_t swing_modeh =
303  ESP_LOGV(TAG, "SwingH: %02X %02X", remote_state[HITACHI_AC344_SWINGH_BYTE], swing_modeh);
304 
305  if ((swing_modeh & 0x3) == 0x3) {
307  } else {
309  }
310 
311  return true;
312 }
313 
315  // Validate header
317  ESP_LOGVV(TAG, "Header fail");
318  return false;
319  }
320 
321  uint8_t recv_state[HITACHI_AC344_STATE_LENGTH] = {0};
322  // Read all bytes.
323  for (uint8_t pos = 0; pos < HITACHI_AC344_STATE_LENGTH; pos++) {
324  // Read bit
325  for (int8_t bit = 0; bit < 8; bit++) {
327  recv_state[pos] |= 1 << bit;
329  ESP_LOGVV(TAG, "Byte %d bit %d fail", pos, bit);
330  return false;
331  }
332  }
333  }
334 
335  // Validate footer
336  if (!data.expect_mark(HITACHI_AC344_BIT_MARK)) {
337  ESP_LOGVV(TAG, "Footer fail");
338  return false;
339  }
340 
341  dump_state_("Recv", recv_state);
342 
343  // parse mode
344  this->parse_mode_(recv_state);
345  // parse temperature
346  this->parse_temperature_(recv_state);
347  // parse fan
348  this->parse_fan_(recv_state);
349  // parse swingv
350  this->parse_swing_(recv_state);
351  this->publish_state();
352  for (uint8_t i = 0; i < HITACHI_AC344_STATE_LENGTH; i++)
353  remote_state_[i] = recv_state[i];
354 
355  return true;
356 }
357 
358 void HitachiClimate::dump_state_(const char action[], uint8_t state[]) {
359  for (uint16_t i = 0; i < HITACHI_AC344_STATE_LENGTH - 10; i += 10) {
360  ESP_LOGV(TAG, "%s: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X", action, state[i + 0], state[i + 1],
361  state[i + 2], state[i + 3], state[i + 4], state[i + 5], state[i + 6], state[i + 7], state[i + 8],
362  state[i + 9]);
363  }
364  ESP_LOGV(TAG, "%s: %02X %02X %02X", action, state[40], state[41], state[42]);
365 }
366 
367 } // namespace hitachi_ac344
368 } // namespace esphome
The fan mode is set to Low.
Definition: climate_mode.h:54
const uint16_t HITACHI_AC344_HDR_MARK
Definition: hitachi_ac344.h:9
value_type const & value() const
Definition: optional.h:89
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition: climate.h:202
const uint8_t HITACHI_AC344_FAN_AUTO
Definition: hitachi_ac344.h:49
The fan mode is set to Both.
Definition: climate_mode.h:74
const uint8_t HITACHI_AC344_SWINGH_BYTE
Definition: hitachi_ac344.h:57
void set_temp_(uint8_t celsius, bool set_previous=false)
float target_temperature
The target temperature of the climate device.
Definition: climate.h:186
const uint8_t HITACHI_AC344_SWINGH_OFFSET
Definition: hitachi_ac344.h:58
const uint8_t HITACHI_AC344_TEMP_SIZE
Definition: hitachi_ac344.h:30
void set_carrier_frequency(uint32_t carrier_frequency)
Definition: remote_base.h:34
const uint8_t HITACHI_AC344_MODE_AUTO
Definition: hitachi_ac344.h:42
void set_bit(uint8_t *const data, const uint8_t position, const bool on)
const uint8_t HITACHI_AC344_FAN_BYTE
Definition: hitachi_ac344.h:44
const uint8_t HITACHI_AC344_BUTTON_SWINGV
Definition: hitachi_ac344.h:24
const LogString * climate_mode_to_string(ClimateMode mode)
Convert the given ClimateMode to a human-readable string.
Definition: climate_mode.cpp:6
The climate device is set to heat to reach the target temperature.
Definition: climate_mode.h:18
const uint8_t HITACHI_AC344_MODE_FAN
Definition: hitachi_ac344.h:38
bool on_receive(remote_base::RemoteReceiveData data) override
void set_bits(uint8_t *const dst, const uint8_t offset, const uint8_t nbits, const uint8_t data)
ClimateMode mode
The active mode of the climate device.
Definition: climate.h:173
int speed
Definition: fan.h:35
const uint8_t HITACHI_AC344_BUTTON_POWER
Definition: hitachi_ac344.h:18
const uint8_t HITACHI_AC344_TEMP_MAX
Definition: hitachi_ac344.h:32
The climate device is set to dry/humidity mode.
Definition: climate_mode.h:22
const uint8_t HITACHI_AC344_TEMP_FAN
Definition: hitachi_ac344.h:33
uint8_t remote_state_[HITACHI_AC344_STATE_LENGTH]
Definition: hitachi_ac344.h:88
bool parse_mode_(const uint8_t remote_state[])
const uint8_t HITACHI_AC344_TEMP_OFFSET
Definition: hitachi_ac344.h:29
const uint8_t HITACHI_AC344_FAN_MEDIUM
Definition: hitachi_ac344.h:47
const uint8_t HITACHI_AC344_SWINGV_BYTE
Definition: hitachi_ac344.h:67
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
const uint8_t HITACHI_AC344_FAN_HIGH
Definition: hitachi_ac344.h:48
The fan mode is set to Auto.
Definition: climate_mode.h:52
const uint32_t HITACHI_AC344_MIN_GAP
Definition: hitachi_ac344.h:14
bool parse_swing_(const uint8_t remote_state[])
const uint8_t HITACHI_AC344_BUTTON_BYTE
Definition: hitachi_ac344.h:17
const uint8_t HITACHI_AC344_SWINGH_AUTO
Definition: hitachi_ac344.h:60
const uint8_t HITACHI_AC344_BUTTON_TEMP_DOWN
Definition: hitachi_ac344.h:22
RemoteTransmitterBase * transmitter_
Definition: remote_base.h:276
const uint8_t HITACHI_AC344_SWINGH_SIZE
Definition: hitachi_ac344.h:59
const uint8_t HITACHI_AC344_POWER_BYTE
Definition: hitachi_ac344.h:53
const uint8_t HITACHI_AC344_POWER_OFF
Definition: hitachi_ac344.h:55
uint16_t temperature
Definition: sun_gtil2.cpp:26
The climate device is set to heat/cool to reach the target temperature.
Definition: climate_mode.h:14
The fan mode is set to Vertical.
Definition: climate_mode.h:76
const uint8_t HITACHI_AC344_BUTTON_SWINGH
Definition: hitachi_ac344.h:25
const uint8_t HITACHI_AC344_FAN_MAX_DRY
Definition: hitachi_ac344.h:51
bool parse_temperature_(const uint8_t remote_state[])
const uint8_t HITACHI_AC344_BUTTON_TEMP_UP
Definition: hitachi_ac344.h:23
const uint16_t HITACHI_AC344_HDR_SPACE
Definition: hitachi_ac344.h:10
const uint8_t HITACHI_AC344_BUTTON_FAN
Definition: hitachi_ac344.h:21
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
const uint8_t HITACHI_AC344_SWINGV_OFFSET
Definition: hitachi_ac344.h:68
The swing mode is set to Off.
Definition: climate_mode.h:72
The climate device is off.
Definition: climate_mode.h:12
bool parse_fan_(const uint8_t remote_state[])
const uint8_t HITACHI_AC344_TEMP_MIN
Definition: hitachi_ac344.h:31
const uint16_t HITACHI_AC344_FREQ
Definition: hitachi_ac344.h:15
const uint8_t HITACHI_AC344_FAN_LOW
Definition: hitachi_ac344.h:46
const uint8_t HITACHI_AC344_MODE_DRY
Definition: hitachi_ac344.h:40
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition: climate.h:199
const uint16_t HITACHI_AC344_ZERO_SPACE
Definition: hitachi_ac344.h:13
const uint16_t HITACHI_AC344_BIT_MARK
Definition: hitachi_ac344.h:11
const uint8_t HITACHI_AC344_MODE_BYTE
Definition: hitachi_ac344.h:37
void dump_state_(const char action[], uint8_t remote_state[])
const uint8_t HITACHI_AC344_SWINGH_LEFT_MAX
Definition: hitachi_ac344.h:65
const uint8_t HITACHI_AC344_SWINGH_MIDDLE
Definition: hitachi_ac344.h:63
The fan mode is set to On.
Definition: climate_mode.h:48
const uint8_t HITACHI_AC344_MODE_COOL
Definition: hitachi_ac344.h:39
uint16_t length
Definition: tt21100.cpp:12
uint8_t * invert_byte_pairs(uint8_t *ptr, const uint16_t length)
const uint8_t HITACHI_AC344_TEMP_BYTE
Definition: hitachi_ac344.h:28
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
const uint16_t HITACHI_AC344_ONE_SPACE
Definition: hitachi_ac344.h:12
const uint8_t HITACHI_AC344_MODE_HEAT
Definition: hitachi_ac344.h:41
const uint8_t HITACHI_AC344_FAN_MIN
Definition: hitachi_ac344.h:45
The fan mode is set to Medium.
Definition: climate_mode.h:56
float position
Definition: cover.h:14
const uint16_t HITACHI_AC344_STATE_LENGTH
Definition: hitachi_ac344.h:73
bool expect_item(uint32_t mark, uint32_t space)
Definition: remote_base.cpp:74
The climate device only has the fan enabled, no heating or cooling is taking place.
Definition: climate_mode.h:20
ClimateAction action
The active state of the climate device.
Definition: climate.h:176
bool state
Definition: fan.h:34
const uint8_t HITACHI_AC344_POWER_ON
Definition: hitachi_ac344.h:54
const uint8_t HITACHI_AC344_FAN_MAX
Definition: hitachi_ac344.h:50