mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Fix Bluetooth failover when esphome device unexpectedly disconnects (#79769)
This commit is contained in:
parent
22d6ce967d
commit
07d4ac42d4
@ -1,6 +1,7 @@
|
|||||||
"""Bluetooth support for esphome."""
|
"""Bluetooth support for esphome."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from aioesphomeapi import APIClient
|
from aioesphomeapi import APIClient
|
||||||
@ -11,14 +12,8 @@ from homeassistant.components.bluetooth import (
|
|||||||
async_register_scanner,
|
async_register_scanner,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import (
|
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback as hass_callback
|
||||||
CALLBACK_TYPE,
|
|
||||||
HomeAssistant,
|
|
||||||
async_get_hass,
|
|
||||||
callback as hass_callback,
|
|
||||||
)
|
|
||||||
|
|
||||||
from ..domain_data import DomainData
|
|
||||||
from ..entry_data import RuntimeEntryData
|
from ..entry_data import RuntimeEntryData
|
||||||
from .client import ESPHomeClient
|
from .client import ESPHomeClient
|
||||||
from .scanner import ESPHomeScanner
|
from .scanner import ESPHomeScanner
|
||||||
@ -27,18 +22,23 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
@hass_callback
|
@hass_callback
|
||||||
def async_can_connect(source: str) -> bool:
|
def _async_can_connect_factory(
|
||||||
"""Check if a given source can make another connection."""
|
entry_data: RuntimeEntryData, source: str
|
||||||
domain_data = DomainData.get(async_get_hass())
|
) -> Callable[[], bool]:
|
||||||
entry = domain_data.get_by_unique_id(source)
|
"""Create a can_connect function for a specific RuntimeEntryData instance."""
|
||||||
entry_data = domain_data.get_entry_data(entry)
|
|
||||||
_LOGGER.debug(
|
@hass_callback
|
||||||
"Checking if %s can connect, available=%s, ble_connections_free=%s",
|
def _async_can_connect() -> bool:
|
||||||
source,
|
"""Check if a given source can make another connection."""
|
||||||
entry_data.available,
|
_LOGGER.debug(
|
||||||
entry_data.ble_connections_free,
|
"Checking if %s can connect, available=%s, ble_connections_free=%s",
|
||||||
)
|
source,
|
||||||
return bool(entry_data.available and entry_data.ble_connections_free)
|
entry_data.available,
|
||||||
|
entry_data.ble_connections_free,
|
||||||
|
)
|
||||||
|
return bool(entry_data.available and entry_data.ble_connections_free)
|
||||||
|
|
||||||
|
return _async_can_connect
|
||||||
|
|
||||||
|
|
||||||
async def async_connect_scanner(
|
async def async_connect_scanner(
|
||||||
@ -63,7 +63,7 @@ async def async_connect_scanner(
|
|||||||
connector = HaBluetoothConnector(
|
connector = HaBluetoothConnector(
|
||||||
client=ESPHomeClient,
|
client=ESPHomeClient,
|
||||||
source=source,
|
source=source,
|
||||||
can_connect=lambda: async_can_connect(source),
|
can_connect=_async_can_connect_factory(entry_data, source),
|
||||||
)
|
)
|
||||||
scanner = ESPHomeScanner(hass, source, new_info_callback, connector, connectable)
|
scanner = ESPHomeScanner(hass, source, new_info_callback, connector, connectable)
|
||||||
unload_callbacks = [
|
unload_callbacks = [
|
||||||
|
Loading…
x
Reference in New Issue
Block a user