-
Notifications
You must be signed in to change notification settings - Fork 26
/
__init__.py
60 lines (51 loc) · 1.92 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import esp32_ble_tracker
from esphome import automation
from esphome.const import (
CONF_ID,
CONF_DISCOVERY,
CONF_MAC_ADDRESS,
CONF_TRIGGER_ID,
CONF_ON_BLE_ADVERTISE,
)
CODEOWNERS = ["@myhomeiot"]
DEPENDENCIES = ["esp32_ble_tracker"]
CONF_DEVICES = "devices"
ble_gateway_ns = cg.esphome_ns.namespace("ble_gateway")
BLEGateway = ble_gateway_ns.class_(
"BLEGateway", esp32_ble_tracker.ESPBTDeviceListener, cg.Component
)
# Triggers
BLEGatewayBLEAdvertiseTrigger = ble_gateway_ns.class_(
"BLEGatewayBLEAdvertiseTrigger", automation.Trigger.template(cg.std_string),
)
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(BLEGateway),
cv.Optional(CONF_DISCOVERY, default=False): cv.boolean,
cv.Optional(CONF_DEVICES): cv.All(
cv.ensure_list(
{
cv.Required(CONF_MAC_ADDRESS): cv.mac_address,
}
),
cv.Length(min=1),
),
cv.Required(CONF_ON_BLE_ADVERTISE): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(BLEGatewayBLEAdvertiseTrigger),
}
),
}
).extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await esp32_ble_tracker.register_ble_device(var, config)
cg.add(var.set_discovery(config[CONF_DISCOVERY]))
if config.get(CONF_DEVICES):
cg.add(var.set_devices("".join(f"{str(conf[CONF_MAC_ADDRESS]).replace(':', '')}" for conf in config[CONF_DEVICES])))
for conf in config.get(CONF_ON_BLE_ADVERTISE):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [(esp32_ble_tracker.ESPBTDeviceConstRef, "x"), (cg.std_string, "packet")], conf)