17static constexpr uint8_t SYNC_BYTE = 0x7A;
18static constexpr uint8_t LENGTH_POS = 4;
19static constexpr uint8_t OPCODE_POS = 7;
20static constexpr uint8_t MIN_FRAME_LEN = OPCODE_POS + 1 + 2;
22static constexpr uint32_t FRAME_GAP_MS = 50;
24static constexpr uint8_t ADDR_MODULE = 0x21;
25static constexpr uint8_t ADDR_DISPLAY = 0xD1;
26static constexpr uint8_t ADDR_BOARD = 0xD5;
28static constexpr uint8_t OP_A3_STATE = 0xA3;
29static constexpr uint8_t OP_A4_ACK = 0xA4;
30static constexpr uint8_t OP_AC_POLL = 0xAC;
33static constexpr uint8_t A5_HEADER[] PROGMEM = {
34 0x7A, 0x7A, 0x21, 0xD1, 0x1B, 0x00, 0x00, 0xA5, 0x0A, 0x0A, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
38static constexpr uint8_t AB_FRAME[] PROGMEM = {
39 0x7A, 0x7A, 0x21, 0xD5, 0x0C, 0x00, 0x00, 0xAB, 0x0A, 0x0A, 0xFC, 0xF9,
42static constexpr uint8_t TEMP_MIN = 16;
43static constexpr uint8_t TEMP_MAX = 31;
46static constexpr uint8_t POWER_BIT = 0x08;
47static constexpr uint8_t MODE_MASK = 0x07;
48static constexpr uint8_t FAN_AUTO = 0x0;
49static constexpr uint8_t FAN_SUPER = 0x8;
50static constexpr uint8_t FAN_STRONG = FAN_SUPER | 0x5;
52static constexpr uint8_t FLAG_QUIET = 0x40;
53static constexpr uint8_t FLAG_FAHRENHEIT = 0x20;
54static constexpr uint8_t SETPOINT_MASK = 0x1F;
56static constexpr uint8_t SWING_VERTICAL = 0x01;
57static constexpr uint8_t SWING_HORIZONTAL = 0x10;
58static constexpr uint8_t SWING_MASK = SWING_VERTICAL | SWING_HORIZONTAL;
66static constexpr CodeMap MODE_CODES[] PROGMEM = {
71static constexpr CodeMap SWING_CODES[] PROGMEM = {
78static constexpr CodeMap FAN_CODES[] PROGMEM = {
86static constexpr const char *
const CUSTOM_FAN_MODES[] = {
"medlow",
"medhigh",
"strong"};
87static constexpr uint8_t CUSTOM_FAN_CODES[] PROGMEM = {0x2, 0x4, FAN_STRONG};
89template<
size_t N>
static bool code_of(
const CodeMap (&table)[N], uint8_t
value, uint8_t &code) {
90 for (
const CodeMap &entry : table) {
99template<
size_t N>
static bool value_of(
const CodeMap (&table)[N], uint8_t code, uint8_t &
value) {
100 for (
const CodeMap &entry : table) {
118 this->
set_interval(10000, [
this]() { this->send_a5_(); });
124 bool received =
false;
146 std::memmove(this->
rx_buffer_.data(), this->rx_buffer_.data() + 1, this->rx_pos_);
154 if (buf[0] == SYNC_BYTE && (this->
rx_pos_ < 2 || buf[1] == SYNC_BYTE)) {
155 if (this->
rx_pos_ <= LENGTH_POS)
157 const uint8_t
len = buf[LENGTH_POS];
158 if (
len >= MIN_FRAME_LEN &&
len <= MAX_FRAME_LEN) {
165 ESP_LOGW(TAG,
"Bad CRC on %u-byte frame",
len);
171 std::memmove(this->
rx_buffer_.data(), buf + drop, this->rx_pos_);
176 const uint8_t opcode = data[OPCODE_POS];
177#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
178 if (opcode == OP_A3_STATE || opcode == OP_AC_POLL) {
183 if (data[3] != ADDR_MODULE)
185 if (data[2] == ADDR_BOARD) {
186 if (
len == 34 && opcode == OP_A3_STATE) {
188 }
else if (
len == 18 && opcode == OP_AC_POLL) {
191 }
else if (data[2] == ADDR_DISPLAY &&
len == 13 && opcode == OP_A4_ACK) {
192 ESP_LOGD(TAG,
"A4 acknowledgement from display");
197 const uint8_t mode_fan = data[13];
198 const uint8_t setpoint_raw = data[14];
201 this->
fahrenheit_ = (setpoint_raw & FLAG_FAHRENHEIT) != 0;
203 const uint8_t fan = (this->
fan_raw_ & FAN_SUPER) != 0 ? FAN_STRONG : this->
fan_raw_;
204 const uint8_t
mode = mode_fan & (POWER_BIT | MODE_MASK);
205 const uint8_t setpoint = (setpoint_raw & SETPOINT_MASK) + TEMP_MIN;
207 this->
current_temperature =
static_cast<float>(data[10]) +
static_cast<float>(data[11]) / 10.0f;
212 if ((
mode & POWER_BIT) == 0) {
214 }
else if (value_of(MODE_CODES, this->
mode_bits_, value)) {
218 if ((setpoint_raw & FLAG_QUIET) != 0) {
220 }
else if (value_of(FAN_CODES, fan,
value)) {
224 for (
size_t i = 0; i < std::size(CUSTOM_FAN_MODES) && !found; i++) {
237 if (value_of(SWING_CODES, this->
swing_ & SWING_MASK,
value))
241 ESP_LOGD(TAG,
"A3: mode=0x%02X fan=0x%02X setpoint=%u ambient=%.1f swing=%u flags=0x%02X",
mode, fan, setpoint,
248 const uint16_t total_usage_time =
encode_uint16(data[10], data[11]);
251 this->total_usage_time_sensor_.publish_state_if_not_dup(total_usage_time);
252 this->revision_sensor_.publish_state_if_not_dup(revision);
254 ESP_LOGD(TAG,
"AC: total_usage_time=%u h revision=%u", total_usage_time, revision);
259 PUBLISH_STATE(this->display_switch_, (this->
flags_ & FLAG_DISPLAY) != 0);
260 PUBLISH_STATE(this->ionizer_switch_, (this->
flags_ & FLAG_IONIZER) != 0);
261 PUBLISH_STATE(this->sleep_switch_, (this->
flags_ & FLAG_SLEEP) != 0);
262 PUBLISH_STATE(this->eco_switch_, (this->
flags_ & FLAG_ECO) != 0);
263 PUBLISH_STATE(this->health_switch_, (this->
flags_ & FLAG_HEALTH) != 0);
269 ESP_LOGW(TAG,
"No state from the unit yet, change ignored");
288 if (
call.get_target_temperature().has_value()) {
291 std::clamp<int32_t>(
static_cast<int32_t
>(std::lround(*
call.get_target_temperature())), TEMP_MIN, TEMP_MAX));
293 if (
call.has_custom_fan_mode()) {
295 }
else if (
call.get_fan_mode().has_value()) {
298 if (
call.get_swing_mode().has_value()) {
303 this->
swing_ =
static_cast<uint8_t
>((this->
swing_ & ~SWING_MASK) | code);
316 uint8_t fan_nibble = this->
fan_raw_;
320 for (
size_t i = 0; i < std::size(CUSTOM_FAN_MODES); i++) {
321 if (custom == CUSTOM_FAN_MODES[i]) {
327 if (fan_nibble == FAN_STRONG && (this->
fan_raw_ & FAN_SUPER) != 0)
329 }
else if (this->
fan_mode.has_value()) {
332 code_of(FAN_CODES, *this->
fan_mode, fan_nibble);
336 const int32_t rounded =
337 std::clamp<int32_t>(
static_cast<int32_t
>(std::lround(this->
target_temperature)), TEMP_MIN, TEMP_MAX);
338 uint8_t setpoint =
static_cast<uint8_t
>(rounded - TEMP_MIN);
340 setpoint |= FLAG_QUIET;
342 setpoint |= FLAG_FAHRENHEIT;
344 const uint8_t
state =
static_cast<uint8_t
>((fan_nibble << 4) | mode_nibble);
347 frame[0] = SYNC_BYTE;
348 frame[1] = SYNC_BYTE;
349 frame[2] = ADDR_MODULE;
350 frame[3] = ADDR_BOARD;
351 frame[LENGTH_POS] =
sizeof(frame);
352 frame[OPCODE_POS] = 0xA1;
354 frame[13] = setpoint;
358 ESP_LOGD(TAG,
"A1: state=0x%02X setpoint=0x%02X swing=0x%02X flags=0x%02X",
state, setpoint, this->
swing_,
362void AirtonSerialClimate::send_a5_() {
363 uint8_t frame[
sizeof(A5_HEADER) + MAC_ADDRESS_SIZE + 2]{};
370 uint8_t frame[
sizeof(AB_FRAME)];
376 const uint16_t crc =
crc16(frame,
len - 2);
377 frame[
len - 2] =
static_cast<uint8_t
>(crc >> 8);
378 frame[
len - 1] =
static_cast<uint8_t
>(crc);
399 LOG_CLIMATE(
"",
"Airton Serial Climate",
this);
401 LOG_SWITCH(
" ",
"Display", this->display_switch_);
402 LOG_SWITCH(
" ",
"Ionizer", this->ionizer_switch_);
403 LOG_SWITCH(
" ",
"Sleep", this->sleep_switch_);
404 LOG_SWITCH(
" ",
"Eco", this->eco_switch_);
405 LOG_SWITCH(
" ",
"Health", this->health_switch_);
408 LOG_SENSOR_WITH_DEDUP(
" ",
"Total Usage Time", this->total_usage_time_sensor_);
409 LOG_SENSOR_WITH_DEDUP(
" ",
"Revision", this->revision_sensor_);
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void set_interval(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a const char* name.
StringRef is a reference to a string owned by something else.
climate::ClimateTraits traits() override
void control(const climate::ClimateCall &call) override
uint16_t SUB_SENSOR_WITH_DEDUP(revision, uint16_t) protected void send_ab_()
void write_frame_(uint8_t *frame, size_t len)
std::array< uint8_t, MAX_FRAME_LEN > rx_buffer_
bool set_flag(uint8_t flag, bool state)
Set or clear one FLAG_* bit and send it to the unit; false until the unit has reported its state.
void handle_byte_(uint8_t byte)
void handle_ac_(const uint8_t *data)
void dump_config() override
void handle_a3_(const uint8_t *data)
void handle_frame_(const uint8_t *data, size_t len)
This class is used to encode all control actions on a climate device.
const optional< ClimateSwingMode > & get_swing_mode() const
const optional< ClimateFanMode > & get_fan_mode() const
StringRef get_custom_fan_mode() const
const optional< ClimateMode > & get_mode() const
ClimateMode mode
The active mode of the climate device.
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
float target_temperature
The target temperature of the climate device.
ClimateSwingMode swing_mode
The active swing mode of the climate device.
bool set_fan_mode_(ClimateFanMode mode)
Set fan mode. Reset custom fan mode. Return true if fan mode has been changed.
void clear_custom_fan_mode_()
Clear custom fan mode.
float current_temperature
The current temperature of the climate device, as reported from the integration.
void set_supported_custom_fan_modes_static(const char *const *modes, size_t count)
Points at a static table that outlives the climate (codegen); call before any runtime copy.
void publish_state()
Publish the state of the climate device, to be called from integrations.
bool has_custom_fan_mode() const
Check if a custom fan mode is currently active.
bool set_custom_fan_mode_(const char *mode)
Set custom fan mode. Reset primary fan mode. Return true if fan mode has been changed.
StringRef get_custom_fan_mode() const
Get the active custom fan mode (read-only access). Returns StringRef.
void set_visual_max_temperature(float visual_max_temperature)
void add_feature_flags(uint32_t feature_flags)
void set_visual_target_temperature_step(float temperature_step)
void set_supported_swing_modes(ClimateSwingModeMask modes)
void set_visual_min_temperature(float visual_min_temperature)
void set_visual_current_temperature_step(float temperature_step)
void set_supported_modes(ClimateModeMask modes)
void set_supported_fan_modes(ClimateFanModeMask modes)
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
void write_array(const uint8_t *data, size_t len)
ESPHOME_LOG_TAG(TAG, "airton_serial.climate")
@ CLIMATE_SUPPORTS_CURRENT_TEMPERATURE
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.
ClimateMode
Enum for all modes a climate device can be in.
@ 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.
ClimateFanMode
NOTE: If adding values, update ClimateFanModeMask in climate_traits.h to use the new last value.
@ CLIMATE_FAN_MEDIUM
The fan mode is set to Medium.
@ 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.
uint16_t crc16(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t reverse_poly, bool refin, bool refout)
Calculate a CRC-16 checksum of data with size len.
void progmem_memcpy(void *dst, const void *src, size_t len)
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.
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
void get_mac_address_raw(uint8_t *mac)
Get the device MAC address as raw bytes, written into the provided byte array (6 bytes).
Application App
Global storage of Application pointer - only one Application can exist.
uint8_t progmem_read_byte(const uint8_t *addr)