esphome/components/esp32_nimble_mqtt_room/__init__.py
2023-12-06 21:01:11 +03:00

41 lines
1.4 KiB
Python

import esphome.config_validation as cv
import esphome.codegen as cg
from esphome.const import (
CONF_ID,
)
from esphome.components import mqtt, nimble_tracker
DEPENDENCIES = ["mqtt"]
AUTO_LOAD=["nimble_distance_custom"]
CONF_NIMBLE_ID = "esp32_nimble_mqtt_room"
CONF_ROOM_KEY = 'room'
CONF_BASE_TOPIC_KEY = 'base_topic'
CONF_MAC_KEY = 'mac_addr'
CONF_MAX_DISTANCE = 'max_distance'
esp32_nimble_tracker_ns = cg.esphome_ns.namespace("esp32_nimble_mqtt_room")
ESP32NimbleMQTTRoom = esp32_nimble_tracker_ns.class_(
"ESP32NimbleMQTTRoom", cg.Component, nimble_tracker.NimbleDeviceListener
)
CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(ESP32NimbleMQTTRoom),
cv.Required(CONF_ROOM_KEY): cv.string,
cv.Required(CONF_MAC_KEY): cv.All(cv.ensure_list(cv.string)),
cv.Optional(CONF_MAX_DISTANCE, default=16.0): cv.float_,
cv.Optional(CONF_BASE_TOPIC_KEY, default="esphome_presense"): cv.string,
}).extend(cv.COMPONENT_SCHEMA).extend(nimble_tracker.NIMBLE_DEVICE_LISTENER_SCHEMA)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
cg.add(var.set_room(config[CONF_ROOM_KEY]))
cg.add(var.set_base_topic(config[CONF_BASE_TOPIC_KEY]))
cg.add(var.set_addresses(config[CONF_MAC_KEY]))
cg.add(var.set_max_distance(config[CONF_MAX_DISTANCE]))
await nimble_tracker.device_listener_to_code(var, config)
await nimble_tracker.register_ble_device(var, config)