add illuminance for xiaomi_mjyd02yla (#1299)

* add illuminance for xiaomi_mjyd02yla

* add illuminance for xiaomi_mjyd02yla
This commit is contained in:
Alone 2020-11-01 12:24:41 +08:00 committed by GitHub
parent 7e53fc9d6a
commit d3f03b7acb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 1 deletions

View file

@ -3,7 +3,7 @@ import esphome.config_validation as cv
from esphome.components import sensor, binary_sensor, esp32_ble_tracker from esphome.components import sensor, binary_sensor, esp32_ble_tracker
from esphome.const import CONF_MAC_ADDRESS, CONF_ID, CONF_BINDKEY, \ from esphome.const import CONF_MAC_ADDRESS, CONF_ID, CONF_BINDKEY, \
CONF_DEVICE_CLASS, CONF_LIGHT, CONF_BATTERY_LEVEL, UNIT_PERCENT, ICON_BATTERY, \ 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'] DEPENDENCIES = ['esp32_ble_tracker']
AUTO_LOAD = ['xiaomi_ble'] 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_DEVICE_CLASS, default='motion'): binary_sensor.device_class,
cv.Optional(CONF_IDLE_TIME): sensor.sensor_schema(UNIT_MINUTE, ICON_TIMELAPSE, 0), 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_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_LIGHT): binary_sensor.BINARY_SENSOR_SCHEMA.extend({
cv.Optional(CONF_DEVICE_CLASS, default='light'): binary_sensor.device_class, 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: if CONF_BATTERY_LEVEL in config:
sens = yield sensor.new_sensor(config[CONF_BATTERY_LEVEL]) sens = yield sensor.new_sensor(config[CONF_BATTERY_LEVEL])
cg.add(var.set_battery_level(sens)) 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: if CONF_LIGHT in config:
sens = yield binary_sensor.new_binary_sensor(config[CONF_LIGHT]) sens = yield binary_sensor.new_binary_sensor(config[CONF_LIGHT])
cg.add(var.set_light(sens)) cg.add(var.set_light(sens))

View file

@ -14,6 +14,7 @@ void XiaomiMJYD02YLA::dump_config() {
LOG_BINARY_SENSOR(" ", "Light", this->is_light_); LOG_BINARY_SENSOR(" ", "Light", this->is_light_);
LOG_SENSOR(" ", "Idle Time", this->idle_time_); LOG_SENSOR(" ", "Idle Time", this->idle_time_);
LOG_SENSOR(" ", "Battery Level", this->battery_level_); LOG_SENSOR(" ", "Battery Level", this->battery_level_);
LOG_SENSOR(" ", "Illuminance", this->illuminance_);
} }
bool XiaomiMJYD02YLA::parse_device(const esp32_ble_tracker::ESPBTDevice &device) { 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); this->idle_time_->publish_state(*res->idle_time);
if (res->battery_level.has_value() && this->battery_level_ != nullptr) if (res->battery_level.has_value() && this->battery_level_ != nullptr)
this->battery_level_->publish_state(*res->battery_level); 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) if (res->is_light.has_value() && this->is_light_ != nullptr)
this->is_light_->publish_state(*res->is_light); this->is_light_->publish_state(*res->is_light);
if (res->has_motion.has_value()) if (res->has_motion.has_value())

View file

@ -24,6 +24,7 @@ class XiaomiMJYD02YLA : public Component,
float get_setup_priority() const override { return setup_priority::DATA; } float get_setup_priority() const override { return setup_priority::DATA; }
void set_idle_time(sensor::Sensor *idle_time) { idle_time_ = idle_time; } 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_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; } void set_light(binary_sensor::BinarySensor *light) { is_light_ = light; }
protected: protected:
@ -31,6 +32,7 @@ class XiaomiMJYD02YLA : public Component,
uint8_t bindkey_[16]; uint8_t bindkey_[16];
sensor::Sensor *idle_time_{nullptr}; sensor::Sensor *idle_time_{nullptr};
sensor::Sensor *battery_level_{nullptr}; sensor::Sensor *battery_level_{nullptr};
sensor::Sensor *illuminance_{nullptr};
binary_sensor::BinarySensor *is_light_{nullptr}; binary_sensor::BinarySensor *is_light_{nullptr};
}; };