ESPHome  2021.11.3
yashima.cpp
Go to the documentation of this file.
1 #include "yashima.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace yashima {
6 
7 static const char *const TAG = "yashima.climate";
8 
9 const uint16_t YASHIMA_STATE_LENGTH = 9;
10 const uint16_t YASHIMA_BITS = YASHIMA_STATE_LENGTH * 8;
11 
12 /* the bit masks are intended to be sent from the MSB to the LSB */
13 const uint8_t YASHIMA_MODE_HEAT_BYTE0 = 0b00100000;
14 const uint8_t YASHIMA_MODE_DRY_BYTE0 = 0b01100000;
15 const uint8_t YASHIMA_MODE_COOL_BYTE0 = 0b11100000;
16 const uint8_t YASHIMA_MODE_FAN_BYTE0 = 0b10100000;
17 const uint8_t YASHIMA_MODE_AUTO_BYTE0 = 0b11100000;
18 const uint8_t YASHIMA_MODE_OFF_BYTE0 = 0b11110000;
19 const uint8_t YASHIMA_BASE_BYTE0 = 0b1110;
20 
21 const uint8_t YASHIMA_TEMP_MAX = 30; // Celsius
22 const uint8_t YASHIMA_TEMP_MIN = 16; // Celsius
23 const uint8_t YASHIMA_TEMP_RANGE = YASHIMA_TEMP_MAX - YASHIMA_TEMP_MIN + 1;
24 
26  0b01100100, // 16C
27  0b10100100, // 17C
28  0b00100100, // 18C
29  0b11000100, // 19C
30  0b01000100, // 20C
31  0b10000100, // 21C
32  0b00000100, // 22C
33  0b11111000, // 23C
34  0b01111000, // 24C
35  0b10111000, // 25C
36  0b00111000, // 26C
37  0b11011000, // 27C
38  0b01011000, // 28C
39  0b10011000, // 29C
40  0b00011000, // 30C
41 };
42 const uint8_t YASHIMA_BASE_BYTE1 = 0b11;
43 
44 const uint8_t YASHIMA_FAN_AUTO_BYTE2 = 0b11000000;
45 const uint8_t YASHIMA_FAN_LOW_BYTE2 = 0b00000000;
46 const uint8_t YASHIMA_FAN_MEDIUM_BYTE2 = 0b10000000;
47 const uint8_t YASHIMA_FAN_HIGH_BYTE2 = 0b01000000;
48 const uint8_t YASHIMA_BASE_BYTE2 = 0b111111;
49 
50 const uint8_t YASHIMA_BASE_BYTE3 = 0b11111111;
51 const uint8_t YASHIMA_BASE_BYTE4 = 0b11;
52 
53 const uint8_t YASHIMA_MODE_HEAT_BYTE5 = 0b00000000;
54 const uint8_t YASHIMA_MODE_DRY_BYTE5 = 0b00000000;
55 const uint8_t YASHIMA_MODE_FAN_BYTE5 = 0b00000000;
56 const uint8_t YASHIMA_MODE_AUTO_BYTE5 = 0b00000000;
57 const uint8_t YASHIMA_MODE_COOL_BYTE5 = 0b10000000;
58 const uint8_t YASHIMA_MODE_OFF_BYTE5 = 0b10000000;
59 const uint8_t YASHIMA_BASE_BYTE5 = 0b11111;
60 
61 const uint8_t YASHIMA_BASE_BYTE6 = 0b11111111;
62 const uint8_t YASHIMA_BASE_BYTE7 = 0b11111111;
63 const uint8_t YASHIMA_BASE_BYTE8 = 0b11001111;
64 
65 /* values sampled using a Broadlink Mini 3: */
66 // const uint16_t YASHIMA_HEADER_MARK = 9600;
67 // const uint16_t YASHIMA_HEADER_SPACE = 4800;
68 // const uint16_t YASHIMA_BIT_MARK = 720;
69 // const uint16_t YASHIMA_ONE_SPACE = 550;
70 // const uint16_t YASHIMA_ZERO_SPACE = 1640;
71 
72 /* scaled values to get correct timing on ESP8266/ESP32: */
73 const uint16_t YASHIMA_HEADER_MARK = 9035;
74 const uint16_t YASHIMA_HEADER_SPACE = 4517;
75 const uint16_t YASHIMA_BIT_MARK = 667;
76 const uint16_t YASHIMA_ONE_SPACE = 517;
77 const uint16_t YASHIMA_ZERO_SPACE = 1543;
79 
80 const uint32_t YASHIMA_CARRIER_FREQUENCY = 38000;
81 
85 
87  if (supports_cool_)
89  if (supports_heat_)
91 
93  traits.set_visual_min_temperature(YASHIMA_TEMP_MIN);
94  traits.set_visual_max_temperature(YASHIMA_TEMP_MAX);
96  return traits;
97 }
98 
100  if (this->sensor_) {
101  this->sensor_->add_on_state_callback([this](float state) {
102  this->current_temperature = state;
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  this->target_temperature = 24;
117  }
118 }
119 
121  if (call.get_mode().has_value())
122  this->mode = *call.get_mode();
123  if (call.get_target_temperature().has_value())
125 
126  this->transmit_state_();
127  this->publish_state();
128 }
129 
131  uint8_t remote_state[YASHIMA_STATE_LENGTH] = {0};
132 
133  remote_state[0] = YASHIMA_BASE_BYTE0;
134  remote_state[1] = YASHIMA_BASE_BYTE1;
135  remote_state[2] = YASHIMA_BASE_BYTE2;
136  remote_state[3] = YASHIMA_BASE_BYTE3;
137  remote_state[4] = YASHIMA_BASE_BYTE4;
138  remote_state[5] = YASHIMA_BASE_BYTE5;
139  remote_state[6] = YASHIMA_BASE_BYTE6;
140  remote_state[7] = YASHIMA_BASE_BYTE7;
141  remote_state[8] = YASHIMA_BASE_BYTE8;
142 
143  // Set mode
144  switch (this->mode) {
146  remote_state[0] |= YASHIMA_MODE_AUTO_BYTE0;
147  remote_state[5] |= YASHIMA_MODE_AUTO_BYTE5;
148  break;
150  remote_state[0] |= YASHIMA_MODE_COOL_BYTE0;
151  remote_state[5] |= YASHIMA_MODE_COOL_BYTE5;
152  break;
154  remote_state[0] |= YASHIMA_MODE_HEAT_BYTE0;
155  remote_state[5] |= YASHIMA_MODE_HEAT_BYTE5;
156  break;
158  default:
159  remote_state[0] |= YASHIMA_MODE_OFF_BYTE0;
160  remote_state[5] |= YASHIMA_MODE_OFF_BYTE5;
161  break;
162  // TODO: CLIMATE_MODE_FAN_ONLY, CLIMATE_MODE_DRY are missing in esphome
163  }
164 
165  // TODO: missing support for fan speed
166  remote_state[2] |= YASHIMA_FAN_AUTO_BYTE2;
167 
168  // Set temperature
169  uint8_t safecelsius = std::max((uint8_t) this->target_temperature, YASHIMA_TEMP_MIN);
170  safecelsius = std::min(safecelsius, YASHIMA_TEMP_MAX);
171  remote_state[1] |= YASHIMA_TEMP_MAP_BYTE1[safecelsius - YASHIMA_TEMP_MIN];
172 
173  auto transmit = this->transmitter_->transmit();
174  auto data = transmit.get_data();
175 
176  data->set_carrier_frequency(YASHIMA_CARRIER_FREQUENCY);
177 
178  // Header
179  data->mark(YASHIMA_HEADER_MARK);
180  data->space(YASHIMA_HEADER_SPACE);
181  // Data (sent from the MSB to the LSB)
182  for (uint8_t i : remote_state)
183  for (int8_t j = 7; j >= 0; j--) {
184  data->mark(YASHIMA_BIT_MARK);
185  bool bit = i & (1 << j);
186  data->space(bit ? YASHIMA_ONE_SPACE : YASHIMA_ZERO_SPACE);
187  }
188  // Footer
189  data->mark(YASHIMA_BIT_MARK);
190  data->space(YASHIMA_GAP);
191 
192  transmit.perform();
193 }
194 
195 } // namespace yashima
196 } // namespace esphome
const uint16_t YASHIMA_BIT_MARK
Definition: yashima.cpp:75
This class is used to encode all control actions on a climate device.
Definition: climate.h:33
const uint8_t YASHIMA_MODE_HEAT_BYTE0
Definition: yashima.cpp:13
void add_on_state_callback(std::function< void(float)> &&callback)
Add a callback that will be called every time a filtered value arrives.
Definition: sensor.cpp:71
void set_visual_temperature_step(float temperature_step)
float target_temperature
The target temperature of the climate device.
Definition: climate.h:183
const uint8_t YASHIMA_MODE_DRY_BYTE5
Definition: yashima.cpp:54
void set_carrier_frequency(uint32_t carrier_frequency)
Definition: remote_base.h:30
const optional< ClimateMode > & get_mode() const
Definition: climate.cpp:259
This class contains all static data for climate devices.
const uint8_t YASHIMA_BASE_BYTE1
Definition: yashima.cpp:42
void set_visual_min_temperature(float visual_min_temperature)
The climate device is set to heat to reach the target temperature.
Definition: climate_mode.h:18
const uint16_t YASHIMA_ONE_SPACE
Definition: yashima.cpp:76
const uint8_t YASHIMA_TEMP_MAP_BYTE1[YASHIMA_TEMP_RANGE]
Definition: yashima.cpp:25
ClimateMode mode
The active mode of the climate device.
Definition: climate.h:175
const uint8_t YASHIMA_TEMP_RANGE
Definition: yashima.cpp:23
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition: climate.h:179
bool has_value() const
Definition: optional.h:87
sensor::Sensor * sensor_
Definition: yashima.h:36
void transmit_state_()
Transmit via IR the state of this climate controller.
Definition: yashima.cpp:130
const uint16_t YASHIMA_HEADER_MARK
Definition: yashima.cpp:73
const uint8_t YASHIMA_BASE_BYTE8
Definition: yashima.cpp:63
const uint16_t YASHIMA_STATE_LENGTH
Definition: yashima.cpp:9
const uint8_t YASHIMA_FAN_LOW_BYTE2
Definition: yashima.cpp:45
The climate device is set to cool to reach the target temperature.
Definition: climate_mode.h:16
float state
This member variable stores the last state that has passed through all filters.
Definition: sensor.h:132
const uint16_t YASHIMA_ZERO_SPACE
Definition: yashima.cpp:77
const uint8_t YASHIMA_FAN_HIGH_BYTE2
Definition: yashima.cpp:47
void set_supported_modes(std::set< ClimateMode > modes)
const uint8_t YASHIMA_MODE_OFF_BYTE5
Definition: yashima.cpp:58
const uint8_t YASHIMA_FAN_AUTO_BYTE2
Definition: yashima.cpp:44
void set_visual_max_temperature(float visual_max_temperature)
const uint8_t YASHIMA_MODE_AUTO_BYTE0
Definition: yashima.cpp:17
The climate device is set to heat/cool to reach the target temperature.
Definition: climate_mode.h:14
const uint8_t YASHIMA_BASE_BYTE0
Definition: yashima.cpp:19
const optional< float > & get_target_temperature() const
Definition: climate.cpp:260
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition: climate.cpp:380
const uint8_t YASHIMA_MODE_OFF_BYTE0
Definition: yashima.cpp:18
The climate device is off.
Definition: climate_mode.h:12
const uint8_t YASHIMA_BASE_BYTE6
Definition: yashima.cpp:61
const uint8_t YASHIMA_FAN_MEDIUM_BYTE2
Definition: yashima.cpp:46
const uint8_t YASHIMA_BASE_BYTE2
Definition: yashima.cpp:48
const uint8_t YASHIMA_TEMP_MAX
Definition: yashima.cpp:21
const uint8_t YASHIMA_BASE_BYTE7
Definition: yashima.cpp:62
const uint8_t YASHIMA_MODE_AUTO_BYTE5
Definition: yashima.cpp:56
const uint8_t YASHIMA_MODE_FAN_BYTE0
Definition: yashima.cpp:16
const uint8_t YASHIMA_MODE_COOL_BYTE0
Definition: yashima.cpp:15
Library based on https://github.com/miguelbalboa/rfid and adapted to ESPHome by . ...
Definition: a4988.cpp:4
const uint8_t YASHIMA_BASE_BYTE4
Definition: yashima.cpp:51
void set_supports_two_point_target_temperature(bool supports_two_point_target_temperature)
optional< ClimateDeviceRestoreState > restore_state_()
Restore the state of the climate device, call this from your setup() method.
Definition: climate.cpp:320
const uint8_t YASHIMA_MODE_FAN_BYTE5
Definition: yashima.cpp:55
void set_supports_current_temperature(bool supports_current_temperature)
void control(const climate::ClimateCall &call) override
Override control to change settings of the climate device.
Definition: yashima.cpp:120
const uint8_t YASHIMA_MODE_HEAT_BYTE5
Definition: yashima.cpp:53
remote_transmitter::RemoteTransmitterComponent * transmitter_
Definition: yashima.h:35
const uint8_t YASHIMA_MODE_COOL_BYTE5
Definition: yashima.cpp:57
const uint16_t YASHIMA_BITS
Definition: yashima.cpp:10
void add_supported_mode(ClimateMode mode)
const uint16_t YASHIMA_HEADER_SPACE
Definition: yashima.cpp:74
const uint32_t YASHIMA_CARRIER_FREQUENCY
Definition: yashima.cpp:80
const uint8_t YASHIMA_BASE_BYTE3
Definition: yashima.cpp:50
const uint8_t YASHIMA_MODE_DRY_BYTE0
Definition: yashima.cpp:14
const uint8_t YASHIMA_TEMP_MIN
Definition: yashima.cpp:22
climate::ClimateTraits traits() override
Return the traits of this controller.
Definition: yashima.cpp:82
const uint8_t YASHIMA_BASE_BYTE5
Definition: yashima.cpp:59
const uint32_t YASHIMA_GAP
Definition: yashima.cpp:78