mirror of
https://github.com/esphome/esphome.git
synced 2024-11-23 11:56:25 +01:00
49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
import voluptuous as vol
|
|
|
|
import esphomeyaml.config_validation as cv
|
|
from esphomeyaml.components import binary_sensor
|
|
from esphomeyaml.components.esp32_ble import ESP32BLETracker
|
|
from esphomeyaml.const import CONF_MAC_ADDRESS, CONF_NAME, ESP_PLATFORM_ESP32
|
|
from esphomeyaml.core import HexInt, MACAddress
|
|
from esphomeyaml.helpers import ArrayInitializer, get_variable
|
|
|
|
ESP_PLATFORMS = [ESP_PLATFORM_ESP32]
|
|
DEPENDENCIES = ['esp32_ble']
|
|
|
|
CONF_ESP32_BLE_ID = 'esp32_ble_id'
|
|
|
|
|
|
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,
|
|
cv.GenerateID(CONF_ESP32_BLE_ID): cv.use_variable_id(ESP32BLETracker)
|
|
}).extend(binary_sensor.BINARY_SENSOR_SCHEMA.schema)
|
|
|
|
|
|
def to_code(config):
|
|
hub = None
|
|
for hub in get_variable(config[CONF_ESP32_BLE_ID]):
|
|
yield
|
|
addr = [HexInt(i) for i in config[CONF_MAC_ADDRESS].parts]
|
|
rhs = hub.make_device(config[CONF_NAME], ArrayInitializer(*addr, multiline=False))
|
|
binary_sensor.register_binary_sensor(rhs, config)
|
|
|
|
|
|
BUILD_FLAGS = '-DUSE_ESP32_BLE_TRACKER'
|