Files
esphome-dev/esphome/components/midea_dongle/midea_dongle.cpp
T
Sergey V. DUDANOV f34c9b33fc 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
2021-03-17 17:27:50 -03:00

99 lines
2.6 KiB
C++

#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