added device type detection to config_flow

This commit is contained in:
Patrick762 committed 2024-02-18 16:37:37 +01:00
1 parent 9b7fd82591
commit a50deaae37
4 files changed
+74 -18

No files matched your search

+11 -14
View File
@@ -5,6 +5,7 @@ from __future__ import annotations
import re
import logging
from typing import Any
from bleak import BleakClient
import voluptuous as vol
@@ -13,11 +14,12 @@ from homeassistant.components.bluetooth import (
BluetoothServiceInfoBleak,
async_discovered_service_info,
)
from homeassistant.const import CONF_ADDRESS, CONF_TYPE, CONF_NAME
from homeassistant.const import CONF_ADDRESS, CONF_NAME
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import selector
from .bluetti_bt_lib.utils.device_builder import get_type_by_bt_name
from .bluetti_bt_lib.bluetooth.device_recognizer import recognize_device
from .const import (
CONF_MAX_RETRIES,
@@ -26,7 +28,6 @@ from .const import (
CONF_POLLING_TIMEOUT,
CONF_USE_CONTROLS,
DOMAIN,
SUPPORTED_MODELS,
)
_LOGGER = logging.getLogger(__name__)
@@ -46,6 +47,14 @@ class BluettiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle bluetooth discovery."""
await self.async_set_unique_id(discovery_info.address)
self._abort_if_unique_id_configured()
# 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.name = discovery_info.name.replace("PBOX", device_type.strip())
self._discovery_info = discovery_info
self.context["title_placeholders"] = {"name": discovery_info.name}
return await self.async_step_user()
@@ -60,18 +69,12 @@ class BluettiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
discovery_info = self._discovered_devices[address]
await self.async_set_unique_id(address, raise_on_progress=False)
self._abort_if_unique_id_configured()
dev_type = get_type_by_bt_name(discovery_info.name)
name = re.sub("[^A-Z0-9]+", "", discovery_info.name)
if dev_type == "Unknown":
dev_type = user_input[CONF_TYPE]
name = name.replace("PBOX", dev_type)
return self.async_create_entry(
title=name,
data={
CONF_ADDRESS: discovery_info.address,
CONF_TYPE: dev_type,
CONF_NAME: name,
},
)
@@ -97,12 +100,6 @@ class BluettiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
for service_info in self._discovered_devices.values()
}
),
vol.Optional(CONF_TYPE): vol.In(
{
model: model
for model in SUPPORTED_MODELS
}
),
}
)
return self.async_show_form(