ESPHome  2024.4.2
delonghi.cpp
Go to the documentation of this file.
1 #include "delonghi.h"
3 
4 namespace esphome {
5 namespace delonghi {
6 
7 static const char *const TAG = "delonghi.climate";
8 
10  uint8_t remote_state[DELONGHI_STATE_FRAME_SIZE] = {0};
11  remote_state[0] = DELONGHI_ADDRESS;
12  remote_state[1] = this->temperature_();
13  remote_state[1] |= (this->fan_speed_()) << 5;
14  remote_state[2] = this->operation_mode_();
15  // Calculate checksum
16  for (int i = 0; i < DELONGHI_STATE_FRAME_SIZE - 1; i++) {
17  remote_state[DELONGHI_STATE_FRAME_SIZE - 1] += remote_state[i];
18  }
19 
20  auto transmit = this->transmitter_->transmit();
21  auto *data = transmit.get_data();
23 
24  data->mark(DELONGHI_HEADER_MARK);
25  data->space(DELONGHI_HEADER_SPACE);
26  for (unsigned char b : remote_state) {
27  for (uint8_t mask = 1; mask > 0; mask <<= 1) { // iterate through bit mask
28  data->mark(DELONGHI_BIT_MARK);
29  bool bit = b & mask;
30  data->space(bit ? DELONGHI_ONE_SPACE : DELONGHI_ZERO_SPACE);
31  }
32  }
33  data->mark(DELONGHI_BIT_MARK);
34  data->space(0);
35 
36  transmit.perform();
37 }
38 
40  uint8_t operating_mode = DELONGHI_MODE_ON;
41  switch (this->mode) {
43  operating_mode |= DELONGHI_MODE_COOL;
44  break;
46  operating_mode |= DELONGHI_MODE_DRY;
47  break;
49  operating_mode |= DELONGHI_MODE_HEAT;
50  break;
52  operating_mode |= DELONGHI_MODE_AUTO;
53  break;
55  operating_mode |= DELONGHI_MODE_FAN;
56  break;
58  default:
59  operating_mode = DELONGHI_MODE_OFF;
60  break;
61  }
62  return operating_mode;
63 }
64 
66  uint16_t fan_speed;
67  switch (this->fan_mode.value()) {
69  fan_speed = DELONGHI_FAN_LOW;
70  break;
72  fan_speed = DELONGHI_FAN_MEDIUM;
73  break;
75  fan_speed = DELONGHI_FAN_HIGH;
76  break;
78  default:
79  fan_speed = DELONGHI_FAN_AUTO;
80  }
81  return fan_speed;
82 }
83 
85  // Force special temperatures depending on the mode
86  uint8_t temperature = 0b0001;
87  switch (this->mode) {
89  temperature = (uint8_t) roundf(this->target_temperature) - DELONGHI_TEMP_OFFSET_HEAT;
90  break;
96  default:
97  temperature = (uint8_t) roundf(this->target_temperature) - DELONGHI_TEMP_OFFSET_COOL;
98  }
99  if (temperature > 0x0F) {
100  temperature = 0x0F; // clamp maximum
101  }
102  return temperature;
103 }
104 
105 bool DelonghiClimate::parse_state_frame_(const uint8_t frame[]) {
106  uint8_t checksum = 0;
107  for (int i = 0; i < (DELONGHI_STATE_FRAME_SIZE - 1); i++) {
108  checksum += frame[i];
109  }
110  if (frame[DELONGHI_STATE_FRAME_SIZE - 1] != checksum) {
111  return false;
112  }
113  uint8_t mode = frame[2] & 0x0F;
114  if (mode & DELONGHI_MODE_ON) {
115  switch (mode & 0x0E) {
116  case DELONGHI_MODE_COOL:
117  this->mode = climate::CLIMATE_MODE_COOL;
118  break;
119  case DELONGHI_MODE_DRY:
120  this->mode = climate::CLIMATE_MODE_DRY;
121  break;
122  case DELONGHI_MODE_HEAT:
123  this->mode = climate::CLIMATE_MODE_HEAT;
124  break;
125  case DELONGHI_MODE_AUTO:
126  this->mode = climate::CLIMATE_MODE_HEAT_COOL;
127  break;
128  case DELONGHI_MODE_FAN:
129  this->mode = climate::CLIMATE_MODE_FAN_ONLY;
130  break;
131  }
132  } else {
133  this->mode = climate::CLIMATE_MODE_OFF;
134  }
135  uint8_t temperature = frame[1] & 0x0F;
136  if (this->mode == climate::CLIMATE_MODE_HEAT) {
137  this->target_temperature = temperature + DELONGHI_TEMP_OFFSET_HEAT;
138  } else {
139  this->target_temperature = temperature + DELONGHI_TEMP_OFFSET_COOL;
140  }
141  uint8_t fan_mode = frame[1] >> 5;
142  switch (fan_mode) {
143  case DELONGHI_FAN_LOW:
144  this->fan_mode = climate::CLIMATE_FAN_LOW;
145  break;
146  case DELONGHI_FAN_MEDIUM:
147  this->fan_mode = climate::CLIMATE_FAN_MEDIUM;
148  break;
149  case DELONGHI_FAN_HIGH:
150  this->fan_mode = climate::CLIMATE_FAN_HIGH;
151  break;
152  case DELONGHI_FAN_AUTO:
153  this->fan_mode = climate::CLIMATE_FAN_AUTO;
154  break;
155  }
156  this->publish_state();
157  return true;
158 }
159 
161  uint8_t state_frame[DELONGHI_STATE_FRAME_SIZE] = {};
163  return false;
164  }
165  for (uint8_t pos = 0; pos < DELONGHI_STATE_FRAME_SIZE; pos++) {
166  uint8_t byte = 0;
167  for (int8_t bit = 0; bit < 8; bit++) {
169  byte |= 1 << bit;
171  return false;
172  }
173  }
174  state_frame[pos] = byte;
175  if (pos == 0) {
176  // frame header
177  if (byte != DELONGHI_ADDRESS) {
178  return false;
179  }
180  }
181  }
182  return this->parse_state_frame_(state_frame);
183 }
184 
185 } // namespace delonghi
186 } // namespace esphome
The fan mode is set to Low.
Definition: climate_mode.h:54
value_type const & value() const
Definition: optional.h:89
float target_temperature
The target temperature of the climate device.
Definition: climate.h:186
void set_carrier_frequency(uint32_t carrier_frequency)
Definition: remote_base.h:29
const uint32_t DELONGHI_IR_FREQUENCY
Definition: delonghi.h:33
The climate device is set to heat to reach the target temperature.
Definition: climate_mode.h:18
bool parse_state_frame_(const uint8_t frame[])
Definition: delonghi.cpp:105
ClimateMode mode
The active mode of the climate device.
Definition: climate.h:173
const uint32_t DELONGHI_ONE_SPACE
Definition: delonghi.h:37
The climate device is set to dry/humidity mode.
Definition: climate_mode.h:22
const uint8_t DELONGHI_ADDRESS
Definition: delonghi.h:9
const uint8_t DELONGHI_FAN_MEDIUM
Definition: delonghi.h:29
const uint32_t DELONGHI_BIT_MARK
Definition: delonghi.h:36
const uint8_t DELONGHI_TEMP_OFFSET_HEAT
Definition: delonghi.h:15
const uint32_t DELONGHI_HEADER_MARK
Definition: delonghi.h:34
The climate device is set to cool to reach the target temperature.
Definition: climate_mode.h:16
The fan mode is set to Auto.
Definition: climate_mode.h:52
const uint8_t DELONGHI_FAN_LOW
Definition: delonghi.h:30
const uint8_t DELONGHI_FAN_HIGH
Definition: delonghi.h:28
const uint8_t DELONGHI_TEMP_OFFSET_COOL
Definition: delonghi.h:14
RemoteTransmitterBase * transmitter_
Definition: remote_base.h:245
const uint8_t DELONGHI_MODE_DRY
Definition: delonghi.h:21
const uint8_t DELONGHI_MODE_OFF
Definition: delonghi.h:23
const uint32_t DELONGHI_ZERO_SPACE
Definition: delonghi.h:38
const uint32_t DELONGHI_HEADER_SPACE
Definition: delonghi.h:35
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
uint8_t checksum
Definition: bl0939.h:35
bool on_receive(remote_base::RemoteReceiveData data) override
Definition: delonghi.cpp:160
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition: climate.cpp:395
const uint8_t DELONGHI_MODE_ON
Definition: delonghi.h:24
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:199
const uint8_t DELONGHI_MODE_HEAT
Definition: delonghi.h:20
const uint8_t DELONGHI_MODE_COOL
Definition: delonghi.h:19
const uint8_t DELONGHI_FAN_AUTO
Definition: delonghi.h:27
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
const uint8_t DELONGHI_MODE_AUTO
Definition: delonghi.h:18
The fan mode is set to Medium.
Definition: climate_mode.h:56
const uint8_t DELONGHI_MODE_FAN
Definition: delonghi.h:22
const uint8_t DELONGHI_STATE_FRAME_SIZE
Definition: delonghi.h:41
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