Skip to content

Commit

Permalink
feat: update manager connection slot allocations on change (#70)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
bdraco and pre-commit-ci[bot] authored Jan 22, 2025
1 parent fc3dc36 commit 63045a7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 40 deletions.
72 changes: 36 additions & 36 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ python = ">=3.11,<3.14"
aioesphomeapi = ">=27.0.0"
bleak = ">=0.21.1"
bluetooth-data-tools = ">=1.18.0"
habluetooth = ">=1.0.0"
habluetooth = ">=3.10.0"
lru-dict = ">=1.2.0"
bleak-retry-connector = ">=3.8.0"

[tool.poetry.group.dev.dependencies]
pytest = ">=7,<9"
Expand Down
14 changes: 14 additions & 0 deletions src/bleak_esphome/backend/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

import asyncio
import logging
from collections.abc import Callable
from dataclasses import dataclass, field

from bleak_retry_connector import Allocations

from .cache import ESPHomeBluetoothCache

_LOGGER = logging.getLogger(__name__)
Expand All @@ -25,6 +28,13 @@ class ESPHomeBluetoothDevice:
loop: asyncio.AbstractEventLoop = field(default_factory=asyncio.get_running_loop)
available: bool = False
cache: ESPHomeBluetoothCache = field(default_factory=ESPHomeBluetoothCache)
_connection_slots_callback: Callable[[Allocations], None] | None = None

def async_subscribe_connection_slots(
self, callback: Callable[[Allocations], None]
) -> None:
"""Subscribe to connection slot changes."""
self._connection_slots_callback = callback

def async_update_ble_connection_limits(self, free: int, limit: int) -> None:
"""Update the BLE connection limits."""
Expand All @@ -47,6 +57,10 @@ def async_update_ble_connection_limits(self, free: int, limit: int) -> None:
if not fut.done():
fut.set_result(free)
self._ble_connection_free_futures.clear()
if connection_slots_callback := self._connection_slots_callback:
# Currently we don't know which connections are in use, so we
# just return an empty list.
connection_slots_callback(Allocations(self.mac_address, limit, free, []))

def _wait_for_ble_connections_free_timeout(self, fut: asyncio.Future[int]) -> None:
"""Timeout the wait_for_ble_connections_free future."""
Expand Down
7 changes: 4 additions & 3 deletions src/bleak_esphome/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
from typing import TYPE_CHECKING

from aioesphomeapi import APIClient, BluetoothProxyFeature, DeviceInfo
from habluetooth import (
HaBluetoothConnector,
)
from habluetooth import HaBluetoothConnector, get_manager

from .backend.client import ESPHomeClient, ESPHomeClientData
from .backend.device import ESPHomeBluetoothDevice
Expand Down Expand Up @@ -63,6 +61,9 @@ def connect_scanner(
bluetooth_device = ESPHomeBluetoothDevice(
name, device_info.mac_address, available=available
)
bluetooth_device.async_subscribe_connection_slots(
get_manager().async_on_allocation_changed
)
_LOGGER.debug(
"%s [%s]: Connecting scanner feature_flags=%s, connectable=%s",
name,
Expand Down

0 comments on commit 63045a7

Please sign in to comment.