2018-08-13 19:11:33 +02:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2018-11-12 23:30:31 +01:00
|
|
|
from esphomeyaml.components import esp32_ble_tracker, sensor
|
2018-08-13 19:11:33 +02:00
|
|
|
from esphomeyaml.components.esp32_ble_tracker import CONF_ESP32_BLE_ID, ESP32BLETracker, \
|
|
|
|
make_address_array
|
2018-11-12 23:30:31 +01:00
|
|
|
import esphomeyaml.config_validation as cv
|
2018-08-13 19:11:33 +02:00
|
|
|
from esphomeyaml.const import CONF_BATTERY_LEVEL, CONF_HUMIDITY, CONF_MAC_ADDRESS, CONF_MAKE_ID, \
|
|
|
|
CONF_NAME, CONF_TEMPERATURE
|
2019-01-06 19:38:23 +01:00
|
|
|
from esphomeyaml.cpp_generator import get_variable, Pvariable
|
2018-08-13 19:11:33 +02:00
|
|
|
|
|
|
|
DEPENDENCIES = ['esp32_ble_tracker']
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = sensor.PLATFORM_SCHEMA.extend({
|
2018-11-12 23:30:31 +01:00
|
|
|
cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(esp32_ble_tracker.XiaomiDevice),
|
2018-08-13 19:11:33 +02:00
|
|
|
cv.GenerateID(CONF_ESP32_BLE_ID): cv.use_variable_id(ESP32BLETracker),
|
|
|
|
vol.Required(CONF_MAC_ADDRESS): cv.mac_address,
|
2018-11-12 23:30:31 +01:00
|
|
|
vol.Optional(CONF_TEMPERATURE): cv.nameable(esp32_ble_tracker.XIAOMI_SENSOR_SCHEMA),
|
|
|
|
vol.Optional(CONF_HUMIDITY): cv.nameable(esp32_ble_tracker.XIAOMI_SENSOR_SCHEMA),
|
|
|
|
vol.Optional(CONF_BATTERY_LEVEL): cv.nameable(esp32_ble_tracker.XIAOMI_SENSOR_SCHEMA),
|
2018-08-13 19:11:33 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
def to_code(config):
|
|
|
|
for hub in get_variable(config[CONF_ESP32_BLE_ID]):
|
|
|
|
yield
|
2018-10-17 20:56:55 +02:00
|
|
|
rhs = hub.make_xiaomi_device(make_address_array(config[CONF_MAC_ADDRESS]))
|
2018-08-13 19:11:33 +02:00
|
|
|
dev = Pvariable(config[CONF_MAKE_ID], rhs)
|
2018-10-17 20:56:55 +02:00
|
|
|
if CONF_TEMPERATURE in config:
|
|
|
|
conf = config[CONF_TEMPERATURE]
|
|
|
|
sensor.register_sensor(dev.Pmake_temperature_sensor(conf[CONF_NAME]), conf)
|
|
|
|
if CONF_HUMIDITY in config:
|
|
|
|
conf = config[CONF_HUMIDITY]
|
|
|
|
sensor.register_sensor(dev.Pmake_humidity_sensor(conf[CONF_NAME]), conf)
|
2018-08-13 19:11:33 +02:00
|
|
|
if CONF_BATTERY_LEVEL in config:
|
|
|
|
conf = config[CONF_BATTERY_LEVEL]
|
|
|
|
sensor.register_sensor(dev.Pmake_battery_level_sensor(conf[CONF_NAME]), conf)
|
2018-11-03 14:08:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
def to_hass_config(data, config):
|
|
|
|
ret = []
|
|
|
|
for key in (CONF_TEMPERATURE, CONF_HUMIDITY, CONF_BATTERY_LEVEL):
|
|
|
|
if key in config:
|
|
|
|
ret.append(sensor.core_to_hass_config(data, config[key]))
|
|
|
|
return ret
|