From d3f03b7acbf7d2030f21e611585918f3346b7350 Mon Sep 17 00:00:00 2001 From: Alone Date: Sun, 1 Nov 2020 12:24:41 +0800 Subject: [PATCH] add illuminance for xiaomi_mjyd02yla (#1299) * add illuminance for xiaomi_mjyd02yla * add illuminance for xiaomi_mjyd02yla --- esphome/components/xiaomi_mjyd02yla/binary_sensor.py | 6 +++++- esphome/components/xiaomi_mjyd02yla/xiaomi_mjyd02yla.cpp | 3 +++ esphome/components/xiaomi_mjyd02yla/xiaomi_mjyd02yla.h | 2 ++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/esphome/components/xiaomi_mjyd02yla/binary_sensor.py b/esphome/components/xiaomi_mjyd02yla/binary_sensor.py index 72cf57d22c..e13dd77d13 100644 --- a/esphome/components/xiaomi_mjyd02yla/binary_sensor.py +++ b/esphome/components/xiaomi_mjyd02yla/binary_sensor.py @@ -3,7 +3,7 @@ import esphome.config_validation as cv from esphome.components import sensor, binary_sensor, esp32_ble_tracker from esphome.const import CONF_MAC_ADDRESS, CONF_ID, CONF_BINDKEY, \ CONF_DEVICE_CLASS, CONF_LIGHT, CONF_BATTERY_LEVEL, UNIT_PERCENT, ICON_BATTERY, \ - CONF_IDLE_TIME, UNIT_MINUTE, ICON_TIMELAPSE + CONF_IDLE_TIME, CONF_ILLUMINANCE, UNIT_MINUTE, UNIT_LUX, ICON_TIMELAPSE, ICON_BRIGHTNESS_5 DEPENDENCIES = ['esp32_ble_tracker'] AUTO_LOAD = ['xiaomi_ble'] @@ -19,6 +19,7 @@ CONFIG_SCHEMA = cv.All(binary_sensor.BINARY_SENSOR_SCHEMA.extend({ cv.Optional(CONF_DEVICE_CLASS, default='motion'): binary_sensor.device_class, cv.Optional(CONF_IDLE_TIME): sensor.sensor_schema(UNIT_MINUTE, ICON_TIMELAPSE, 0), cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(UNIT_PERCENT, ICON_BATTERY, 0), + cv.Optional(CONF_ILLUMINANCE): sensor.sensor_schema(UNIT_LUX, ICON_BRIGHTNESS_5, 0), cv.Optional(CONF_LIGHT): binary_sensor.BINARY_SENSOR_SCHEMA.extend({ cv.Optional(CONF_DEVICE_CLASS, default='light'): binary_sensor.device_class, }), @@ -40,6 +41,9 @@ def to_code(config): if CONF_BATTERY_LEVEL in config: sens = yield sensor.new_sensor(config[CONF_BATTERY_LEVEL]) cg.add(var.set_battery_level(sens)) + if CONF_ILLUMINANCE in config: + sens = yield sensor.new_sensor(config[CONF_ILLUMINANCE]) + cg.add(var.set_illuminance(sens)) if CONF_LIGHT in config: sens = yield binary_sensor.new_binary_sensor(config[CONF_LIGHT]) cg.add(var.set_light(sens)) diff --git a/esphome/components/xiaomi_mjyd02yla/xiaomi_mjyd02yla.cpp b/esphome/components/xiaomi_mjyd02yla/xiaomi_mjyd02yla.cpp index aaea3606ba..c7a2a75348 100644 --- a/esphome/components/xiaomi_mjyd02yla/xiaomi_mjyd02yla.cpp +++ b/esphome/components/xiaomi_mjyd02yla/xiaomi_mjyd02yla.cpp @@ -14,6 +14,7 @@ void XiaomiMJYD02YLA::dump_config() { LOG_BINARY_SENSOR(" ", "Light", this->is_light_); LOG_SENSOR(" ", "Idle Time", this->idle_time_); LOG_SENSOR(" ", "Battery Level", this->battery_level_); + LOG_SENSOR(" ", "Illuminance", this->illuminance_); } bool XiaomiMJYD02YLA::parse_device(const esp32_ble_tracker::ESPBTDevice &device) { @@ -47,6 +48,8 @@ bool XiaomiMJYD02YLA::parse_device(const esp32_ble_tracker::ESPBTDevice &device) this->idle_time_->publish_state(*res->idle_time); if (res->battery_level.has_value() && this->battery_level_ != nullptr) this->battery_level_->publish_state(*res->battery_level); + if (res->illuminance.has_value() && this->illuminance_ != nullptr) + this->illuminance_->publish_state(*res->illuminance); if (res->is_light.has_value() && this->is_light_ != nullptr) this->is_light_->publish_state(*res->is_light); if (res->has_motion.has_value()) diff --git a/esphome/components/xiaomi_mjyd02yla/xiaomi_mjyd02yla.h b/esphome/components/xiaomi_mjyd02yla/xiaomi_mjyd02yla.h index d3fde4d6f8..973b19a372 100644 --- a/esphome/components/xiaomi_mjyd02yla/xiaomi_mjyd02yla.h +++ b/esphome/components/xiaomi_mjyd02yla/xiaomi_mjyd02yla.h @@ -24,6 +24,7 @@ class XiaomiMJYD02YLA : public Component, float get_setup_priority() const override { return setup_priority::DATA; } void set_idle_time(sensor::Sensor *idle_time) { idle_time_ = idle_time; } void set_battery_level(sensor::Sensor *battery_level) { battery_level_ = battery_level; } + void set_illuminance(sensor::Sensor *illuminance) { illuminance_ = illuminance; } void set_light(binary_sensor::BinarySensor *light) { is_light_ = light; } protected: @@ -31,6 +32,7 @@ class XiaomiMJYD02YLA : public Component, uint8_t bindkey_[16]; sensor::Sensor *idle_time_{nullptr}; sensor::Sensor *battery_level_{nullptr}; + sensor::Sensor *illuminance_{nullptr}; binary_sensor::BinarySensor *is_light_{nullptr}; };