Midea climate support (#1328)

* Added support for Midea IoT climate devices via UART interface (USB-dongle)

* Fixed lint checks

* Fixed lint checks

* CODEOWNERS update

* Clang-format

* Clang-format

* Add network device notification message support (show WiFi sign on devices)

* Make wifi_signal_sensor optional component

* Some optimization

* Optimizations and code formatting

* Fixed lint checks

* Fixed lint checks

* Fixed sign error

* Code changes

* Network notify repeat every 10 min

* Added log messages

* Fixed lint checks

* Refactoring: MideaClimate => MideaAC

* Using enums instead literals in Midea states

* Enum changed to be more correct

* Shrink notify frame to 32 bytes

* Fixed lint checks

* Change notify frame appliance type to common broadcast

* Control optimization

* Fixed control error

* Control command now don't reset others uncontrollable properties of device

* Fixed lint checks

* Some optimization

* on_receive callback give const Frame

* Fix control

* Fixes

* Some minor changes

* Fixed lint error

* No dependency from wifi_signal sensor for stretched WiFi icon. New option: stretched_icon instead wifi_signal_id.

* Fix option name

* Added export of outdoor temperature as sensor value

* Fixed lint errors

* Fixed pylint error

* Minor fix

* Fix temperature overflow in some cases

* Added answer on QueryNetwork command from appliance. Now don't wait for ack on 0x0D command.

* Fix lint error

* Added humidity setpoint optional sensor

* Added boolean options 'swing_horizontal' and 'swing_both'

* Added debug frame output

* Added debug frame output

* Fix lints error

* Some debug output optimization

* Fix lint check

* Some code optimization: adding templates

* Fix lint error

* Added sensors device classes

* Python code reformatted with black formatter

* RX frame debug message

RX frame debug message now prints before checking

* Remove CRC check for receiving frames

* Added experimental power usage option

* Added power usage option

* Fixed lint errors

* Major changes. See esp-docs.

* Added tests in test4.yaml

* Added tests in test1.yaml

* Added wifi dependency

* Fix test1.yaml

* Some fix :)

* One more refactoring

* One more refactoring

* One more refactoring
This commit is contained in:
Sergey V. DUDANOV
2021-03-18 00:27:50 +04:00
committed by GitHub
parent faf577a9dd
commit f34c9b33fc
13 changed files with 925 additions and 0 deletions
@@ -0,0 +1,30 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import uart
from esphome.const import CONF_ID
DEPENDENCIES = ["wifi", "uart"]
CODEOWNERS = ["@dudanov"]
midea_dongle_ns = cg.esphome_ns.namespace("midea_dongle")
MideaDongle = midea_dongle_ns.class_("MideaDongle", cg.Component, uart.UARTDevice)
CONF_MIDEA_DONGLE_ID = "midea_dongle_id"
CONF_STRENGTH_ICON = "strength_icon"
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(MideaDongle),
cv.Optional(CONF_STRENGTH_ICON, default=False): cv.boolean,
}
)
.extend(cv.COMPONENT_SCHEMA)
.extend(uart.UART_DEVICE_SCHEMA)
)
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config)
yield uart.register_uart_device(var, config)
cg.add(var.use_strength_icon(config[CONF_STRENGTH_ICON]))
@@ -0,0 +1,98 @@
#include "midea_dongle.h"
#include "esphome/core/log.h"
#include "esphome/core/helpers.h"
namespace esphome {
namespace midea_dongle {
static const char *TAG = "midea_dongle";
void MideaDongle::loop() {
while (this->available()) {
const uint8_t rx = this->read();
if (this->idx_ <= OFFSET_LENGTH) {
if (this->idx_ == OFFSET_LENGTH) {
if (rx <= OFFSET_BODY || rx >= sizeof(this->buf_)) {
this->reset_();
continue;
}
this->cnt_ = rx;
} else if (rx != SYNC_BYTE) {
continue;
}
}
this->buf_[this->idx_++] = rx;
if (--this->cnt_)
continue;
this->reset_();
const BaseFrame frame(this->buf_);
ESP_LOGD(TAG, "RX: %s", frame.to_string().c_str());
if (!frame.is_valid()) {
ESP_LOGW(TAG, "RX: frame check failed!");
continue;
}
if (frame.get_type() == QUERY_NETWORK) {
this->notify_.set_type(QUERY_NETWORK);
this->need_notify_ = true;
continue;
}
if (this->appliance_ != nullptr)
this->appliance_->on_frame(frame);
}
}
void MideaDongle::update() {
const bool is_conn = WiFi.isConnected();
uint8_t wifi_strength = 0;
if (!this->rssi_timer_) {
if (is_conn)
wifi_strength = 4;
} else if (is_conn) {
if (--this->rssi_timer_) {
wifi_strength = this->notify_.get_signal_strength();
} else {
this->rssi_timer_ = 60;
const long dbm = WiFi.RSSI();
if (dbm > -63)
wifi_strength = 4;
else if (dbm > -75)
wifi_strength = 3;
else if (dbm > -88)
wifi_strength = 2;
else if (dbm > -100)
wifi_strength = 1;
}
} else {
this->rssi_timer_ = 1;
}
if (this->notify_.is_connected() != is_conn) {
this->notify_.set_connected(is_conn);
this->need_notify_ = true;
}
if (this->notify_.get_signal_strength() != wifi_strength) {
this->notify_.set_signal_strength(wifi_strength);
this->need_notify_ = true;
}
if (!--this->notify_timer_) {
this->notify_.set_type(NETWORK_NOTIFY);
this->need_notify_ = true;
}
if (this->need_notify_) {
ESP_LOGD(TAG, "TX: notify WiFi STA %s, signal strength %d", is_conn ? "connected" : "not connected", wifi_strength);
this->need_notify_ = false;
this->notify_timer_ = 600;
this->notify_.finalize();
this->write_frame(this->notify_);
return;
}
if (this->appliance_ != nullptr)
this->appliance_->on_update();
}
void MideaDongle::write_frame(const Frame &frame) {
this->write_array(frame.data(), frame.size());
ESP_LOGD(TAG, "TX: %s", frame.to_string().c_str());
}
} // namespace midea_dongle
} // namespace esphome
@@ -0,0 +1,56 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/wifi/wifi_component.h"
#include "esphome/components/uart/uart.h"
#include "midea_frame.h"
namespace esphome {
namespace midea_dongle {
enum MideaApplianceType : uint8_t { DEHUMIDIFIER = 0xA1, AIR_CONDITIONER = 0xAC, BROADCAST = 0xFF };
enum MideaMessageType : uint8_t {
DEVICE_CONTROL = 0x02,
DEVICE_QUERY = 0x03,
NETWORK_NOTIFY = 0x0D,
QUERY_NETWORK = 0x63,
};
struct MideaAppliance {
/// Calling on update event
virtual void on_update() = 0;
/// Calling on frame receive event
virtual void on_frame(const Frame &frame) = 0;
};
class MideaDongle : public PollingComponent, public uart::UARTDevice {
public:
MideaDongle() : PollingComponent(1000) {}
float get_setup_priority() const override { return setup_priority::LATE; }
void update() override;
void loop() override;
void set_appliance(MideaAppliance *app) { this->appliance_ = app; }
void use_strength_icon(bool state) { this->rssi_timer_ = state; }
void write_frame(const Frame &frame);
protected:
MideaAppliance *appliance_{nullptr};
NotifyFrame notify_;
unsigned notify_timer_{1};
// Buffer
uint8_t buf_[36];
// Index
uint8_t idx_{0};
// Reverse receive counter
uint8_t cnt_{2};
uint8_t rssi_timer_{0};
bool need_notify_{false};
// Reset receiver state
void reset_() {
this->idx_ = 0;
this->cnt_ = 2;
}
};
} // namespace midea_dongle
} // namespace esphome
@@ -0,0 +1,95 @@
#include "midea_frame.h"
namespace esphome {
namespace midea_dongle {
const uint8_t BaseFrame::CRC_TABLE[] = {
0x00, 0x5E, 0xBC, 0xE2, 0x61, 0x3F, 0xDD, 0x83, 0xC2, 0x9C, 0x7E, 0x20, 0xA3, 0xFD, 0x1F, 0x41, 0x9D, 0xC3, 0x21,
0x7F, 0xFC, 0xA2, 0x40, 0x1E, 0x5F, 0x01, 0xE3, 0xBD, 0x3E, 0x60, 0x82, 0xDC, 0x23, 0x7D, 0x9F, 0xC1, 0x42, 0x1C,
0xFE, 0xA0, 0xE1, 0xBF, 0x5D, 0x03, 0x80, 0xDE, 0x3C, 0x62, 0xBE, 0xE0, 0x02, 0x5C, 0xDF, 0x81, 0x63, 0x3D, 0x7C,
0x22, 0xC0, 0x9E, 0x1D, 0x43, 0xA1, 0xFF, 0x46, 0x18, 0xFA, 0xA4, 0x27, 0x79, 0x9B, 0xC5, 0x84, 0xDA, 0x38, 0x66,
0xE5, 0xBB, 0x59, 0x07, 0xDB, 0x85, 0x67, 0x39, 0xBA, 0xE4, 0x06, 0x58, 0x19, 0x47, 0xA5, 0xFB, 0x78, 0x26, 0xC4,
0x9A, 0x65, 0x3B, 0xD9, 0x87, 0x04, 0x5A, 0xB8, 0xE6, 0xA7, 0xF9, 0x1B, 0x45, 0xC6, 0x98, 0x7A, 0x24, 0xF8, 0xA6,
0x44, 0x1A, 0x99, 0xC7, 0x25, 0x7B, 0x3A, 0x64, 0x86, 0xD8, 0x5B, 0x05, 0xE7, 0xB9, 0x8C, 0xD2, 0x30, 0x6E, 0xED,
0xB3, 0x51, 0x0F, 0x4E, 0x10, 0xF2, 0xAC, 0x2F, 0x71, 0x93, 0xCD, 0x11, 0x4F, 0xAD, 0xF3, 0x70, 0x2E, 0xCC, 0x92,
0xD3, 0x8D, 0x6F, 0x31, 0xB2, 0xEC, 0x0E, 0x50, 0xAF, 0xF1, 0x13, 0x4D, 0xCE, 0x90, 0x72, 0x2C, 0x6D, 0x33, 0xD1,
0x8F, 0x0C, 0x52, 0xB0, 0xEE, 0x32, 0x6C, 0x8E, 0xD0, 0x53, 0x0D, 0xEF, 0xB1, 0xF0, 0xAE, 0x4C, 0x12, 0x91, 0xCF,
0x2D, 0x73, 0xCA, 0x94, 0x76, 0x28, 0xAB, 0xF5, 0x17, 0x49, 0x08, 0x56, 0xB4, 0xEA, 0x69, 0x37, 0xD5, 0x8B, 0x57,
0x09, 0xEB, 0xB5, 0x36, 0x68, 0x8A, 0xD4, 0x95, 0xCB, 0x29, 0x77, 0xF4, 0xAA, 0x48, 0x16, 0xE9, 0xB7, 0x55, 0x0B,
0x88, 0xD6, 0x34, 0x6A, 0x2B, 0x75, 0x97, 0xC9, 0x4A, 0x14, 0xF6, 0xA8, 0x74, 0x2A, 0xC8, 0x96, 0x15, 0x4B, 0xA9,
0xF7, 0xB6, 0xE8, 0x0A, 0x54, 0xD7, 0x89, 0x6B, 0x35};
const uint8_t NotifyFrame::INIT[] = {0xAA, 0x1F, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x0D, 0x01,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
bool BaseFrame::is_valid() const { return /*this->has_valid_crc_() &&*/ this->has_valid_cs_(); }
void BaseFrame::finalize() {
this->update_crc_();
this->update_cs_();
}
void BaseFrame::update_crc_() {
uint8_t crc = 0;
uint8_t *ptr = this->pbuf_ + OFFSET_BODY;
uint8_t len = this->length_() - OFFSET_BODY;
while (--len)
crc = pgm_read_byte(BaseFrame::CRC_TABLE + (crc ^ *ptr++));
*ptr = crc;
}
void BaseFrame::update_cs_() {
uint8_t cs = 0;
uint8_t *ptr = this->pbuf_ + OFFSET_LENGTH;
uint8_t len = this->length_();
while (--len)
cs -= *ptr++;
*ptr = cs;
}
bool BaseFrame::has_valid_crc_() const {
uint8_t crc = 0;
uint8_t len = this->length_() - OFFSET_BODY;
const uint8_t *ptr = this->pbuf_ + OFFSET_BODY;
for (; len; ptr++, len--)
crc = pgm_read_byte(BaseFrame::CRC_TABLE + (crc ^ *ptr));
return !crc;
}
bool BaseFrame::has_valid_cs_() const {
uint8_t cs = 0;
uint8_t len = this->length_();
const uint8_t *ptr = this->pbuf_ + OFFSET_LENGTH;
for (; len; ptr++, len--)
cs -= *ptr;
return !cs;
}
void BaseFrame::set_bytemask_(uint8_t idx, uint8_t mask, bool state) {
uint8_t *dst = this->pbuf_ + idx;
if (state)
*dst |= mask;
else
*dst &= ~mask;
}
static char u4hex(uint8_t num) { return num + ((num < 10) ? '0' : ('A' - 10)); }
String Frame::to_string() const {
String ret;
char buf[4];
buf[2] = ' ';
buf[3] = '\0';
ret.reserve(3 * 36);
const uint8_t *it = this->data();
for (size_t i = 0; i < this->size(); i++, it++) {
buf[0] = u4hex(*it >> 4);
buf[1] = u4hex(*it & 15);
ret.concat(buf);
}
return ret;
}
} // namespace midea_dongle
} // namespace esphome
@@ -0,0 +1,104 @@
#pragma once
#include "esphome/core/component.h"
namespace esphome {
namespace midea_dongle {
static const uint8_t OFFSET_START = 0;
static const uint8_t OFFSET_LENGTH = 1;
static const uint8_t OFFSET_APPTYPE = 2;
static const uint8_t OFFSET_BODY = 10;
static const uint8_t SYNC_BYTE = 0xAA;
class Frame {
public:
Frame() = delete;
Frame(uint8_t *data) : pbuf_(data) {}
Frame(const Frame &frame) : pbuf_(frame.data()) {}
// Frame buffer
uint8_t *data() const { return this->pbuf_; }
// Frame size
uint8_t size() const { return this->length_() + OFFSET_LENGTH; }
uint8_t app_type() const { return this->pbuf_[OFFSET_APPTYPE]; }
template<typename T> typename std::enable_if<std::is_base_of<Frame, T>::value, T>::type as() const {
return T(*this);
}
String to_string() const;
protected:
uint8_t *pbuf_;
uint8_t length_() const { return this->pbuf_[OFFSET_LENGTH]; }
};
class BaseFrame : public Frame {
public:
BaseFrame() = delete;
BaseFrame(uint8_t *data) : Frame(data) {}
BaseFrame(const Frame &frame) : Frame(frame) {}
// Check for valid
bool is_valid() const;
// Prepare for sending to device
void finalize();
uint8_t get_type() const { return this->pbuf_[9]; }
void set_type(uint8_t value) { this->pbuf_[9] = value; }
bool has_response_type(uint8_t type) const { return this->resp_type_() == type; }
bool has_type(uint8_t type) const { return this->get_type() == type; }
protected:
static const uint8_t PROGMEM CRC_TABLE[256];
void set_bytemask_(uint8_t idx, uint8_t mask, bool state);
uint8_t resp_type_() const { return this->pbuf_[OFFSET_BODY]; }
bool has_valid_crc_() const;
bool has_valid_cs_() const;
void update_crc_();
void update_cs_();
};
template<typename T = Frame, size_t buf_size = 36> class StaticFrame : public T {
public:
// Default constructor
StaticFrame() : T(this->buf_) {}
// Copy constructor
StaticFrame(const Frame &src) : T(this->buf_) {
if (src.length_() < sizeof(this->buf_)) {
memcpy(this->buf_, src.data(), src.length_() + OFFSET_LENGTH);
}
}
// Constructor for RAM data
StaticFrame(const uint8_t *src) : T(this->buf_) {
const uint8_t len = src[OFFSET_LENGTH];
if (len < sizeof(this->buf_)) {
memcpy(this->buf_, src, len + OFFSET_LENGTH);
}
}
// Constructor for PROGMEM data
StaticFrame(const __FlashStringHelper *pgm) : T(this->buf_) {
const uint8_t *src = reinterpret_cast<decltype(src)>(pgm);
const uint8_t len = pgm_read_byte(src + OFFSET_LENGTH);
if (len < sizeof(this->buf_)) {
memcpy_P(this->buf_, src, len + OFFSET_LENGTH);
}
}
protected:
uint8_t buf_[buf_size];
};
// Device network notification frame
class NotifyFrame : public midea_dongle::StaticFrame<BaseFrame, 32> {
public:
NotifyFrame() : StaticFrame(FPSTR(NotifyFrame::INIT)) {}
void set_signal_strength(uint8_t value) { this->pbuf_[12] = value; }
uint8_t get_signal_strength() const { return this->pbuf_[12]; }
void set_connected(bool state) { this->pbuf_[18] = state ? 0 : 1; }
bool is_connected() const { return !this->pbuf_[18]; }
private:
static const uint8_t PROGMEM INIT[];
};
} // namespace midea_dongle
} // namespace esphome