ESPHome  2024.10.2
toshiba.cpp
Go to the documentation of this file.
1 #include "toshiba.h"
2 
3 #include <vector>
4 
5 namespace esphome {
6 namespace toshiba {
7 
8 struct RacPt1411hwruFanSpeed {
9  uint8_t code1;
10  uint8_t code2;
11 };
12 
13 static const char *const TAG = "toshiba.climate";
14 // Timings for IR bits/data
15 const uint16_t TOSHIBA_HEADER_MARK = 4380;
16 const uint16_t TOSHIBA_HEADER_SPACE = 4370;
17 const uint16_t TOSHIBA_GAP_SPACE = 5480;
18 const uint16_t TOSHIBA_PACKET_SPACE = 10500;
19 const uint16_t TOSHIBA_BIT_MARK = 540;
20 const uint16_t TOSHIBA_ZERO_SPACE = 540;
21 const uint16_t TOSHIBA_ONE_SPACE = 1620;
22 const uint16_t TOSHIBA_CARRIER_FREQUENCY = 38000;
23 const uint8_t TOSHIBA_HEADER_LENGTH = 4;
24 // Generic Toshiba commands/flags
25 const uint8_t TOSHIBA_COMMAND_DEFAULT = 0x01;
26 const uint8_t TOSHIBA_COMMAND_TIMER = 0x02;
27 const uint8_t TOSHIBA_COMMAND_POWER = 0x08;
28 const uint8_t TOSHIBA_COMMAND_MOTION = 0x02;
29 
30 const uint8_t TOSHIBA_MODE_AUTO = 0x00;
31 const uint8_t TOSHIBA_MODE_COOL = 0x01;
32 const uint8_t TOSHIBA_MODE_DRY = 0x02;
33 const uint8_t TOSHIBA_MODE_HEAT = 0x03;
34 const uint8_t TOSHIBA_MODE_FAN_ONLY = 0x04;
35 const uint8_t TOSHIBA_MODE_OFF = 0x07;
36 
37 const uint8_t TOSHIBA_FAN_SPEED_AUTO = 0x00;
38 const uint8_t TOSHIBA_FAN_SPEED_QUIET = 0x20;
39 const uint8_t TOSHIBA_FAN_SPEED_1 = 0x40;
40 const uint8_t TOSHIBA_FAN_SPEED_2 = 0x60;
41 const uint8_t TOSHIBA_FAN_SPEED_3 = 0x80;
42 const uint8_t TOSHIBA_FAN_SPEED_4 = 0xa0;
43 const uint8_t TOSHIBA_FAN_SPEED_5 = 0xc0;
44 
45 const uint8_t TOSHIBA_POWER_HIGH = 0x01;
46 const uint8_t TOSHIBA_POWER_ECO = 0x03;
47 
48 const uint8_t TOSHIBA_MOTION_SWING = 0x04;
49 const uint8_t TOSHIBA_MOTION_FIX = 0x00;
50 
51 // RAC-PT1411HWRU temperature code flag bits
52 const uint8_t RAC_PT1411HWRU_FLAG_FAH = 0x01;
53 const uint8_t RAC_PT1411HWRU_FLAG_FRAC = 0x20;
54 const uint8_t RAC_PT1411HWRU_FLAG_NEG = 0x10;
55 // RAC-PT1411HWRU temperature short code flags mask
56 const uint8_t RAC_PT1411HWRU_FLAG_MASK = 0x0F;
57 // RAC-PT1411HWRU Headers, Footers and such
58 const uint8_t RAC_PT1411HWRU_MESSAGE_HEADER0 = 0xB2;
59 const uint8_t RAC_PT1411HWRU_MESSAGE_HEADER1 = 0xD5;
61 // RAC-PT1411HWRU "Comfort Sense" feature bits
62 const uint8_t RAC_PT1411HWRU_CS_ENABLED = 0x40;
63 const uint8_t RAC_PT1411HWRU_CS_DATA = 0x80;
64 const uint8_t RAC_PT1411HWRU_CS_HEADER = 0xBA;
65 const uint8_t RAC_PT1411HWRU_CS_FOOTER_AUTO = 0x7A;
66 const uint8_t RAC_PT1411HWRU_CS_FOOTER_COOL = 0x72;
67 const uint8_t RAC_PT1411HWRU_CS_FOOTER_HEAT = 0x7E;
68 // RAC-PT1411HWRU Swing
69 const uint8_t RAC_PT1411HWRU_SWING_HEADER = 0xB9;
70 const std::vector<uint8_t> RAC_PT1411HWRU_SWING_VERTICAL{0xB9, 0x46, 0xF5, 0x0A, 0x04, 0xFB};
71 const std::vector<uint8_t> RAC_PT1411HWRU_SWING_OFF{0xB9, 0x46, 0xF5, 0x0A, 0x05, 0xFA};
72 // RAC-PT1411HWRU Fan speeds
73 const uint8_t RAC_PT1411HWRU_FAN_OFF = 0x7B;
74 constexpr RacPt1411hwruFanSpeed RAC_PT1411HWRU_FAN_AUTO{0xBF, 0x66};
75 constexpr RacPt1411hwruFanSpeed RAC_PT1411HWRU_FAN_LOW{0x9F, 0x28};
76 constexpr RacPt1411hwruFanSpeed RAC_PT1411HWRU_FAN_MED{0x5F, 0x3C};
77 constexpr RacPt1411hwruFanSpeed RAC_PT1411HWRU_FAN_HIGH{0x3F, 0x64};
78 // RAC-PT1411HWRU Fan speed for Auto and Dry climate modes
79 const RacPt1411hwruFanSpeed RAC_PT1411HWRU_NO_FAN{0x1F, 0x65};
80 // RAC-PT1411HWRU Modes
81 const uint8_t RAC_PT1411HWRU_MODE_AUTO = 0x08;
82 const uint8_t RAC_PT1411HWRU_MODE_COOL = 0x00;
83 const uint8_t RAC_PT1411HWRU_MODE_DRY = 0x04;
84 const uint8_t RAC_PT1411HWRU_MODE_FAN = 0x04;
85 const uint8_t RAC_PT1411HWRU_MODE_HEAT = 0x0C;
86 const uint8_t RAC_PT1411HWRU_MODE_OFF = 0x00;
87 // RAC-PT1411HWRU Fan-only "temperature"/system off
89 // RAC-PT1411HWRU temperature codes are not sequential; they instead follow a modified Gray code.
90 // Hence these look-up tables. In addition, the upper nibble is used here for additional
91 // "negative" and "fractional value" flags as required for some temperatures.
92 // RAC-PT1411HWRU °C Temperatures (short codes)
93 const std::vector<uint8_t> RAC_PT1411HWRU_TEMPERATURE_C{0x10, 0x00, 0x01, 0x03, 0x02, 0x06, 0x07, 0x05,
94  0x04, 0x0C, 0x0D, 0x09, 0x08, 0x0A, 0x0B};
95 // RAC-PT1411HWRU °F Temperatures (short codes)
96 const std::vector<uint8_t> RAC_PT1411HWRU_TEMPERATURE_F{0x10, 0x30, 0x00, 0x20, 0x01, 0x21, 0x03, 0x23, 0x02,
97  0x22, 0x06, 0x26, 0x07, 0x05, 0x25, 0x04, 0x24, 0x0C,
98  0x2C, 0x0D, 0x2D, 0x09, 0x08, 0x28, 0x0A, 0x2A, 0x0B};
99 
101  if (this->sensor_) {
102  this->sensor_->add_on_state_callback([this](float state) {
103  this->current_temperature = state;
104  this->transmit_rac_pt1411hwru_temp_();
105  // current temperature changed, publish state
106  this->publish_state();
107  });
108  this->current_temperature = this->sensor_->state;
109  } else
110  this->current_temperature = NAN;
111  // restore set points
112  auto restore = this->restore_state_();
113  if (restore.has_value()) {
114  restore->apply(this);
115  } else {
116  // restore from defaults
118  // initialize target temperature to some value so that it's not NAN
119  this->target_temperature =
120  roundf(clamp<float>(this->current_temperature, this->minimum_temperature_, this->maximum_temperature_));
123  }
124  // Set supported modes & temperatures based on model
125  this->minimum_temperature_ = this->temperature_min_();
126  this->maximum_temperature_ = this->temperature_max_();
127  this->swing_modes_ = this->toshiba_swing_modes_();
128  // Never send nan to HA
129  if (std::isnan(this->target_temperature))
130  this->target_temperature = 24;
131 }
132 
134  if (this->model_ == MODEL_RAC_PT1411HWRU_C || this->model_ == MODEL_RAC_PT1411HWRU_F) {
135  transmit_rac_pt1411hwru_();
136  } else {
137  transmit_generic_();
138  }
139 }
140 
142  uint8_t message[16] = {0};
143  uint8_t message_length = 9;
144 
145  // Header
146  message[0] = 0xf2;
147  message[1] = 0x0d;
148 
149  // Message length
150  message[2] = message_length - 6;
151 
152  // First checksum
153  message[3] = message[0] ^ message[1] ^ message[2];
154 
155  // Command
156  message[4] = TOSHIBA_COMMAND_DEFAULT;
157 
158  // Temperature
159  uint8_t temperature = static_cast<uint8_t>(
161  message[5] = (temperature - static_cast<uint8_t>(TOSHIBA_GENERIC_TEMP_C_MIN)) << 4;
162 
163  // Mode and fan
164  uint8_t mode;
165  switch (this->mode) {
167  mode = TOSHIBA_MODE_OFF;
168  break;
169 
171  mode = TOSHIBA_MODE_HEAT;
172  break;
173 
175  mode = TOSHIBA_MODE_COOL;
176  break;
177 
179  mode = TOSHIBA_MODE_DRY;
180  break;
181 
183  mode = TOSHIBA_MODE_FAN_ONLY;
184  break;
185 
187  default:
188  mode = TOSHIBA_MODE_AUTO;
189  }
190 
191  uint8_t fan;
192  switch (this->fan_mode.value()) {
195  break;
196 
198  fan = TOSHIBA_FAN_SPEED_1;
199  break;
200 
202  fan = TOSHIBA_FAN_SPEED_3;
203  break;
204 
206  fan = TOSHIBA_FAN_SPEED_5;
207  break;
208 
210  default:
212  break;
213  }
214  message[6] = fan | mode;
215 
216  // Zero
217  message[7] = 0x00;
218 
219  // If timers bit in the command is set, two extra bytes are added here
220 
221  // If power bit is set in the command, one extra byte is added here
222 
223  // The last byte is the xor of all bytes from [4]
224  for (uint8_t i = 4; i < 8; i++) {
225  message[8] ^= message[i];
226  }
227 
228  // Transmit
229  auto transmit = this->transmitter_->transmit();
230  auto *data = transmit.get_data();
231 
232  encode_(data, message, message_length, 1);
233 
234  transmit.perform();
235 }
236 
238  uint8_t code = 0, index = 0, message[RAC_PT1411HWRU_MESSAGE_LENGTH * 2] = {0};
239  float temperature =
241  float temp_adjd = temperature - TOSHIBA_RAC_PT1411HWRU_TEMP_C_MIN;
242  auto transmit = this->transmitter_->transmit();
243  auto *data = transmit.get_data();
244 
245  // Byte 0: Header upper (0xB2)
246  message[0] = RAC_PT1411HWRU_MESSAGE_HEADER0;
247  // Byte 1: Header lower (0x4D)
248  message[1] = ~message[0];
249  // Byte 2u: Fan speed
250  // Byte 2l: 1111 (on) or 1011 (off)
251  if (this->mode == climate::CLIMATE_MODE_OFF) {
252  message[2] = RAC_PT1411HWRU_FAN_OFF;
253  } else if ((this->mode == climate::CLIMATE_MODE_HEAT_COOL) || (this->mode == climate::CLIMATE_MODE_DRY)) {
254  message[2] = RAC_PT1411HWRU_NO_FAN.code1;
255  message[7] = RAC_PT1411HWRU_NO_FAN.code2;
256  } else {
257  switch (this->fan_mode.value()) {
259  message[2] = RAC_PT1411HWRU_FAN_LOW.code1;
260  message[7] = RAC_PT1411HWRU_FAN_LOW.code2;
261  break;
262 
264  message[2] = RAC_PT1411HWRU_FAN_MED.code1;
265  message[7] = RAC_PT1411HWRU_FAN_MED.code2;
266  break;
267 
269  message[2] = RAC_PT1411HWRU_FAN_HIGH.code1;
270  message[7] = RAC_PT1411HWRU_FAN_HIGH.code2;
271  break;
272 
274  default:
275  message[2] = RAC_PT1411HWRU_FAN_AUTO.code1;
276  message[7] = RAC_PT1411HWRU_FAN_AUTO.code2;
277  }
278  }
279  // Byte 3u: ~Fan speed
280  // Byte 3l: 0000 (on) or 0100 (off)
281  message[3] = ~message[2];
282  // Byte 4u: Temp
283  if (this->model_ == MODEL_RAC_PT1411HWRU_F) {
284  temperature = (temperature * 1.8) + 32;
285  temp_adjd = temperature - TOSHIBA_RAC_PT1411HWRU_TEMP_F_MIN;
286  }
287 
288  index = static_cast<uint8_t>(roundf(temp_adjd));
289 
290  if (this->model_ == MODEL_RAC_PT1411HWRU_F) {
291  code = RAC_PT1411HWRU_TEMPERATURE_F[index];
292  message[9] |= RAC_PT1411HWRU_FLAG_FAH;
293  } else {
294  code = RAC_PT1411HWRU_TEMPERATURE_C[index];
295  }
296  if ((this->mode == climate::CLIMATE_MODE_FAN_ONLY) || (this->mode == climate::CLIMATE_MODE_OFF)) {
298  }
299 
300  if (code & RAC_PT1411HWRU_FLAG_FRAC) {
301  message[8] |= RAC_PT1411HWRU_FLAG_FRAC;
302  }
303  if (code & RAC_PT1411HWRU_FLAG_NEG) {
304  message[9] |= RAC_PT1411HWRU_FLAG_NEG;
305  }
306  message[4] = (code & RAC_PT1411HWRU_FLAG_MASK) << 4;
307  // Byte 4l: Mode
308  switch (this->mode) {
310  // zerooooo
311  break;
312 
314  message[4] |= RAC_PT1411HWRU_MODE_HEAT;
315  break;
316 
318  message[4] |= RAC_PT1411HWRU_MODE_COOL;
319  break;
320 
322  message[4] |= RAC_PT1411HWRU_MODE_DRY;
323  break;
324 
326  message[4] |= RAC_PT1411HWRU_MODE_FAN;
327  break;
328 
330  default:
331  message[4] |= RAC_PT1411HWRU_MODE_AUTO;
332  }
333 
334  // Byte 5u: ~Temp
335  // Byte 5l: ~Mode
336  message[5] = ~message[4];
337 
338  if (this->mode != climate::CLIMATE_MODE_OFF) {
339  // Byte 6: Header (0xD5)
340  message[6] = RAC_PT1411HWRU_MESSAGE_HEADER1;
341  // Byte 7: Fan speed part 2 (done above)
342  // Byte 8: 0x20 for °F frac, else 0 (done above)
343  // Byte 9: 0x10=NEG, 0x01=°F (done above)
344  // Byte 10: 0
345  // Byte 11: Checksum (bytes 6 through 10)
346  for (index = 6; index <= 10; index++) {
347  message[11] += message[index];
348  }
349  }
350  ESP_LOGV(TAG, "*** Generated codes: 0x%.2X%.2X%.2X%.2X%.2X%.2X 0x%.2X%.2X%.2X%.2X%.2X%.2X", message[0], message[1],
351  message[2], message[3], message[4], message[5], message[6], message[7], message[8], message[9], message[10],
352  message[11]);
353 
354  // load first block of IR code and repeat it once
355  encode_(data, &message[0], RAC_PT1411HWRU_MESSAGE_LENGTH, 1);
356  // load second block of IR code, if present
357  if (message[6] != 0) {
358  encode_(data, &message[6], RAC_PT1411HWRU_MESSAGE_LENGTH, 0);
359  }
360 
361  transmit.perform();
362 
363  // Swing Mode
364  data->reset();
365  data->space(TOSHIBA_PACKET_SPACE);
366  switch (this->swing_mode) {
368  encode_(data, &RAC_PT1411HWRU_SWING_VERTICAL[0], RAC_PT1411HWRU_MESSAGE_LENGTH, 1);
369  break;
370 
372  default:
373  encode_(data, &RAC_PT1411HWRU_SWING_OFF[0], RAC_PT1411HWRU_MESSAGE_LENGTH, 1);
374  }
375 
376  data->space(TOSHIBA_PACKET_SPACE);
377  transmit.perform();
378 
379  if (this->sensor_) {
380  transmit_rac_pt1411hwru_temp_(true, false);
381  }
382 }
383 
384 void ToshibaClimate::transmit_rac_pt1411hwru_temp_(const bool cs_state, const bool cs_send_update) {
385  if ((this->mode == climate::CLIMATE_MODE_HEAT) || (this->mode == climate::CLIMATE_MODE_COOL) ||
387  uint8_t message[RAC_PT1411HWRU_MESSAGE_LENGTH] = {0};
388  float temperature = clamp<float>(this->current_temperature, 0.0, TOSHIBA_RAC_PT1411HWRU_TEMP_C_MAX + 1);
389  auto transmit = this->transmitter_->transmit();
390  auto *data = transmit.get_data();
391  // "Comfort Sense" feature notes
392  // IR Code: 0xBA45 xxXX yyYY
393  // xx: Temperature in °C
394  // Bit 6: feature state (on/off)
395  // Bit 7: message contains temperature data for feature (bit 6 must also be set)
396  // XX: Bitwise complement of xx
397  // yy: Mode: Auto=0x7A, Cool=0x72, Heat=0x7E
398  // YY: Bitwise complement of yy
399  //
400  // Byte 0: Header upper (0xBA)
401  message[0] = RAC_PT1411HWRU_CS_HEADER;
402  // Byte 1: Header lower (0x45)
403  message[1] = ~message[0];
404  // Byte 2: Temperature in °C
405  message[2] = static_cast<uint8_t>(roundf(temperature));
406  if (cs_send_update) {
407  message[2] |= RAC_PT1411HWRU_CS_ENABLED | RAC_PT1411HWRU_CS_DATA;
408  } else if (cs_state) {
409  message[2] |= RAC_PT1411HWRU_CS_ENABLED;
410  }
411  // Byte 3: Bitwise complement of byte 2
412  message[3] = ~message[2];
413  // Byte 4: Footer upper
414  switch (this->mode) {
416  message[4] = RAC_PT1411HWRU_CS_FOOTER_HEAT;
417  break;
418 
420  message[4] = RAC_PT1411HWRU_CS_FOOTER_COOL;
421  break;
422 
424  message[4] = RAC_PT1411HWRU_CS_FOOTER_AUTO;
425 
426  default:
427  break;
428  }
429  // Byte 5: Footer lower/bitwise complement of byte 4
430  message[5] = ~message[4];
431 
432  ESP_LOGV(TAG, "*** Generated code: 0x%.2X%.2X%.2X%.2X%.2X%.2X", message[0], message[1], message[2], message[3],
433  message[4], message[5]);
434  // load IR code and repeat it once
435  encode_(data, message, RAC_PT1411HWRU_MESSAGE_LENGTH, 1);
436 
437  transmit.perform();
438  }
439 }
440 
441 uint8_t ToshibaClimate::is_valid_rac_pt1411hwru_header_(const uint8_t *message) {
442  const std::vector<uint8_t> header{RAC_PT1411HWRU_MESSAGE_HEADER0, RAC_PT1411HWRU_CS_HEADER,
443  RAC_PT1411HWRU_SWING_HEADER};
444 
445  for (auto i : header) {
446  if ((message[0] == i) && (message[1] == static_cast<uint8_t>(~i)))
447  return i;
448  }
449  if (message[0] == RAC_PT1411HWRU_MESSAGE_HEADER1)
451 
452  return 0;
453 }
454 
455 bool ToshibaClimate::compare_rac_pt1411hwru_packets_(const uint8_t *message1, const uint8_t *message2) {
456  for (uint8_t i = 0; i < RAC_PT1411HWRU_MESSAGE_LENGTH; i++) {
457  if (message1[i] != message2[i])
458  return false;
459  }
460  return true;
461 }
462 
464  uint8_t checksum = 0;
465 
466  switch (is_valid_rac_pt1411hwru_header_(message)) {
470  if (is_valid_rac_pt1411hwru_header_(message) && (message[2] == static_cast<uint8_t>(~message[3])) &&
471  (message[4] == static_cast<uint8_t>(~message[5]))) {
472  return true;
473  }
474  break;
475 
477  for (uint8_t i = 0; i < RAC_PT1411HWRU_MESSAGE_LENGTH - 1; i++) {
478  checksum += message[i];
479  }
480  if (checksum == message[RAC_PT1411HWRU_MESSAGE_LENGTH - 1]) {
481  return true;
482  }
483  break;
484 
485  default:
486  return false;
487  }
488 
489  return false;
490 }
491 
493  uint8_t message[18] = {0};
494  uint8_t message_length = TOSHIBA_HEADER_LENGTH, temperature_code = 0;
495 
496  // Validate header
497  if (!data.expect_item(TOSHIBA_HEADER_MARK, TOSHIBA_HEADER_SPACE)) {
498  return false;
499  }
500  // Read incoming bits into buffer
501  if (!decode_(&data, message, message_length)) {
502  return false;
503  }
504  // Determine incoming message protocol version and/or length
505  if (is_valid_rac_pt1411hwru_header_(message)) {
506  // We already received four bytes
507  message_length = RAC_PT1411HWRU_MESSAGE_LENGTH - 4;
508  } else if ((message[0] ^ message[1] ^ message[2]) != message[3]) {
509  // Return false if first checksum was not valid
510  return false;
511  } else {
512  // First checksum was valid so continue receiving the remaining bits
513  message_length = message[2] + 2;
514  }
515  // Decode the remaining bytes
516  if (!decode_(&data, &message[4], message_length)) {
517  return false;
518  }
519  // If this is a RAC-PT1411HWRU message, we expect the first packet a second time and also possibly a third packet
520  if (is_valid_rac_pt1411hwru_header_(message)) {
521  // There is always a space between packets
522  if (!data.expect_item(TOSHIBA_BIT_MARK, TOSHIBA_GAP_SPACE)) {
523  return false;
524  }
525  // Validate header 2
526  if (!data.expect_item(TOSHIBA_HEADER_MARK, TOSHIBA_HEADER_SPACE)) {
527  return false;
528  }
529  if (!decode_(&data, &message[6], RAC_PT1411HWRU_MESSAGE_LENGTH)) {
530  return false;
531  }
532  // If this is a RAC-PT1411HWRU message, there may also be a third packet.
533  // We do not fail the receive if we don't get this; it isn't always present
534  if (data.expect_item(TOSHIBA_BIT_MARK, TOSHIBA_GAP_SPACE)) {
535  // Validate header 3
536  data.expect_item(TOSHIBA_HEADER_MARK, TOSHIBA_HEADER_SPACE);
537  if (decode_(&data, &message[12], RAC_PT1411HWRU_MESSAGE_LENGTH)) {
538  if (!is_valid_rac_pt1411hwru_message_(&message[12])) {
539  // If a third packet was received but the checksum is not valid, fail
540  return false;
541  }
542  }
543  }
544  if (!compare_rac_pt1411hwru_packets_(&message[0], &message[6])) {
545  // If the first two packets don't match each other, fail
546  return false;
547  }
548  if (!is_valid_rac_pt1411hwru_message_(&message[0])) {
549  // If the first packet isn't valid, fail
550  return false;
551  }
552  }
553 
554  // Header has been verified, now determine protocol version and set the climate component properties
555  switch (is_valid_rac_pt1411hwru_header_(message)) {
556  // Power, temperature, mode, fan speed
558  // Get the mode
559  switch (message[4] & 0x0F) {
562  break;
563 
564  // case RAC_PT1411HWRU_MODE_OFF:
566  if (((message[4] >> 4) == RAC_PT1411HWRU_TEMPERATURE_FAN_ONLY) && (message[2] == RAC_PT1411HWRU_FAN_OFF)) {
568  } else {
570  }
571  break;
572 
573  // case RAC_PT1411HWRU_MODE_DRY:
575  if ((message[4] >> 4) == RAC_PT1411HWRU_TEMPERATURE_FAN_ONLY) {
577  } else {
579  }
580  break;
581 
584  break;
585 
586  default:
588  break;
589  }
590  // Get the fan speed/mode
591  switch (message[2]) {
592  case RAC_PT1411HWRU_FAN_LOW.code1:
594  break;
595 
596  case RAC_PT1411HWRU_FAN_MED.code1:
598  break;
599 
600  case RAC_PT1411HWRU_FAN_HIGH.code1:
602  break;
603 
604  case RAC_PT1411HWRU_FAN_AUTO.code1:
605  default:
607  break;
608  }
609  // Get the target temperature
610  if (is_valid_rac_pt1411hwru_message_(&message[12])) {
611  temperature_code =
612  (message[4] >> 4) | (message[14] & RAC_PT1411HWRU_FLAG_FRAC) | (message[15] & RAC_PT1411HWRU_FLAG_NEG);
613  if (message[15] & RAC_PT1411HWRU_FLAG_FAH) {
614  for (size_t i = 0; i < RAC_PT1411HWRU_TEMPERATURE_F.size(); i++) {
615  if (RAC_PT1411HWRU_TEMPERATURE_F[i] == temperature_code) {
616  this->target_temperature = static_cast<float>((i + TOSHIBA_RAC_PT1411HWRU_TEMP_F_MIN - 32) * 5) / 9;
617  }
618  }
619  } else {
620  for (size_t i = 0; i < RAC_PT1411HWRU_TEMPERATURE_C.size(); i++) {
621  if (RAC_PT1411HWRU_TEMPERATURE_C[i] == temperature_code) {
623  }
624  }
625  }
626  }
627  break;
628  // "Comfort Sense" temperature packet
630  // "Comfort Sense" feature notes
631  // IR Code: 0xBA45 xxXX yyYY
632  // xx: Temperature in °C
633  // Bit 6: feature state (on/off)
634  // Bit 7: message contains temperature data for feature (bit 6 must also be set)
635  // XX: Bitwise complement of xx
636  // yy: Mode: Auto: 7A
637  // Cool: 72
638  // Heat: 7E
639  // YY: Bitwise complement of yy
640  if ((message[2] & RAC_PT1411HWRU_CS_ENABLED) && (message[2] & RAC_PT1411HWRU_CS_DATA)) {
641  // Setting current_temperature this way allows the unit's remote to provide the temperature to HA
642  this->current_temperature = message[2] & ~(RAC_PT1411HWRU_CS_ENABLED | RAC_PT1411HWRU_CS_DATA);
643  }
644  break;
645  // Swing mode
647  if (message[4] == RAC_PT1411HWRU_SWING_VERTICAL[4]) {
649  } else {
651  }
652  break;
653  // Generic (old) Toshiba packet
654  default:
655  uint8_t checksum = 0;
656  // Add back the length of the header (we pruned it above)
657  message_length += TOSHIBA_HEADER_LENGTH;
658  // Validate the second checksum before trusting any more of the message
659  for (uint8_t i = TOSHIBA_HEADER_LENGTH; i < message_length - 1; i++) {
660  checksum ^= message[i];
661  }
662  // Did our computed checksum and the provided checksum match?
663  if (checksum != message[message_length - 1]) {
664  return false;
665  }
666  // Check if this is a short swing/fix message
667  if (message[4] & TOSHIBA_COMMAND_MOTION) {
668  // Not supported yet
669  return false;
670  }
671 
672  // Get the mode
673  switch (message[6] & 0x0F) {
674  case TOSHIBA_MODE_OFF:
676  break;
677 
678  case TOSHIBA_MODE_COOL:
680  break;
681 
682  case TOSHIBA_MODE_DRY:
684  break;
685 
688  break;
689 
690  case TOSHIBA_MODE_HEAT:
692  break;
693 
694  case TOSHIBA_MODE_AUTO:
695  default:
697  }
698 
699  // Get the fan mode
700  switch (message[6] & 0xF0) {
703  break;
704 
705  case TOSHIBA_FAN_SPEED_1:
707  break;
708 
709  case TOSHIBA_FAN_SPEED_3:
711  break;
712 
713  case TOSHIBA_FAN_SPEED_5:
715  break;
716 
718  default:
720  break;
721  }
722 
723  // Get the target temperature
724  this->target_temperature = (message[5] >> 4) + TOSHIBA_GENERIC_TEMP_C_MIN;
725  }
726 
727  this->publish_state();
728  return true;
729 }
730 
731 void ToshibaClimate::encode_(remote_base::RemoteTransmitData *data, const uint8_t *message, const uint8_t nbytes,
732  const uint8_t repeat) {
733  data->set_carrier_frequency(TOSHIBA_CARRIER_FREQUENCY);
734 
735  for (uint8_t copy = 0; copy <= repeat; copy++) {
736  data->item(TOSHIBA_HEADER_MARK, TOSHIBA_HEADER_SPACE);
737 
738  for (uint8_t byte = 0; byte < nbytes; byte++) {
739  for (uint8_t bit = 0; bit < 8; bit++) {
740  data->mark(TOSHIBA_BIT_MARK);
741  if (message[byte] & (1 << (7 - bit))) {
742  data->space(TOSHIBA_ONE_SPACE);
743  } else {
744  data->space(TOSHIBA_ZERO_SPACE);
745  }
746  }
747  }
748  data->item(TOSHIBA_BIT_MARK, TOSHIBA_GAP_SPACE);
749  }
750 }
751 
752 bool ToshibaClimate::decode_(remote_base::RemoteReceiveData *data, uint8_t *message, const uint8_t nbytes) {
753  for (uint8_t byte = 0; byte < nbytes; byte++) {
754  for (uint8_t bit = 0; bit < 8; bit++) {
755  if (data->expect_item(TOSHIBA_BIT_MARK, TOSHIBA_ONE_SPACE)) {
756  message[byte] |= 1 << (7 - bit);
757  } else if (data->expect_item(TOSHIBA_BIT_MARK, TOSHIBA_ZERO_SPACE)) {
758  message[byte] &= static_cast<uint8_t>(~(1 << (7 - bit)));
759  } else {
760  return false;
761  }
762  }
763  }
764  return true;
765 }
766 
767 } // namespace toshiba
768 } // namespace esphome
The fan mode is set to Low.
Definition: climate_mode.h:54
const uint8_t RAC_PT1411HWRU_MESSAGE_LENGTH
Definition: toshiba.cpp:60
The fan mode is set to Quiet.
Definition: climate_mode.h:66
const float TOSHIBA_RAC_PT1411HWRU_TEMP_C_MAX
Definition: toshiba.h:19
const uint8_t TOSHIBA_HEADER_LENGTH
Definition: toshiba.cpp:23
const std::vector< uint8_t > RAC_PT1411HWRU_TEMPERATURE_F
Definition: toshiba.cpp:96
const uint16_t TOSHIBA_HEADER_MARK
Definition: toshiba.cpp:15
const uint8_t RAC_PT1411HWRU_TEMPERATURE_FAN_ONLY
Definition: toshiba.cpp:88
bool decode_(remote_base::RemoteReceiveData *data, uint8_t *message, uint8_t nbytes)
Definition: toshiba.cpp:752
void transmit_state() override
Definition: toshiba.cpp:133
const std::vector< uint8_t > RAC_PT1411HWRU_TEMPERATURE_C
Definition: toshiba.cpp:93
const uint8_t TOSHIBA_FAN_SPEED_QUIET
Definition: toshiba.cpp:38
void set_carrier_frequency(uint32_t carrier_frequency)
Definition: remote_base.h:34
uint8_t checksum
Definition: bl0906.h:210
const uint8_t TOSHIBA_COMMAND_POWER
Definition: toshiba.cpp:27
const uint8_t TOSHIBA_FAN_SPEED_3
Definition: toshiba.cpp:41
void item(uint32_t mark, uint32_t space)
Definition: remote_base.h:29
The climate device is set to heat to reach the target temperature.
Definition: climate_mode.h:18
const uint8_t TOSHIBA_FAN_SPEED_2
Definition: toshiba.cpp:40
void transmit_rac_pt1411hwru_temp_(bool cs_state=true, bool cs_send_update=true)
Definition: toshiba.cpp:384
const uint16_t TOSHIBA_ONE_SPACE
Definition: toshiba.cpp:21
const uint8_t RAC_PT1411HWRU_SWING_HEADER
Definition: toshiba.cpp:69
const uint8_t TOSHIBA_COMMAND_DEFAULT
Definition: toshiba.cpp:25
const uint8_t TOSHIBA_COMMAND_MOTION
Definition: toshiba.cpp:28
const uint16_t TOSHIBA_CARRIER_FREQUENCY
Definition: toshiba.cpp:22
const RacPt1411hwruFanSpeed RAC_PT1411HWRU_NO_FAN
Definition: toshiba.cpp:79
const uint8_t RAC_PT1411HWRU_MODE_AUTO
Definition: toshiba.cpp:81
The climate device is set to dry/humidity mode.
Definition: climate_mode.h:22
constexpr RacPt1411hwruFanSpeed RAC_PT1411HWRU_FAN_LOW
Definition: toshiba.cpp:75
const uint8_t RAC_PT1411HWRU_MODE_OFF
Definition: toshiba.cpp:86
const uint8_t TOSHIBA_FAN_SPEED_5
Definition: toshiba.cpp:43
const uint8_t TOSHIBA_MODE_DRY
Definition: toshiba.cpp:32
const uint8_t TOSHIBA_MODE_FAN_ONLY
Definition: toshiba.cpp:34
ClimateSwingMode swing_mode
Definition: climate.h:581
const uint8_t RAC_PT1411HWRU_FAN_OFF
Definition: toshiba.cpp:73
const uint8_t TOSHIBA_MOTION_FIX
Definition: toshiba.cpp:49
bool on_receive(remote_base::RemoteReceiveData data) override
Definition: toshiba.cpp:492
const uint8_t RAC_PT1411HWRU_FLAG_FAH
Definition: toshiba.cpp:52
const uint8_t TOSHIBA_MODE_OFF
Definition: toshiba.cpp:35
const uint16_t TOSHIBA_HEADER_SPACE
Definition: toshiba.cpp:16
const float TOSHIBA_RAC_PT1411HWRU_TEMP_F_MIN
Definition: toshiba.h:20
const uint16_t TOSHIBA_BIT_MARK
Definition: toshiba.cpp:19
const uint8_t RAC_PT1411HWRU_FLAG_NEG
Definition: toshiba.cpp:54
const uint8_t RAC_PT1411HWRU_FLAG_FRAC
Definition: toshiba.cpp:53
The climate device is set to cool to reach the target temperature.
Definition: climate_mode.h:16
const uint8_t RAC_PT1411HWRU_CS_DATA
Definition: toshiba.cpp:63
const uint8_t RAC_PT1411HWRU_MODE_COOL
Definition: toshiba.cpp:82
The fan mode is set to Auto.
Definition: climate_mode.h:52
const uint8_t TOSHIBA_MODE_COOL
Definition: toshiba.cpp:31
const uint8_t RAC_PT1411HWRU_MODE_HEAT
Definition: toshiba.cpp:85
const uint16_t TOSHIBA_ZERO_SPACE
Definition: toshiba.cpp:20
constexpr RacPt1411hwruFanSpeed RAC_PT1411HWRU_FAN_AUTO
Definition: toshiba.cpp:74
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:183
const uint8_t RAC_PT1411HWRU_FLAG_MASK
Definition: toshiba.cpp:56
const std::vector< uint8_t > RAC_PT1411HWRU_SWING_OFF
Definition: toshiba.cpp:71
const uint8_t RAC_PT1411HWRU_CS_FOOTER_HEAT
Definition: toshiba.cpp:67
const uint16_t TOSHIBA_PACKET_SPACE
Definition: toshiba.cpp:18
uint16_t temperature
Definition: sun_gtil2.cpp:26
const uint8_t RAC_PT1411HWRU_MESSAGE_HEADER0
Definition: toshiba.cpp:58
The climate device is set to heat/cool to reach the target temperature.
Definition: climate_mode.h:14
bool is_valid_rac_pt1411hwru_message_(const uint8_t *message)
Definition: toshiba.cpp:463
The fan mode is set to Vertical.
Definition: climate_mode.h:76
void encode_(remote_base::RemoteTransmitData *data, const uint8_t *message, uint8_t nbytes, uint8_t repeat)
Definition: toshiba.cpp:731
The fan mode is set to High.
Definition: climate_mode.h:58
The swing mode is set to Off.
Definition: climate_mode.h:72
The climate device is off.
Definition: climate_mode.h:12
ClimateFanMode fan_mode
Definition: climate.h:573
const uint8_t RAC_PT1411HWRU_CS_HEADER
Definition: toshiba.cpp:64
const uint8_t TOSHIBA_MODE_AUTO
Definition: toshiba.cpp:30
const uint8_t TOSHIBA_FAN_SPEED_AUTO
Definition: toshiba.cpp:37
const uint8_t TOSHIBA_MODE_HEAT
Definition: toshiba.cpp:33
constexpr RacPt1411hwruFanSpeed RAC_PT1411HWRU_FAN_HIGH
Definition: toshiba.cpp:77
const uint8_t RAC_PT1411HWRU_CS_ENABLED
Definition: toshiba.cpp:62
const uint8_t TOSHIBA_COMMAND_TIMER
Definition: toshiba.cpp:26
const float TOSHIBA_RAC_PT1411HWRU_TEMP_C_MIN
Definition: toshiba.h:18
const uint8_t RAC_PT1411HWRU_MESSAGE_HEADER1
Definition: toshiba.cpp:59
const uint8_t RAC_PT1411HWRU_MODE_DRY
Definition: toshiba.cpp:83
constexpr RacPt1411hwruFanSpeed RAC_PT1411HWRU_FAN_MED
Definition: toshiba.cpp:76
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
const uint16_t TOSHIBA_GAP_SPACE
Definition: toshiba.cpp:17
const uint8_t TOSHIBA_FAN_SPEED_1
Definition: toshiba.cpp:39
const uint8_t TOSHIBA_FAN_SPEED_4
Definition: toshiba.cpp:42
const uint8_t RAC_PT1411HWRU_MODE_FAN
Definition: toshiba.cpp:84
The fan mode is set to Medium.
Definition: climate_mode.h:56
const uint8_t RAC_PT1411HWRU_CS_FOOTER_COOL
Definition: toshiba.cpp:66
const uint8_t TOSHIBA_POWER_ECO
Definition: toshiba.cpp:46
bool expect_item(uint32_t mark, uint32_t space)
Definition: remote_base.cpp:74
const std::vector< uint8_t > RAC_PT1411HWRU_SWING_VERTICAL
Definition: toshiba.cpp:70
The climate device only has the fan enabled, no heating or cooling is taking place.
Definition: climate_mode.h:20
const uint8_t RAC_PT1411HWRU_CS_FOOTER_AUTO
Definition: toshiba.cpp:65
const float TOSHIBA_GENERIC_TEMP_C_MAX
Definition: toshiba.h:17
const float TOSHIBA_GENERIC_TEMP_C_MIN
Definition: toshiba.h:16
float target_temperature
Definition: climate.h:138
const uint8_t TOSHIBA_MOTION_SWING
Definition: toshiba.cpp:48
const uint8_t TOSHIBA_POWER_HIGH
Definition: toshiba.cpp:45
bool compare_rac_pt1411hwru_packets_(const uint8_t *message1, const uint8_t *message2)
Definition: toshiba.cpp:455
uint8_t is_valid_rac_pt1411hwru_header_(const uint8_t *message)
Definition: toshiba.cpp:441
bool state
Definition: fan.h:34