Skip to content

Commit

Permalink
add illuminance for xiaomi_mjyd02yla (esphome#1299)
Browse files Browse the repository at this point in the history
* add illuminance for xiaomi_mjyd02yla

* add illuminance for xiaomi_mjyd02yla
  • Loading branch information
al-one authored Nov 1, 2020
1 parent 7e53fc9 commit d3f03b7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion esphome/components/xiaomi_mjyd02yla/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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']
Expand All @@ -19,6 +19,7 @@
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,
}),
Expand All @@ -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))
3 changes: 3 additions & 0 deletions esphome/components/xiaomi_mjyd02yla/xiaomi_mjyd02yla.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 2 additions & 0 deletions esphome/components/xiaomi_mjyd02yla/xiaomi_mjyd02yla.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ 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:
uint64_t address_;
uint8_t bindkey_[16];
sensor::Sensor *idle_time_{nullptr};
sensor::Sensor *battery_level_{nullptr};
sensor::Sensor *illuminance_{nullptr};
binary_sensor::BinarySensor *is_light_{nullptr};
};

Expand Down

0 comments on commit d3f03b7

Please sign in to comment.