mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Reduce Bluetooth setup time (#111304)
This commit is contained in:
parent
dd80157dc7
commit
b9b52b5e6d
@ -16,6 +16,7 @@ from bluetooth_adapters import (
|
|||||||
DEFAULT_ADDRESS,
|
DEFAULT_ADDRESS,
|
||||||
DEFAULT_CONNECTION_SLOTS,
|
DEFAULT_CONNECTION_SLOTS,
|
||||||
AdapterDetails,
|
AdapterDetails,
|
||||||
|
BluetoothAdapters,
|
||||||
adapter_human_name,
|
adapter_human_name,
|
||||||
adapter_model,
|
adapter_model,
|
||||||
adapter_unique_name,
|
adapter_unique_name,
|
||||||
@ -135,27 +136,13 @@ async def _async_get_adapter_from_address(
|
|||||||
return await _get_manager(hass).async_get_adapter_from_address(address)
|
return await _get_manager(hass).async_get_adapter_from_address(address)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
async def _async_start_adapter_discovery(
|
||||||
"""Set up the bluetooth integration."""
|
hass: HomeAssistant,
|
||||||
await passive_update_processor.async_setup(hass)
|
manager: HomeAssistantBluetoothManager,
|
||||||
integration_matcher = IntegrationMatcher(await async_get_bluetooth(hass))
|
bluetooth_adapters: BluetoothAdapters,
|
||||||
integration_matcher.async_setup()
|
) -> None:
|
||||||
bluetooth_adapters = get_adapters()
|
"""Start adapter discovery."""
|
||||||
bluetooth_storage = BluetoothStorage(hass)
|
|
||||||
await bluetooth_storage.async_setup()
|
|
||||||
slot_manager = BleakSlotManager()
|
|
||||||
await slot_manager.async_setup()
|
|
||||||
manager = HomeAssistantBluetoothManager(
|
|
||||||
hass, integration_matcher, bluetooth_adapters, bluetooth_storage, slot_manager
|
|
||||||
)
|
|
||||||
set_manager(manager)
|
|
||||||
await manager.async_setup()
|
|
||||||
hass.bus.async_listen_once(
|
|
||||||
EVENT_HOMEASSISTANT_STOP, hass_callback(lambda event: manager.async_stop())
|
|
||||||
)
|
|
||||||
hass.data[DATA_MANAGER] = models.MANAGER = manager
|
|
||||||
adapters = await manager.async_get_bluetooth_adapters()
|
adapters = await manager.async_get_bluetooth_adapters()
|
||||||
|
|
||||||
async_migrate_entries(hass, adapters, bluetooth_adapters.default_adapter)
|
async_migrate_entries(hass, adapters, bluetooth_adapters.default_adapter)
|
||||||
await async_discover_adapters(hass, adapters)
|
await async_discover_adapters(hass, adapters)
|
||||||
|
|
||||||
@ -212,7 +199,40 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
EVENT_HOMEASSISTANT_STOP, hass_callback(lambda event: cancel())
|
EVENT_HOMEASSISTANT_STOP, hass_callback(lambda event: cancel())
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
|
"""Set up the bluetooth integration."""
|
||||||
|
bluetooth_adapters = get_adapters()
|
||||||
|
bluetooth_storage = BluetoothStorage(hass)
|
||||||
|
slot_manager = BleakSlotManager()
|
||||||
|
integration_matcher = IntegrationMatcher(await async_get_bluetooth(hass))
|
||||||
|
|
||||||
|
slot_manager_setup_task = hass.async_create_task(
|
||||||
|
slot_manager.async_setup(), "slot_manager setup"
|
||||||
|
)
|
||||||
|
processor_setup_task = hass.async_create_task(
|
||||||
|
passive_update_processor.async_setup(hass), "passive_update_processor setup"
|
||||||
|
)
|
||||||
|
storage_setup_task = hass.async_create_task(
|
||||||
|
bluetooth_storage.async_setup(), "bluetooth storage setup"
|
||||||
|
)
|
||||||
|
integration_matcher.async_setup()
|
||||||
|
manager = HomeAssistantBluetoothManager(
|
||||||
|
hass, integration_matcher, bluetooth_adapters, bluetooth_storage, slot_manager
|
||||||
|
)
|
||||||
|
set_manager(manager)
|
||||||
|
|
||||||
|
await storage_setup_task
|
||||||
|
await manager.async_setup()
|
||||||
|
hass.data[DATA_MANAGER] = models.MANAGER = manager
|
||||||
|
|
||||||
|
hass.async_create_background_task(
|
||||||
|
_async_start_adapter_discovery(hass, manager, bluetooth_adapters),
|
||||||
|
"start_adapter_discovery",
|
||||||
|
)
|
||||||
|
await slot_manager_setup_task
|
||||||
async_delete_issue(hass, DOMAIN, "haos_outdated")
|
async_delete_issue(hass, DOMAIN, "haos_outdated")
|
||||||
|
await processor_setup_task
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ from bluetooth_adapters import BluetoothAdapters
|
|||||||
from habluetooth import BaseHaRemoteScanner, BaseHaScanner, BluetoothManager
|
from habluetooth import BaseHaRemoteScanner, BaseHaScanner, BluetoothManager
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.const import EVENT_LOGGING_CHANGED
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP, EVENT_LOGGING_CHANGED
|
||||||
from homeassistant.core import (
|
from homeassistant.core import (
|
||||||
CALLBACK_TYPE,
|
CALLBACK_TYPE,
|
||||||
Event,
|
Event,
|
||||||
@ -136,6 +136,7 @@ class HomeAssistantBluetoothManager(BluetoothManager):
|
|||||||
self._cancel_logging_listener = self.hass.bus.async_listen(
|
self._cancel_logging_listener = self.hass.bus.async_listen(
|
||||||
EVENT_LOGGING_CHANGED, self._async_logging_changed
|
EVENT_LOGGING_CHANGED, self._async_logging_changed
|
||||||
)
|
)
|
||||||
|
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self.async_stop)
|
||||||
seen: set[str] = set()
|
seen: set[str] = set()
|
||||||
for address, service_info in itertools.chain(
|
for address, service_info in itertools.chain(
|
||||||
self._connectable_history.items(), self._all_history.items()
|
self._connectable_history.items(), self._all_history.items()
|
||||||
@ -187,7 +188,7 @@ class HomeAssistantBluetoothManager(BluetoothManager):
|
|||||||
return _async_remove_callback
|
return _async_remove_callback
|
||||||
|
|
||||||
@hass_callback
|
@hass_callback
|
||||||
def async_stop(self) -> None:
|
def async_stop(self, event: Event | None = None) -> None:
|
||||||
"""Stop the Bluetooth integration at shutdown."""
|
"""Stop the Bluetooth integration at shutdown."""
|
||||||
_LOGGER.debug("Stopping bluetooth manager")
|
_LOGGER.debug("Stopping bluetooth manager")
|
||||||
self._async_save_scanner_histories()
|
self._async_save_scanner_histories()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user