diff --git a/custom_components/bluetti_bt/bluetti_bt_lib/utils/device_builder.py b/custom_components/bluetti_bt/bluetti_bt_lib/utils/device_builder.py index e0e56c7..ed912b2 100644 --- a/custom_components/bluetti_bt/bluetti_bt_lib/utils/device_builder.py +++ b/custom_components/bluetti_bt/bluetti_bt_lib/utils/device_builder.py @@ -53,6 +53,6 @@ def build_device(address: str, name: str): def get_type_by_bt_name(bt_name: str): match = DEVICE_NAME_RE.match(bt_name) - if match is None or len(match) < 2: + if match is None: return "Unknown" return match[1] diff --git a/custom_components/bluetti_bt/config_flow.py b/custom_components/bluetti_bt/config_flow.py index 0d20d13..545ff4e 100644 --- a/custom_components/bluetti_bt/config_flow.py +++ b/custom_components/bluetti_bt/config_flow.py @@ -14,7 +14,7 @@ from homeassistant.components.bluetooth import ( BluetoothServiceInfoBleak, async_discovered_service_info, ) -from homeassistant.const import CONF_ADDRESS, CONF_NAME +from homeassistant.const import CONF_ADDRESS, CONF_NAME, CONF_TYPE from homeassistant.data_entry_flow import FlowResult from homeassistant.helpers import selector @@ -48,11 +48,20 @@ class BluettiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): await self.async_set_unique_id(discovery_info.address) self._abort_if_unique_id_configured() + if isinstance(discovery_info.name, str): + name = re.sub("[^A-Z0-9]+", "", discovery_info.name) + discovery_info.manufacturer_data = { + CONF_TYPE: get_type_by_bt_name(name) + } + # Get device type if needed if isinstance(discovery_info.name, str) and discovery_info.name.startswith("PBOX"): bleak_device = BleakClient(discovery_info.device) device_type = await recognize_device(bleak_device, self.hass.loop.create_future) _LOGGER.info("Device identified as %s", device_type) + discovery_info.manufacturer_data = { + CONF_TYPE: device_type.strip(), + } discovery_info.name = discovery_info.name.replace("PBOX", device_type.strip()) self._discovery_info = discovery_info @@ -76,6 +85,7 @@ class BluettiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): data={ CONF_ADDRESS: discovery_info.address, CONF_NAME: name, + CONF_TYPE: discovery_info.manufacturer_data.get(CONF_TYPE, "Unknown"), }, )