ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
hitachi_ac344.cpp
Go to the documentation of this file.
1#include "hitachi_ac344.h"
3
4#include <cstring>
5
7
8ESPHOME_LOG_TAG(TAG, "climate.hitachi_ac344");
9
10void set_bits(uint8_t *const dst, const uint8_t offset, const uint8_t nbits, const uint8_t data) {
11 if (offset >= 8 || !nbits)
12 return; // Short circuit as it won't change.
13 // Calculate the mask for the supplied value.
14 uint8_t mask = UINT8_MAX >> (8 - ((nbits > 8) ? 8 : nbits));
15 // Calculate the mask & clear the space for the data.
16 // Clear the destination bits.
17 *dst &= ~(uint8_t) (mask << offset);
18 // Merge in the data.
19 *dst |= ((data & mask) << offset);
20}
21
22uint8_t *invert_byte_pairs(uint8_t *ptr, const uint16_t length) {
23 for (uint16_t i = 1; i < length; i += 2) {
24 // Code done this way to avoid a compiler warning bug.
25 uint8_t inv = ~*(ptr + i - 1);
26 *(ptr + i) = inv;
27 }
28 return ptr;
29}
30
32
37
39
41 uint8_t new_mode = mode;
42 switch (mode) {
43 // Fan mode sets a special temp.
46 break;
50 break;
51 default:
52 new_mode = HITACHI_AC344_MODE_COOL;
53 }
55 if (new_mode != HITACHI_AC344_MODE_FAN)
57 set_fan_(get_fan_()); // Reset the fan speed after the mode change.
58 set_power_(true);
59}
60
61void HitachiClimate::set_temp_(uint8_t celsius, bool set_previous) {
62 uint8_t temp;
63 temp = std::min(celsius, HITACHI_AC344_TEMP_MAX);
64 temp = std::max(temp, HITACHI_AC344_TEMP_MIN);
66 if (previous_temp_ > temp) {
68 } else if (previous_temp_ < temp) {
70 }
71 if (set_previous)
72 previous_temp_ = temp;
73}
74
76
77void HitachiClimate::set_fan_(uint8_t speed) {
78 uint8_t new_speed = std::max(speed, HITACHI_AC344_FAN_MIN);
79 uint8_t fan_max = HITACHI_AC344_FAN_MAX;
80
81 // Only 2 x low speeds in Dry mode or Auto
83 fan_max = HITACHI_AC344_FAN_AUTO;
84 } else if (get_mode_() == HITACHI_AC344_MODE_DRY) {
87 // Fan Mode does not have auto. Set to safe low
88 new_speed = HITACHI_AC344_FAN_MIN;
89 }
90
91 new_speed = std::min(new_speed, fan_max);
92 // Handle the setting the button value if we are going to change the value.
93 if (new_speed != get_fan_())
95 // Set the values
96
98 remote_state_[9] = 0x92;
99
100 // When fan is at min/max, additional bytes seem to be set
101 if (new_speed == HITACHI_AC344_FAN_MIN)
102 remote_state_[9] = 0x98;
103 remote_state_[29] = 0x01;
104}
105
109
113
121
125
127
128static bool swing_vertical(climate::ClimateSwingMode mode) {
130}
131
135
137 // A repeated write resends the frame, so a missed burst can be retried
140 this->publish_mildew_proof_();
141}
142
144#ifdef USE_SWITCH
145 PUBLISH_STATE(this->mildew_proof_switch_, this->get_mildew_proof());
146#endif
147}
148
149void HitachiClimate::setup() {
150 // Seed the frame before the base publishes the restored state; an on_state automation may transmit from that
151 // callback. Nothing is sent at boot, the restored settings ride along with the next frame.
152 auto restore = this->restore_state_();
153 if (restore.has_value()) {
154 set_swing_v_(swing_vertical(restore->swing_mode));
155 }
156#ifdef USE_SWITCH
157 if (this->mildew_proof_switch_ != nullptr) {
158 if (auto initial = this->mildew_proof_switch_->get_initial_write_state())
160 }
161#endif
162 this->setup_(restore);
163 this->publish_mildew_proof_();
164}
165
167
169 switch (this->mode) {
172 break;
175 break;
178 break;
181 break;
184 break;
186 set_power_(false);
187 break;
188 default:
189 ESP_LOGW(TAG, "Unsupported mode: %s", LOG_STR_ARG(climate_mode_to_string(this->mode)));
190 }
191
192 set_temp_(static_cast<uint8_t>(this->target_temperature));
193
194 switch (this->fan_mode.value_or(climate::CLIMATE_FAN_ON)) {
197 break;
200 break;
203 break;
206 break;
209 default:
211 }
212
213 const bool swing_v = swing_vertical(this->swing_mode);
214 const bool swing_h =
217 // The unit ignores the swing bit and toggles on the swing button, so only a vertical swing change presses it;
218 // everything else sends the given button
220 set_swing_v_(swing_v);
221
222 // The remote sends this when powering off with mildew proof on
223 if (this->mode == climate::CLIMATE_MODE_OFF && this->get_mildew_proof())
224 remote_state_[9] = 0xA5;
225
227
228 auto transmit = this->transmitter_->transmit();
229 auto *data = transmit.get_data();
230 data->set_carrier_frequency(HITACHI_AC344_FREQ);
231
232 uint8_t repeat = 0;
233 for (uint8_t r = 0; r <= repeat; r++) {
234 // Header
236 // Data
237 for (uint8_t i : remote_state_) {
238 for (uint8_t j = 0; j < 8; j++) {
239 data->mark(HITACHI_AC344_BIT_MARK);
240 bool bit = i & (1 << j);
242 }
243 }
244 // Footer
246 }
247 transmit.perform();
248
249 dump_state_("Sent", remote_state_);
250}
251
252bool HitachiClimate::parse_mode_(const uint8_t remote_state[]) {
253 uint8_t power = remote_state[HITACHI_AC344_POWER_BYTE];
254 ESP_LOGV(TAG, "Power: %02X %02X", remote_state[HITACHI_AC344_POWER_BYTE], power);
255 uint8_t mode = remote_state[HITACHI_AC344_MODE_BYTE] & 0xF;
256 ESP_LOGV(TAG, "Mode: %02X %02X", remote_state[HITACHI_AC344_MODE_BYTE], mode);
257 if (power == HITACHI_AC344_POWER_ON) {
258 switch (mode) {
260 this->mode = climate::CLIMATE_MODE_COOL;
261 break;
263 this->mode = climate::CLIMATE_MODE_DRY;
264 break;
266 this->mode = climate::CLIMATE_MODE_HEAT;
267 break;
270 break;
273 break;
274 default:
275 break;
276 }
277 } else {
278 this->mode = climate::CLIMATE_MODE_OFF;
279 }
280 return true;
281}
282
283bool HitachiClimate::parse_temperature_(const uint8_t remote_state[]) {
284 uint8_t temperature =
287 ESP_LOGV(TAG, "Temperature: %02X %02u %04f", remote_state[HITACHI_AC344_TEMP_BYTE], temperature,
288 this->target_temperature);
289 return true;
290}
291
292bool HitachiClimate::parse_fan_(const uint8_t remote_state[]) {
293 uint8_t fan_mode = remote_state[HITACHI_AC344_FAN_BYTE] >> 4 & 0xF;
294 ESP_LOGV(TAG, "Fan: %02X %02X", remote_state[HITACHI_AC344_FAN_BYTE], fan_mode);
295 switch (fan_mode) {
297 this->fan_mode = climate::CLIMATE_FAN_QUIET;
298 break;
300 this->fan_mode = climate::CLIMATE_FAN_LOW;
301 break;
303 this->fan_mode = climate::CLIMATE_FAN_MEDIUM;
304 break;
307 this->fan_mode = climate::CLIMATE_FAN_HIGH;
308 break;
310 this->fan_mode = climate::CLIMATE_FAN_AUTO;
311 break;
312 default:
313 break;
314 }
315 return true;
316}
317
318bool HitachiClimate::parse_swing_(const uint8_t remote_state[]) {
319 uint8_t swing_modeh =
321 ESP_LOGV(TAG, "SwingH: %02X %02X", remote_state[HITACHI_AC344_SWINGH_BYTE], swing_modeh);
322 bool swing_v = GETBIT8(remote_state[HITACHI_AC344_SWINGV_BYTE], HITACHI_AC344_SWINGV_OFFSET);
323 ESP_LOGV(TAG, "SwingV: %02X %s", remote_state[HITACHI_AC344_SWINGV_BYTE], ONOFF(swing_v));
324
325 if (swing_modeh != HITACHI_AC344_SWINGH_AUTO) {
327 } else {
329 }
330
331 return true;
332}
333
335 // Validate header
336 if (!data.expect_item(HITACHI_AC344_HDR_MARK, HITACHI_AC344_HDR_SPACE)) {
337 ESP_LOGVV(TAG, "Header fail");
338 return false;
339 }
340
341 uint8_t recv_state[HITACHI_AC344_STATE_LENGTH] = {0};
342 // Read all bytes.
343 for (uint8_t pos = 0; pos < HITACHI_AC344_STATE_LENGTH; pos++) {
344 // Read bit
345 for (int8_t bit = 0; bit < 8; bit++) {
346 if (data.expect_item(HITACHI_AC344_BIT_MARK, HITACHI_AC344_ONE_SPACE)) {
347 recv_state[pos] |= 1 << bit;
348 } else if (!data.expect_item(HITACHI_AC344_BIT_MARK, HITACHI_AC344_ZERO_SPACE)) {
349 ESP_LOGVV(TAG, "Byte %d bit %d fail", pos, bit);
350 return false;
351 }
352 }
353 }
354
355 // Validate footer
356 if (!data.expect_mark(HITACHI_AC344_BIT_MARK)) {
357 ESP_LOGVV(TAG, "Footer fail");
358 return false;
359 }
360
361 dump_state_("Recv", recv_state);
362
363 // parse mode
364 this->parse_mode_(recv_state);
365 // parse temperature
366 this->parse_temperature_(recv_state);
367 // parse fan
368 this->parse_fan_(recv_state);
369 // parse swingv
370 this->parse_swing_(recv_state);
371 // Copy first so an on_state automation transmits from the received frame
372 memcpy(remote_state_, recv_state, HITACHI_AC344_STATE_LENGTH);
373 this->publish_state();
374 this->publish_mildew_proof_();
375
376 return true;
377}
378
379void HitachiClimate::dump_state_(const char action[], uint8_t state[]) {
381}
382
383} // namespace esphome::hitachi_ac344
BedjetMode mode
BedJet operating mode.
ClimateMode mode
The active mode of the climate device.
Definition climate.h:317
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:311
float target_temperature
The target temperature of the climate device.
Definition climate.h:298
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition climate.h:323
ClimateAction action
The active state of the climate device.
Definition climate.h:320
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:432
optional< ClimateDeviceRestoreState > restore_state_()
Restore the state of the climate device, call this from your setup() method.
Definition climate.cpp:365
void setup_(optional< climate::ClimateDeviceRestoreState > restore)
setup() with the restored state already loaded, for subclasses that read it first.
bool on_receive(remote_base::RemoteReceiveData data) override
void dump_state_(const char action[], uint8_t remote_state[])
bool parse_temperature_(const uint8_t remote_state[])
bool parse_mode_(const uint8_t remote_state[])
void set_temp_(uint8_t celsius, bool set_previous=true)
bool parse_fan_(const uint8_t remote_state[])
uint8_t remote_state_[HITACHI_AC344_STATE_LENGTH]
bool parse_swing_(const uint8_t remote_state[])
void set_mildew_proof(bool on)
Mildew proof keeps the fan running after power off to dry the unit.
float position
Definition cover.h:0
int speed
Definition fan.h:3
bool state
Definition fan.h:2
ClimateSwingMode
Enum for all modes a climate swing can be in NOTE: If adding values, update ClimateSwingModeMask in c...
@ CLIMATE_SWING_OFF
The swing mode is set to Off.
@ 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_FAN_MEDIUM
The fan mode is set to Medium.
@ CLIMATE_FAN_ON
The fan mode is set to On.
@ CLIMATE_FAN_AUTO
The fan mode is set to Auto.
@ CLIMATE_FAN_LOW
The fan mode is set to Low.
@ CLIMATE_FAN_QUIET
The fan mode is set to Quiet.
@ CLIMATE_FAN_HIGH
The fan mode is set to High.
const uint8_t HITACHI_AC344_SWINGV_OFFSET
const uint8_t HITACHI_AC344_MILDEWPROOF_BYTE
const uint8_t HITACHI_AC344_TEMP_MIN
const uint8_t HITACHI_AC344_BUTTON_FAN
const uint16_t HITACHI_AC344_BIT_MARK
const uint8_t HITACHI_AC344_BUTTON_BYTE
const uint8_t HITACHI_AC344_TEMP_BYTE
const uint8_t HITACHI_AC344_MODE_BYTE
const uint8_t HITACHI_AC344_SWINGV_BYTE
const uint16_t HITACHI_AC344_ZERO_SPACE
const uint8_t HITACHI_AC344_SWINGH_SIZE
const uint8_t HITACHI_AC344_FAN_MIN
const uint8_t HITACHI_AC344_BUTTON_POWER
uint8_t * invert_byte_pairs(uint8_t *ptr, const uint16_t length)
const uint8_t HITACHI_AC344_MILDEWPROOF_OFFSET
const uint8_t HITACHI_AC344_FAN_MAX
const uint8_t HITACHI_AC344_MODE_DRY
const uint16_t HITACHI_AC344_HDR_SPACE
const uint8_t HITACHI_AC344_BUTTON_MILDEWPROOF
const uint8_t HITACHI_AC344_FAN_MAX_DRY
const uint8_t HITACHI_AC344_MODE_FAN
void set_bits(uint8_t *const dst, const uint8_t offset, const uint8_t nbits, const uint8_t data)
const uint16_t HITACHI_AC344_FREQ
const uint8_t HITACHI_AC344_SWINGH_AUTO
const uint8_t HITACHI_AC344_BUTTON_TEMP_DOWN
const uint8_t HITACHI_AC344_MODE_AUTO
const uint8_t HITACHI_AC344_BUTTON_TEMP_UP
const uint8_t HITACHI_AC344_MODE_COOL
const uint16_t HITACHI_AC344_STATE_LENGTH
const uint8_t HITACHI_AC344_SWINGH_OFFSET
const uint8_t HITACHI_AC344_POWER_ON
const uint8_t HITACHI_AC344_TEMP_SIZE
const uint8_t HITACHI_AC344_FAN_HIGH
const uint8_t HITACHI_AC344_MODE_HEAT
const uint8_t HITACHI_AC344_FAN_BYTE
const uint16_t HITACHI_AC344_HDR_MARK
ESPHOME_LOG_TAG(TAG, "climate.hitachi_ac344")
const uint8_t HITACHI_AC344_SWINGH_LEFT_MAX
const uint8_t HITACHI_AC344_TEMP_MAX
const uint8_t HITACHI_AC344_TEMP_OFFSET
const uint8_t HITACHI_AC344_FAN_LOW
const uint32_t HITACHI_AC344_MIN_GAP
const uint8_t HITACHI_AC344_FAN_MEDIUM
const uint8_t HITACHI_AC344_POWER_OFF
const uint8_t HITACHI_AC344_SWINGH_MIDDLE
const uint8_t HITACHI_AC344_POWER_BYTE
const uint8_t HITACHI_AC344_FAN_AUTO
const uint16_t HITACHI_AC344_ONE_SPACE
const uint8_t HITACHI_AC344_SWINGH_BYTE
const uint8_t HITACHI_AC344_BUTTON_SWINGV
const uint8_t HITACHI_AC344_TEMP_FAN
ESPHOME_ALWAYS_INLINE constexpr void assign_bits(T &value, std::type_identity_t< T > mask, bool on)
Set the bits of mask in value when on is true, clear them otherwise.
Definition helpers.h:1150
size_t size_t pos
Definition helpers.h:1333
uint16_t temperature
Definition sun_gtil2.cpp:12
uint16_t length
Definition tt21100.cpp:0