From ea019a057b7ca14033f24ea143d54b24ef9808b8 Mon Sep 17 00:00:00 2001 From: Paul Nicholls Date: Tue, 26 Jan 2021 17:44:10 +1300 Subject: [PATCH 1/4] Add support for string-type Tuya datapoints (#1488) --- esphome/components/tuya/tuya.cpp | 18 +++++++++++++++++- esphome/components/tuya/tuya.h | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/esphome/components/tuya/tuya.cpp b/esphome/components/tuya/tuya.cpp index 2fd4b27937..75514dde19 100644 --- a/esphome/components/tuya/tuya.cpp +++ b/esphome/components/tuya/tuya.cpp @@ -42,6 +42,8 @@ void Tuya::dump_config() { ESP_LOGCONFIG(TAG, " Datapoint %d: switch (value: %s)", info.id, ONOFF(info.value_bool)); else if (info.type == TuyaDatapointType::INTEGER) ESP_LOGCONFIG(TAG, " Datapoint %d: int value (value: %d)", info.id, info.value_int); + else if (info.type == TuyaDatapointType::STRING) + ESP_LOGCONFIG(TAG, " Datapoint %d: string value (value: %s)", info.id, info.value_string.c_str()); else if (info.type == TuyaDatapointType::ENUM) ESP_LOGCONFIG(TAG, " Datapoint %d: enum (value: %d)", info.id, info.value_enum); else if (info.type == TuyaDatapointType::BITMASK) @@ -283,6 +285,9 @@ void Tuya::handle_datapoint_(const uint8_t *buffer, size_t len) { return; datapoint.value_uint = encode_uint32(data[0], data[1], data[2], data[3]); break; + case TuyaDatapointType::STRING: + datapoint.value_string = std::string(reinterpret_cast(data), data_len); + break; case TuyaDatapointType::ENUM: if (data_len != 1) return; @@ -339,7 +344,13 @@ void Tuya::set_datapoint_value(TuyaDatapoint datapoint) { ESP_LOGV(TAG, "Datapoint %u set to %u", datapoint.id, datapoint.value_uint); for (auto &other : this->datapoints_) { if (other.id == datapoint.id) { - if (other.value_uint == datapoint.value_uint) { + // String value is stored outside the union; must be checked separately. + if (datapoint.type == TuyaDatapointType::STRING) { + if (other.value_string == datapoint.value_string) { + ESP_LOGV(TAG, "Not sending unchanged value"); + return; + } + } else if (other.value_uint == datapoint.value_uint) { ESP_LOGV(TAG, "Not sending unchanged value"); return; } @@ -359,6 +370,11 @@ void Tuya::set_datapoint_value(TuyaDatapoint datapoint) { data.push_back(datapoint.value_uint >> 8); data.push_back(datapoint.value_uint >> 0); break; + case TuyaDatapointType::STRING: + for (char const &c : datapoint.value_string) { + data.push_back(c); + } + break; case TuyaDatapointType::ENUM: data.push_back(datapoint.value_enum); break; diff --git a/esphome/components/tuya/tuya.h b/esphome/components/tuya/tuya.h index ba20cfd314..ddbbb48edf 100644 --- a/esphome/components/tuya/tuya.h +++ b/esphome/components/tuya/tuya.h @@ -30,6 +30,7 @@ struct TuyaDatapoint { uint8_t value_enum; uint16_t value_bitmask; }; + std::string value_string; }; struct TuyaDatapointListener { From 95bef53d3771f15b66efb99399fe3cfff671c59b Mon Sep 17 00:00:00 2001 From: Klarstein <62219057+Klarstein@users.noreply.github.com> Date: Wed, 27 Jan 2021 07:16:59 +0100 Subject: [PATCH 2/4] Add docker healthcheck (#1492) --- docker/Dockerfile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docker/Dockerfile b/docker/Dockerfile index 899a1654c6..12b06ec284 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -15,6 +15,10 @@ ENV USERNAME="" PASSWORD="" # Expose the dashboard to Docker EXPOSE 6052 +# Run healthcheck (heartbeat) +HEALTHCHECK --interval=5m --timeout=3s \ + CMD curl --fail http://localhost:6052 || exit 1 + # The directory the user should mount their configuration files to WORKDIR /config # Set entrypoint to esphome so that the user doesn't have to type 'esphome' From 5057caa7fc1e8132826ec8b1b2d00c701bb9c89e Mon Sep 17 00:00:00 2001 From: nikito7 <45373783+nikito7@users.noreply.github.com> Date: Wed, 27 Jan 2021 07:14:43 +0000 Subject: [PATCH 3/4] Add support for MHO-C401 (#1486) Committer: nikito7 Co-authored-by: vevsvevs Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Co-authored-by: nikito7 --- CODEOWNERS | 1 + esphome/components/xiaomi_ble/xiaomi_ble.cpp | 3 + esphome/components/xiaomi_ble/xiaomi_ble.h | 3 +- esphome/components/xiaomi_mhoc401/__init__.py | 0 esphome/components/xiaomi_mhoc401/sensor.py | 43 ++++++++++ .../xiaomi_mhoc401/xiaomi_mhoc401.cpp | 81 +++++++++++++++++++ .../xiaomi_mhoc401/xiaomi_mhoc401.h | 36 +++++++++ 7 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 esphome/components/xiaomi_mhoc401/__init__.py create mode 100644 esphome/components/xiaomi_mhoc401/sensor.py create mode 100644 esphome/components/xiaomi_mhoc401/xiaomi_mhoc401.cpp create mode 100644 esphome/components/xiaomi_mhoc401/xiaomi_mhoc401.h diff --git a/CODEOWNERS b/CODEOWNERS index 4c24586bb6..0106b87fa8 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -100,3 +100,4 @@ esphome/components/version/* @esphome/core esphome/components/web_server_base/* @OttoWinter esphome/components/whirlpool/* @glmnet esphome/components/xiaomi_lywsd03mmc/* @ahpohl +esphome/components/xiaomi_mhoc401/* @vevsvevs diff --git a/esphome/components/xiaomi_ble/xiaomi_ble.cpp b/esphome/components/xiaomi_ble/xiaomi_ble.cpp index 75af1e4b7c..033269f66c 100644 --- a/esphome/components/xiaomi_ble/xiaomi_ble.cpp +++ b/esphome/components/xiaomi_ble/xiaomi_ble.cpp @@ -194,6 +194,9 @@ optional parse_xiaomi_header(const esp32_ble_tracker::Service result.name = "MJYD02YLA"; if (raw.size() == 19) result.raw_offset -= 6; + } else if ((raw[2] == 0x87) && (raw[3] == 0x03)) { // square body, e-ink display + result.type = XiaomiParseResult::TYPE_MHOC401; + result.name = "MHOC401"; } else { ESP_LOGVV(TAG, "parse_xiaomi_header(): unknown device, no magic bytes."); return {}; diff --git a/esphome/components/xiaomi_ble/xiaomi_ble.h b/esphome/components/xiaomi_ble/xiaomi_ble.h index ad73226159..3973dac80f 100644 --- a/esphome/components/xiaomi_ble/xiaomi_ble.h +++ b/esphome/components/xiaomi_ble/xiaomi_ble.h @@ -21,7 +21,8 @@ struct XiaomiParseResult { TYPE_JQJCY01YM, TYPE_MUE4094RT, TYPE_WX08ZM, - TYPE_MJYD02YLA + TYPE_MJYD02YLA, + TYPE_MHOC401 } type; std::string name; optional temperature; diff --git a/esphome/components/xiaomi_mhoc401/__init__.py b/esphome/components/xiaomi_mhoc401/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/esphome/components/xiaomi_mhoc401/sensor.py b/esphome/components/xiaomi_mhoc401/sensor.py new file mode 100644 index 0000000000..579ca0619a --- /dev/null +++ b/esphome/components/xiaomi_mhoc401/sensor.py @@ -0,0 +1,43 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import sensor, esp32_ble_tracker +from esphome.const import CONF_BATTERY_LEVEL, CONF_HUMIDITY, CONF_MAC_ADDRESS, CONF_TEMPERATURE, \ + UNIT_CELSIUS, ICON_THERMOMETER, UNIT_PERCENT, ICON_WATER_PERCENT, ICON_BATTERY, CONF_ID, \ + CONF_BINDKEY + +CODEOWNERS = ['@vevsvevs'] +DEPENDENCIES = ['esp32_ble_tracker'] +AUTO_LOAD = ['xiaomi_ble'] + +xiaomi_mhoc401_ns = cg.esphome_ns.namespace('xiaomi_mhoc401') +XiaomiMHOC401 = xiaomi_mhoc401_ns.class_('XiaomiMHOC401', + esp32_ble_tracker.ESPBTDeviceListener, + cg.Component) + +CONFIG_SCHEMA = cv.Schema({ + cv.GenerateID(): cv.declare_id(XiaomiMHOC401), + cv.Required(CONF_BINDKEY): cv.bind_key, + cv.Required(CONF_MAC_ADDRESS): cv.mac_address, + cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(UNIT_CELSIUS, ICON_THERMOMETER, 1), + cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(UNIT_PERCENT, ICON_WATER_PERCENT, 0), + cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(UNIT_PERCENT, ICON_BATTERY, 0), +}).extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA).extend(cv.COMPONENT_SCHEMA) + + +def to_code(config): + var = cg.new_Pvariable(config[CONF_ID]) + yield cg.register_component(var, config) + yield esp32_ble_tracker.register_ble_device(var, config) + + cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex)) + cg.add(var.set_bindkey(config[CONF_BINDKEY])) + + if CONF_TEMPERATURE in config: + sens = yield sensor.new_sensor(config[CONF_TEMPERATURE]) + cg.add(var.set_temperature(sens)) + if CONF_HUMIDITY in config: + sens = yield sensor.new_sensor(config[CONF_HUMIDITY]) + cg.add(var.set_humidity(sens)) + if CONF_BATTERY_LEVEL in config: + sens = yield sensor.new_sensor(config[CONF_BATTERY_LEVEL]) + cg.add(var.set_battery_level(sens)) diff --git a/esphome/components/xiaomi_mhoc401/xiaomi_mhoc401.cpp b/esphome/components/xiaomi_mhoc401/xiaomi_mhoc401.cpp new file mode 100644 index 0000000000..a41df3e6a1 --- /dev/null +++ b/esphome/components/xiaomi_mhoc401/xiaomi_mhoc401.cpp @@ -0,0 +1,81 @@ +#include "xiaomi_mhoc401.h" +#include "esphome/core/log.h" + +#ifdef ARDUINO_ARCH_ESP32 + +namespace esphome { +namespace xiaomi_mhoc401 { + +static const char *TAG = "xiaomi_mhoc401"; + +void XiaomiMHOC401::dump_config() { + ESP_LOGCONFIG(TAG, "Xiaomi MHOC401"); + ESP_LOGCONFIG(TAG, " Bindkey: %s", hexencode(this->bindkey_, 16).c_str()); + LOG_SENSOR(" ", "Temperature", this->temperature_); + LOG_SENSOR(" ", "Humidity", this->humidity_); + LOG_SENSOR(" ", "Battery Level", this->battery_level_); +} + +bool XiaomiMHOC401::parse_device(const esp32_ble_tracker::ESPBTDevice &device) { + if (device.address_uint64() != this->address_) { + ESP_LOGVV(TAG, "parse_device(): unknown MAC address."); + return false; + } + ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str().c_str()); + + bool success = false; + for (auto &service_data : device.get_service_datas()) { + auto res = xiaomi_ble::parse_xiaomi_header(service_data); + if (!res.has_value()) { + continue; + } + if (res->is_duplicate) { + continue; + } + if (res->has_encryption && + (!(xiaomi_ble::decrypt_xiaomi_payload(const_cast &>(service_data.data), this->bindkey_, + this->address_)))) { + continue; + } + if (!(xiaomi_ble::parse_xiaomi_message(service_data.data, *res))) { + continue; + } + if (res->humidity.has_value() && this->humidity_ != nullptr) { + // see https://github.com/custom-components/sensor.mitemp_bt/issues/7#issuecomment-595948254 + *res->humidity = trunc(*res->humidity); + } + if (!(xiaomi_ble::report_xiaomi_results(res, device.address_str()))) { + continue; + } + if (res->temperature.has_value() && this->temperature_ != nullptr) + this->temperature_->publish_state(*res->temperature); + if (res->humidity.has_value() && this->humidity_ != nullptr) + this->humidity_->publish_state(*res->humidity); + if (res->battery_level.has_value() && this->battery_level_ != nullptr) + this->battery_level_->publish_state(*res->battery_level); + success = true; + } + + if (!success) { + return false; + } + + return true; +} + +void XiaomiMHOC401::set_bindkey(const std::string &bindkey) { + memset(bindkey_, 0, 16); + if (bindkey.size() != 32) { + return; + } + char temp[3] = {0}; + for (int i = 0; i < 16; i++) { + strncpy(temp, &(bindkey.c_str()[i * 2]), 2); + bindkey_[i] = std::strtoul(temp, NULL, 16); + } +} + +} // namespace xiaomi_mhoc401 +} // namespace esphome + +#endif diff --git a/esphome/components/xiaomi_mhoc401/xiaomi_mhoc401.h b/esphome/components/xiaomi_mhoc401/xiaomi_mhoc401.h new file mode 100644 index 0000000000..e80916f855 --- /dev/null +++ b/esphome/components/xiaomi_mhoc401/xiaomi_mhoc401.h @@ -0,0 +1,36 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/components/sensor/sensor.h" +#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h" +#include "esphome/components/xiaomi_ble/xiaomi_ble.h" + +#ifdef ARDUINO_ARCH_ESP32 + +namespace esphome { +namespace xiaomi_mhoc401 { + +class XiaomiMHOC401 : public Component, public esp32_ble_tracker::ESPBTDeviceListener { + public: + void set_address(uint64_t address) { address_ = address; }; + void set_bindkey(const std::string &bindkey); + + bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override; + void dump_config() override; + float get_setup_priority() const override { return setup_priority::DATA; } + void set_temperature(sensor::Sensor *temperature) { temperature_ = temperature; } + void set_humidity(sensor::Sensor *humidity) { humidity_ = humidity; } + void set_battery_level(sensor::Sensor *battery_level) { battery_level_ = battery_level; } + + protected: + uint64_t address_; + uint8_t bindkey_[16]; + sensor::Sensor *temperature_{nullptr}; + sensor::Sensor *humidity_{nullptr}; + sensor::Sensor *battery_level_{nullptr}; +}; + +} // namespace xiaomi_mhoc401 +} // namespace esphome + +#endif From b601cf6bc6d73042e7c16133444286a790fa1896 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 27 Jan 2021 23:04:20 +1300 Subject: [PATCH 4/4] Bump version to v1.16.0b6 --- esphome/const.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/const.py b/esphome/const.py index a96d38eb75..d096a52ab7 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -2,7 +2,7 @@ MAJOR_VERSION = 1 MINOR_VERSION = 16 -PATCH_VERSION = '0b5' +PATCH_VERSION = '0b6' __short_version__ = f'{MAJOR_VERSION}.{MINOR_VERSION}' __version__ = f'{__short_version__}.{PATCH_VERSION}'