From 0f3d4d9a4703a4aedd7908ce89272599e9f3727b Mon Sep 17 00:00:00 2001 From: drug123 Date: Wed, 26 Jan 2022 23:54:29 +0000 Subject: [PATCH] Add Xiaomi MHOC303 sensor e-ink clock (#3115) --- CODEOWNERS | 1 + esphome/components/xiaomi_ble/xiaomi_ble.cpp | 3 + esphome/components/xiaomi_ble/xiaomi_ble.h | 1 + esphome/components/xiaomi_mhoc303/__init__.py | 1 + esphome/components/xiaomi_mhoc303/sensor.py | 73 +++++++++++++++++++ .../xiaomi_mhoc303/xiaomi_mhoc303.cpp | 59 +++++++++++++++ .../xiaomi_mhoc303/xiaomi_mhoc303.h | 35 +++++++++ tests/test2.yaml | 8 ++ 8 files changed, 181 insertions(+) create mode 100644 esphome/components/xiaomi_mhoc303/__init__.py create mode 100644 esphome/components/xiaomi_mhoc303/sensor.py create mode 100644 esphome/components/xiaomi_mhoc303/xiaomi_mhoc303.cpp create mode 100644 esphome/components/xiaomi_mhoc303/xiaomi_mhoc303.h diff --git a/CODEOWNERS b/CODEOWNERS index 6a46732dc1..f075cc8649 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -204,5 +204,6 @@ esphome/components/wake_on_lan/* @willwill2will54 esphome/components/web_server_base/* @OttoWinter esphome/components/whirlpool/* @glmnet esphome/components/xiaomi_lywsd03mmc/* @ahpohl +esphome/components/xiaomi_mhoc303/* @drug123 esphome/components/xiaomi_mhoc401/* @vevsvevs esphome/components/xpt2046/* @numo68 diff --git a/esphome/components/xiaomi_ble/xiaomi_ble.cpp b/esphome/components/xiaomi_ble/xiaomi_ble.cpp index 583b68a77b..bdd745b859 100644 --- a/esphome/components/xiaomi_ble/xiaomi_ble.cpp +++ b/esphome/components/xiaomi_ble/xiaomi_ble.cpp @@ -198,6 +198,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] == 0xd3) && (raw[3] == 0x06)) { // rectangular body, e-ink display with alarm + result.type = XiaomiParseResult::TYPE_MHOC303; + result.name = "MHOC303"; } else if ((raw[2] == 0x87) && (raw[3] == 0x03)) { // square body, e-ink display result.type = XiaomiParseResult::TYPE_MHOC401; result.name = "MHOC401"; diff --git a/esphome/components/xiaomi_ble/xiaomi_ble.h b/esphome/components/xiaomi_ble/xiaomi_ble.h index 54ab9a144f..ee65d7c82f 100644 --- a/esphome/components/xiaomi_ble/xiaomi_ble.h +++ b/esphome/components/xiaomi_ble/xiaomi_ble.h @@ -23,6 +23,7 @@ struct XiaomiParseResult { TYPE_MUE4094RT, TYPE_WX08ZM, TYPE_MJYD02YLA, + TYPE_MHOC303, TYPE_MHOC401, TYPE_CGPR1 } type; diff --git a/esphome/components/xiaomi_mhoc303/__init__.py b/esphome/components/xiaomi_mhoc303/__init__.py new file mode 100644 index 0000000000..e7005b3957 --- /dev/null +++ b/esphome/components/xiaomi_mhoc303/__init__.py @@ -0,0 +1 @@ +CODEOWNERS = ["@drug123"] diff --git a/esphome/components/xiaomi_mhoc303/sensor.py b/esphome/components/xiaomi_mhoc303/sensor.py new file mode 100644 index 0000000000..18f5ad7764 --- /dev/null +++ b/esphome/components/xiaomi_mhoc303/sensor.py @@ -0,0 +1,73 @@ +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, + DEVICE_CLASS_TEMPERATURE, + ENTITY_CATEGORY_DIAGNOSTIC, + STATE_CLASS_MEASUREMENT, + UNIT_CELSIUS, + UNIT_PERCENT, + DEVICE_CLASS_HUMIDITY, + DEVICE_CLASS_BATTERY, + CONF_ID, +) + +DEPENDENCIES = ["esp32_ble_tracker"] +AUTO_LOAD = ["xiaomi_ble"] + +xiaomi_mhoc303_ns = cg.esphome_ns.namespace("xiaomi_mhoc303") +XiaomiMHOC303 = xiaomi_mhoc303_ns.class_( + "XiaomiMHOC303", esp32_ble_tracker.ESPBTDeviceListener, cg.Component +) + +CONFIG_SCHEMA = ( + cv.Schema( + { + cv.GenerateID(): cv.declare_id(XiaomiMHOC303), + cv.Required(CONF_MAC_ADDRESS): cv.mac_address, + cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( + unit_of_measurement=UNIT_CELSIUS, + accuracy_decimals=1, + device_class=DEVICE_CLASS_TEMPERATURE, + state_class=STATE_CLASS_MEASUREMENT, + ), + cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( + unit_of_measurement=UNIT_PERCENT, + accuracy_decimals=1, + device_class=DEVICE_CLASS_HUMIDITY, + state_class=STATE_CLASS_MEASUREMENT, + ), + cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema( + unit_of_measurement=UNIT_PERCENT, + accuracy_decimals=0, + device_class=DEVICE_CLASS_BATTERY, + state_class=STATE_CLASS_MEASUREMENT, + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, + ), + } + ) + .extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA) + .extend(cv.COMPONENT_SCHEMA) +) + + +async def to_code(config): + var = cg.new_Pvariable(config[CONF_ID]) + await cg.register_component(var, config) + await esp32_ble_tracker.register_ble_device(var, config) + + cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex)) + + if CONF_TEMPERATURE in config: + sens = await sensor.new_sensor(config[CONF_TEMPERATURE]) + cg.add(var.set_temperature(sens)) + if CONF_HUMIDITY in config: + sens = await sensor.new_sensor(config[CONF_HUMIDITY]) + cg.add(var.set_humidity(sens)) + if CONF_BATTERY_LEVEL in config: + sens = await sensor.new_sensor(config[CONF_BATTERY_LEVEL]) + cg.add(var.set_battery_level(sens)) diff --git a/esphome/components/xiaomi_mhoc303/xiaomi_mhoc303.cpp b/esphome/components/xiaomi_mhoc303/xiaomi_mhoc303.cpp new file mode 100644 index 0000000000..e613faec7e --- /dev/null +++ b/esphome/components/xiaomi_mhoc303/xiaomi_mhoc303.cpp @@ -0,0 +1,59 @@ +#include "xiaomi_mhoc303.h" +#include "esphome/core/log.h" + +#ifdef USE_ESP32 + +namespace esphome { +namespace xiaomi_mhoc303 { + +static const char *const TAG = "xiaomi_mhoc303"; + +void XiaomiMHOC303::dump_config() { + ESP_LOGCONFIG(TAG, "Xiaomi MHOC303"); + LOG_SENSOR(" ", "Temperature", this->temperature_); + LOG_SENSOR(" ", "Humidity", this->humidity_); + LOG_SENSOR(" ", "Battery Level", this->battery_level_); +} + +bool XiaomiMHOC303::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) { + ESP_LOGVV(TAG, "parse_device(): payload decryption is currently not supported on this device."); + continue; + } + if (!(xiaomi_ble::parse_xiaomi_message(service_data.data, *res))) { + continue; + } + 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; + } + + return success; +} + +} // namespace xiaomi_mhoc303 +} // namespace esphome + +#endif diff --git a/esphome/components/xiaomi_mhoc303/xiaomi_mhoc303.h b/esphome/components/xiaomi_mhoc303/xiaomi_mhoc303.h new file mode 100644 index 0000000000..d0304f7894 --- /dev/null +++ b/esphome/components/xiaomi_mhoc303/xiaomi_mhoc303.h @@ -0,0 +1,35 @@ +#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 USE_ESP32 + +namespace esphome { +namespace xiaomi_mhoc303 { + +class XiaomiMHOC303 : public Component, public esp32_ble_tracker::ESPBTDeviceListener { + public: + void set_address(uint64_t address) { address_ = address; } + + 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_; + sensor::Sensor *temperature_{nullptr}; + sensor::Sensor *humidity_{nullptr}; + sensor::Sensor *battery_level_{nullptr}; +}; + +} // namespace xiaomi_mhoc303 +} // namespace esphome + +#endif diff --git a/tests/test2.yaml b/tests/test2.yaml index 7920bf3fe3..6d606a3143 100644 --- a/tests/test2.yaml +++ b/tests/test2.yaml @@ -227,6 +227,14 @@ sensor: name: 'JQJCY01YM Formaldehyde' battery_level: name: 'JQJCY01YM Battery Level' + - platform: xiaomi_mhoc303 + mac_address: 'E7:50:59:32:A0:1C' + temperature: + name: 'MHO-C303 Temperature' + humidity: + name: 'MHO-C303 Humidity' + battery_level: + name: 'MHO-C303 Battery Level' - platform: atc_mithermometer mac_address: 'A4:C1:38:4E:16:78' temperature: