Files
esphome/esphome/components/zephyr_ble_server/__init__.py
T

70 lines
2.2 KiB
Python

from esphome import automation
import esphome.codegen as cg
from esphome.components.nrf52.framework import (
bluetooth_west_projects,
include_west_project,
)
from esphome.components.zephyr import zephyr_add_prj_conf
import esphome.config_validation as cv
from esphome.const import CONF_ID, Framework
from esphome.core import CORE
from esphome.types import ConfigType
zephyr_ble_server_ns = cg.esphome_ns.namespace("zephyr_ble_server")
BLEServer = zephyr_ble_server_ns.class_("BLEServer", cg.Component)
CONF_ON_NUMERIC_COMPARISON_REQUEST = "on_numeric_comparison_request"
CONF_ACCEPT = "accept"
CONFIG_SCHEMA = cv.All(
cv.Schema(
{
cv.GenerateID(): cv.declare_id(BLEServer),
cv.Optional(
CONF_ON_NUMERIC_COMPARISON_REQUEST
): automation.validate_automation({}),
}
).extend(cv.COMPONENT_SCHEMA),
cv.only_with_framework(Framework.ZEPHYR),
)
_CALLBACK_AUTOMATIONS = (
automation.CallbackAutomation(
CONF_ON_NUMERIC_COMPARISON_REQUEST,
"add_passkey_callback",
[(cg.uint32, "passkey")],
),
)
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
zephyr_add_prj_conf("BT", True)
for project in bluetooth_west_projects():
include_west_project(project)
zephyr_add_prj_conf("BT_PERIPHERAL", True)
zephyr_add_prj_conf("BT_RX_STACK_SIZE", 1536)
zephyr_add_prj_conf("BT_DEVICE_NAME", CORE.name)
await cg.register_component(var, config)
if config.get(CONF_ON_NUMERIC_COMPARISON_REQUEST):
zephyr_add_prj_conf("BT_SMP", True)
zephyr_add_prj_conf("BT_SETTINGS", True)
zephyr_add_prj_conf("BT_SMP_SC_ONLY", True)
zephyr_add_prj_conf("BT_KEYS_OVERWRITE_OLDEST", True)
await automation.build_callback_automations(var, config, _CALLBACK_AUTOMATIONS)
BLE_NUMERIC_COMPARISON_REPLY_ACTION_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_ID): cv.use_id(BLEServer),
cv.Required(CONF_ACCEPT): cv.templatable(cv.boolean),
}
)
automation.register_apply_action(
"ble_server.numeric_comparison_reply",
BLE_NUMERIC_COMPARISON_REPLY_ACTION_SCHEMA,
automation.ApplyField(CONF_ACCEPT, "numeric_comparison_reply", cg.bool_),
)