esphome/esphomeyaml/components/binary_sensor/esp32_ble.py

49 lines
1.6 KiB
Python
Raw Normal View History

2018-05-14 11:50:56 +02:00
import voluptuous as vol
import esphomeyaml.config_validation as cv
from esphomeyaml.components import binary_sensor
2018-05-20 12:41:52 +02:00
from esphomeyaml.components.esp32_ble import ESP32BLETracker
from esphomeyaml.const import CONF_MAC_ADDRESS, CONF_NAME, ESP_PLATFORM_ESP32
2018-05-14 11:50:56 +02:00
from esphomeyaml.core import HexInt, MACAddress
2018-05-20 12:41:52 +02:00
from esphomeyaml.helpers import ArrayInitializer, get_variable
2018-05-14 11:50:56 +02:00
ESP_PLATFORMS = [ESP_PLATFORM_ESP32]
DEPENDENCIES = ['esp32_ble']
2018-06-02 22:22:20 +02:00
CONF_ESP32_BLE_ID = 'esp32_ble_id'
2018-05-14 11:50:56 +02:00
def validate_mac(value):
value = cv.string_strict(value)
parts = value.split(':')
if len(parts) != 6:
raise vol.Invalid("MAC Address must consist of 6 : (colon) separated parts")
parts_int = []
if any(len(part) != 2 for part in parts):
raise vol.Invalid("MAC Address must be format XX:XX:XX:XX:XX:XX")
for part in parts:
try:
parts_int.append(int(part, 16))
except ValueError:
raise vol.Invalid("MAC Address parts must be hexadecimal values from 00 to FF")
return MACAddress(*parts_int)
PLATFORM_SCHEMA = binary_sensor.PLATFORM_SCHEMA.extend({
vol.Required(CONF_MAC_ADDRESS): validate_mac,
2018-06-02 22:22:20 +02:00
cv.GenerateID(CONF_ESP32_BLE_ID): cv.use_variable_id(ESP32BLETracker)
2018-05-20 12:41:52 +02:00
}).extend(binary_sensor.BINARY_SENSOR_SCHEMA.schema)
2018-05-14 11:50:56 +02:00
def to_code(config):
2018-06-02 22:22:20 +02:00
hub = None
for hub in get_variable(CONF_ESP32_BLE_ID):
yield
2018-05-14 11:50:56 +02:00
addr = [HexInt(i) for i in config[CONF_MAC_ADDRESS].parts]
rhs = hub.make_device(config[CONF_NAME], ArrayInitializer(*addr, multiline=False))
2018-06-03 07:11:11 +02:00
binary_sensor.register_binary_sensor(rhs, config)
2018-05-14 11:50:56 +02:00
BUILD_FLAGS = '-DUSE_ESP32_BLE_TRACKER'