From 65d08beaa4ca4fe9ab19a1e108f6af0eb45941ab Mon Sep 17 00:00:00 2001 From: junnikokuki Date: Wed, 28 Aug 2019 03:06:39 +0800 Subject: [PATCH] add xiaomi BLE Thermometer lywsd02 model support (#664) * add xiaomi BLE Thermometer lywsd02 model support * remove battery level * Update sensor.py to pass the lint test https://github.com/esphome/esphome/pull/664 * fix trailing space Co-authored-by: Guoxue Co-authored-by: mr G1K --- esphome/components/xiaomi_ble/xiaomi_ble.cpp | 17 +++++-- esphome/components/xiaomi_ble/xiaomi_ble.h | 2 +- esphome/components/xiaomi_lywsd02/__init__.py | 0 esphome/components/xiaomi_lywsd02/sensor.py | 34 ++++++++++++++ .../xiaomi_lywsd02/xiaomi_lywsd02.cpp | 20 ++++++++ .../xiaomi_lywsd02/xiaomi_lywsd02.h | 46 +++++++++++++++++++ 6 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 esphome/components/xiaomi_lywsd02/__init__.py create mode 100644 esphome/components/xiaomi_lywsd02/sensor.py create mode 100644 esphome/components/xiaomi_lywsd02/xiaomi_lywsd02.cpp create mode 100644 esphome/components/xiaomi_lywsd02/xiaomi_lywsd02.h diff --git a/esphome/components/xiaomi_ble/xiaomi_ble.cpp b/esphome/components/xiaomi_ble/xiaomi_ble.cpp index 4367b5fd1e..5bb6709e5f 100644 --- a/esphome/components/xiaomi_ble/xiaomi_ble.cpp +++ b/esphome/components/xiaomi_ble/xiaomi_ble.cpp @@ -83,8 +83,9 @@ optional parse_xiaomi(const esp32_ble_tracker::ESPBTDevice &d bool is_mijia = (raw[1] & 0x20) == 0x20 && raw[2] == 0xAA && raw[3] == 0x01; bool is_miflora = (raw[1] & 0x20) == 0x20 && raw[2] == 0x98 && raw[3] == 0x00; + bool is_lywsd02 = (raw[1] & 0x20) == 0x20 && raw[2] == 0x5b && raw[3] == 0x04; - if (!is_mijia && !is_miflora) { + if (!is_mijia && !is_miflora && !is_lywsd02) { // ESP_LOGVV(TAG, "Xiaomi no magic bytes"); return {}; } @@ -101,7 +102,12 @@ optional parse_xiaomi(const esp32_ble_tracker::ESPBTDevice &d return {}; } XiaomiParseResult result; - result.type = is_miflora ? XiaomiParseResult::TYPE_MIFLORA : XiaomiParseResult::TYPE_MIJIA; + result.type = XiaomiParseResult::TYPE_MIFLORA; + if (is_mijia) { + result.type = XiaomiParseResult::TYPE_MIJIA; + } else if (is_lywsd02) { + result.type = XiaomiParseResult::TYPE_LYWSD02; + } bool success = parse_xiaomi_data_byte(raw_type, data, data_length, result); if (!success) return {}; @@ -113,7 +119,12 @@ bool XiaomiListener::parse_device(const esp32_ble_tracker::ESPBTDevice &device) if (!res.has_value()) return false; - const char *name = res->type == XiaomiParseResult::TYPE_MIFLORA ? "Mi Flora" : "Mi Jia"; + const char *name = "Mi Flora"; + if (res->type == XiaomiParseResult::TYPE_MIJIA) { + name = "Mi Jia"; + } else if (res->type == XiaomiParseResult::TYPE_LYWSD02) { + name = "LYWSD02"; + } ESP_LOGD(TAG, "Got Xiaomi %s (%s):", name, device.address_str().c_str()); diff --git a/esphome/components/xiaomi_ble/xiaomi_ble.h b/esphome/components/xiaomi_ble/xiaomi_ble.h index 058a89927b..b8b602ecef 100644 --- a/esphome/components/xiaomi_ble/xiaomi_ble.h +++ b/esphome/components/xiaomi_ble/xiaomi_ble.h @@ -9,7 +9,7 @@ namespace esphome { namespace xiaomi_ble { struct XiaomiParseResult { - enum { TYPE_MIJIA, TYPE_MIFLORA } type; + enum { TYPE_MIJIA, TYPE_MIFLORA, TYPE_LYWSD02 } type; optional temperature; optional humidity; optional battery_level; diff --git a/esphome/components/xiaomi_lywsd02/__init__.py b/esphome/components/xiaomi_lywsd02/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/esphome/components/xiaomi_lywsd02/sensor.py b/esphome/components/xiaomi_lywsd02/sensor.py new file mode 100644 index 0000000000..8e4d59316b --- /dev/null +++ b/esphome/components/xiaomi_lywsd02/sensor.py @@ -0,0 +1,34 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import sensor, esp32_ble_tracker +from esphome.const import CONF_HUMIDITY, CONF_MAC_ADDRESS, CONF_TEMPERATURE, \ + UNIT_CELSIUS, ICON_THERMOMETER, UNIT_PERCENT, ICON_WATER_PERCENT, CONF_ID + +DEPENDENCIES = ['esp32_ble_tracker'] +AUTO_LOAD = ['xiaomi_ble'] + +xiaomi_lywsd02_ns = cg.esphome_ns.namespace('xiaomi_lywsd02') +XiaomiLYWSD02 = xiaomi_lywsd02_ns.class_('XiaomiLYWSD02', esp32_ble_tracker.ESPBTDeviceListener, + cg.Component) + +CONFIG_SCHEMA = cv.Schema({ + cv.GenerateID(): cv.declare_id(XiaomiLYWSD02), + 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, 1), +}).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)) + + 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)) diff --git a/esphome/components/xiaomi_lywsd02/xiaomi_lywsd02.cpp b/esphome/components/xiaomi_lywsd02/xiaomi_lywsd02.cpp new file mode 100644 index 0000000000..cd77c133a5 --- /dev/null +++ b/esphome/components/xiaomi_lywsd02/xiaomi_lywsd02.cpp @@ -0,0 +1,20 @@ +#include "xiaomi_lywsd02.h" +#include "esphome/core/log.h" + +#ifdef ARDUINO_ARCH_ESP32 + +namespace esphome { +namespace xiaomi_lywsd02 { + +static const char *TAG = "xiaomi_lywsd02"; + +void XiaomiLYWSD02::dump_config() { + ESP_LOGCONFIG(TAG, "Xiaomi LYWSD02"); + LOG_SENSOR(" ", "Temperature", this->temperature_); + LOG_SENSOR(" ", "Humidity", this->humidity_); +} + +} // namespace xiaomi_lywsd02 +} // namespace esphome + +#endif diff --git a/esphome/components/xiaomi_lywsd02/xiaomi_lywsd02.h b/esphome/components/xiaomi_lywsd02/xiaomi_lywsd02.h new file mode 100644 index 0000000000..9b8aba1bb0 --- /dev/null +++ b/esphome/components/xiaomi_lywsd02/xiaomi_lywsd02.h @@ -0,0 +1,46 @@ +#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_lywsd02 { + +class XiaomiLYWSD02 : 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 { + if (device.address_uint64() != this->address_) + return false; + + auto res = xiaomi_ble::parse_xiaomi(device); + if (!res.has_value()) + return false; + + 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); + return true; + } + + 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; } + + protected: + uint64_t address_; + sensor::Sensor *temperature_{nullptr}; + sensor::Sensor *humidity_{nullptr}; +}; + +} // namespace xiaomi_lywsd02 +} // namespace esphome + +#endif