From 321a9a0fa8e0813296dc932f26a5247012fac1b9 Mon Sep 17 00:00:00 2001 From: Rapsssito Date: Sat, 19 Oct 2024 17:45:34 +0200 Subject: [PATCH] Recover hex UUIDs --- .../components/esp32_ble_server/__init__.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/esphome/components/esp32_ble_server/__init__.py b/esphome/components/esp32_ble_server/__init__.py index db4873801a..a6c6c7def5 100644 --- a/esphome/components/esp32_ble_server/__init__.py +++ b/esphome/components/esp32_ble_server/__init__.py @@ -152,7 +152,7 @@ VALUE_EXTRAS_SCHEMA = cv.Schema( DESCRIPTOR_SCHEMA = cv.Schema( { cv.GenerateID(): cv.declare_id(BLEDescriptor), - cv.Required(CONF_UUID): bt_uuid, + cv.Required(CONF_UUID): cv.Any(bt_uuid, cv.hex_uint32_t), cv.Optional(CONF_READ, default=True): cv.boolean, cv.Optional(CONF_WRITE, default=True): cv.boolean, cv.Optional(CONF_ON_WRITE): automation.validate_automation( @@ -167,7 +167,7 @@ SERVICE_CHARACTERISTIC_SCHEMA = ( cv.Schema( { cv.GenerateID(): cv.declare_id(BLECharacteristic), - cv.Required(CONF_UUID): bt_uuid, + cv.Required(CONF_UUID): cv.Any(bt_uuid, cv.hex_uint32_t), cv.Optional(CONF_VALUE): VALUE_SCHEMA, cv.GenerateID(CONF_CHAR_VALUE_ACTION_ID_): cv.declare_id( BLECharacteristicSetValueAction @@ -188,7 +188,7 @@ SERVICE_CHARACTERISTIC_SCHEMA = ( SERVICE_SCHEMA = cv.Schema( { cv.GenerateID(): cv.declare_id(BLEService), - cv.Required(CONF_UUID): bt_uuid, + cv.Required(CONF_UUID): cv.Any(bt_uuid, cv.hex_uint32_t), cv.Optional(CONF_ADVERTISE, default=False): cv.boolean, cv.Optional(CONF_CHARACTERISTICS, default=[]): cv.ensure_list( SERVICE_CHARACTERISTIC_SCHEMA @@ -216,6 +216,13 @@ def parse_properties(char_conf): start=0, ) +def parse_uuid(uuid): + # If the UUID is a int, use from_uint32 + if isinstance(uuid, int): + return ESPBTUUID_ns.from_uint32(uuid) + # Otherwise, use ESPBTUUID_ns.from_raw + return ESPBTUUID_ns.from_raw(uuid) + def bytebuffer_parser_(value, str_encoding): for val_method, casting in zip( @@ -301,7 +308,7 @@ async def to_code_descriptor(descriptor_conf, char_var): ) desc_var = cg.new_Pvariable( descriptor_conf[CONF_ID], - ESPBTUUID_ns.from_raw(descriptor_conf[CONF_UUID]), + parse_uuid(descriptor_conf[CONF_UUID]), value.get_capacity(), descriptor_conf[CONF_READ], descriptor_conf[CONF_WRITE], @@ -321,7 +328,7 @@ async def to_code_characteristic(service_var, char_conf): char_var = cg.Pvariable( char_conf[CONF_ID], service_var.create_characteristic( - ESPBTUUID_ns.from_raw(char_conf[CONF_UUID]), + parse_uuid(char_conf[CONF_UUID]), parse_properties(char_conf), ), ) @@ -371,7 +378,7 @@ async def to_code(config): service_var = cg.Pvariable( service_config[CONF_ID], var.create_service( - ESPBTUUID_ns.from_raw(service_config[CONF_UUID]), + parse_uuid(service_config[CONF_UUID]), service_config[CONF_ADVERTISE], num_handles, ),