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 <gx@m15.cn>
Co-authored-by: mr G1K <mr@g1k.ru>
This commit is contained in:
junnikokuki 2019-08-28 03:06:39 +08:00 committed by Otto Winter
parent 9770bc371b
commit 65d08beaa4
6 changed files with 115 additions and 4 deletions

View file

@ -83,8 +83,9 @@ optional<XiaomiParseResult> parse_xiaomi(const esp32_ble_tracker::ESPBTDevice &d
bool is_mijia = (raw[1] & 0x20) == 0x20 && raw[2] == 0xAA && raw[3] == 0x01; 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_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"); // ESP_LOGVV(TAG, "Xiaomi no magic bytes");
return {}; return {};
} }
@ -101,7 +102,12 @@ optional<XiaomiParseResult> parse_xiaomi(const esp32_ble_tracker::ESPBTDevice &d
return {}; return {};
} }
XiaomiParseResult result; 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); bool success = parse_xiaomi_data_byte(raw_type, data, data_length, result);
if (!success) if (!success)
return {}; return {};
@ -113,7 +119,12 @@ bool XiaomiListener::parse_device(const esp32_ble_tracker::ESPBTDevice &device)
if (!res.has_value()) if (!res.has_value())
return false; 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()); ESP_LOGD(TAG, "Got Xiaomi %s (%s):", name, device.address_str().c_str());

View file

@ -9,7 +9,7 @@ namespace esphome {
namespace xiaomi_ble { namespace xiaomi_ble {
struct XiaomiParseResult { struct XiaomiParseResult {
enum { TYPE_MIJIA, TYPE_MIFLORA } type; enum { TYPE_MIJIA, TYPE_MIFLORA, TYPE_LYWSD02 } type;
optional<float> temperature; optional<float> temperature;
optional<float> humidity; optional<float> humidity;
optional<float> battery_level; optional<float> battery_level;

View file

@ -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))

View file

@ -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

View file

@ -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