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