Skip to content

Commit

Permalink
Update habluetooth to 2.0.0 (home-assistant#106145)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Dec 21, 2023
1 parent dbb726f commit e50fe79
Show file tree
Hide file tree
Showing 18 changed files with 53 additions and 60 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/bluetooth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
adapters = await manager.async_get_bluetooth_adapters()
details = adapters[adapter]
slots: int = details.get(ADAPTER_CONNECTION_SLOTS) or DEFAULT_CONNECTION_SLOTS
entry.async_on_unload(async_register_scanner(hass, scanner, True, slots))
entry.async_on_unload(async_register_scanner(hass, scanner, connection_slots=slots))
await async_update_device(hass, entry, adapter, details)
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = scanner
entry.async_on_unload(entry.add_update_listener(async_update_listener))
Expand Down
5 changes: 1 addition & 4 deletions homeassistant/components/bluetooth/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,10 @@ def async_rediscover_address(hass: HomeAssistant, address: str) -> None:
def async_register_scanner(
hass: HomeAssistant,
scanner: BaseHaScanner,
connectable: bool,
connection_slots: int | None = None,
) -> CALLBACK_TYPE:
"""Register a BleakScanner."""
return _get_manager(hass).async_register_scanner(
scanner, connectable, connection_slots
)
return _get_manager(hass).async_register_scanner(scanner, connection_slots)


@hass_callback
Expand Down
5 changes: 1 addition & 4 deletions homeassistant/components/bluetooth/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,12 @@ def _async_unregister_scanner(
def async_register_scanner(
self,
scanner: BaseHaScanner,
connectable: bool,
connection_slots: int | None = None,
) -> CALLBACK_TYPE:
"""Register a scanner."""
if isinstance(scanner, BaseHaRemoteScanner):
if history := self.storage.async_get_advertisement_history(scanner.source):
scanner.restore_discovered_devices(history)

unregister = super().async_register_scanner(
scanner, connectable, connection_slots
)
unregister = super().async_register_scanner(scanner, connection_slots)
return partial(self._async_unregister_scanner, scanner, unregister)
2 changes: 1 addition & 1 deletion homeassistant/components/bluetooth/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
"bluetooth-auto-recovery==1.2.3",
"bluetooth-data-tools==1.18.0",
"dbus-fast==2.21.0",
"habluetooth==1.0.0"
"habluetooth==2.0.0"
]
}
2 changes: 1 addition & 1 deletion homeassistant/components/esphome/bluetooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def async_connect_scanner(
return partial(
_async_unload,
[
async_register_scanner(hass, scanner, scanner.connectable),
async_register_scanner(hass, scanner),
scanner.async_setup(),
],
)
2 changes: 1 addition & 1 deletion homeassistant/components/ruuvi_gateway/bluetooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def async_connect_scanner(
coordinator=coordinator,
)
unload_callbacks = [
async_register_scanner(hass, scanner, connectable=False),
async_register_scanner(hass, scanner),
scanner.async_setup(),
scanner.start_polling(),
]
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/shelly/bluetooth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async def async_connect_scanner(
)
scanner = ShellyBLEScanner(source, entry.title, connector, False)
unload_callbacks = [
async_register_scanner(hass, scanner, False),
async_register_scanner(hass, scanner),
scanner.async_setup(),
coordinator.async_subscribe_events(scanner.async_on_event),
]
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/package_constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dbus-fast==2.21.0
fnv-hash-fast==0.5.0
ha-av==10.1.1
ha-ffmpeg==3.1.0
habluetooth==1.0.0
habluetooth==2.0.0
hass-nabucasa==0.75.1
hassil==1.5.1
home-assistant-bluetooth==1.11.0
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ ha-philipsjs==3.1.1
habitipy==0.2.0

# homeassistant.components.bluetooth
habluetooth==1.0.0
habluetooth==2.0.0

# homeassistant.components.cloud
hass-nabucasa==0.75.1
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ ha-philipsjs==3.1.1
habitipy==0.2.0

# homeassistant.components.bluetooth
habluetooth==1.0.0
habluetooth==2.0.0

# homeassistant.components.cloud
hass-nabucasa==0.75.1
Expand Down
2 changes: 1 addition & 1 deletion tests/components/bluetooth/test_advertisement_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ async def test_advertisment_interval_longer_than_adapter_stack_timeout_adapter_c
switchbot_device_went_unavailable = False

scanner = FakeScanner("new", "fake_adapter")
cancel_scanner = async_register_scanner(hass, scanner, False)
cancel_scanner = async_register_scanner(hass, scanner)

@callback
def _switchbot_device_unavailable_callback(_address: str) -> None:
Expand Down
8 changes: 4 additions & 4 deletions tests/components/bluetooth/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async def test_scanner_by_source(hass: HomeAssistant, enable_bluetooth: None) ->
"""Test we can get a scanner by source."""

hci2_scanner = FakeScanner("hci2", "hci2")
cancel_hci2 = bluetooth.async_register_scanner(hass, hci2_scanner, True)
cancel_hci2 = bluetooth.async_register_scanner(hass, hci2_scanner)

assert async_scanner_by_source(hass, "hci2") is hci2_scanner
cancel_hci2()
Expand Down Expand Up @@ -74,9 +74,9 @@ def inject_advertisement(
connector = (
HaBluetoothConnector(MockBleakClient, "mock_bleak_client", lambda: False),
)
scanner = FakeInjectableScanner("esp32", "esp32", connector, False)
scanner = FakeInjectableScanner("esp32", "esp32", connector, True)
unsetup = scanner.async_setup()
cancel = manager.async_register_scanner(scanner, True)
cancel = manager.async_register_scanner(scanner)
switchbot_device = generate_ble_device(
"44:44:33:11:23:45",
"wohand",
Expand Down Expand Up @@ -141,7 +141,7 @@ def discovered_devices_and_advertisement_data(
HaBluetoothConnector(MockBleakClient, "mock_bleak_client", lambda: False),
)
scanner = FakeStaticScanner("esp32", "esp32", connector)
cancel = manager.async_register_scanner(scanner, False)
cancel = manager.async_register_scanner(scanner)

assert scanner.discovered_devices_and_advertisement_data == {
switchbot_device.address: (switchbot_device, switchbot_device_adv)
Expand Down
24 changes: 12 additions & 12 deletions tests/components/bluetooth/test_base_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ async def test_remote_scanner(
)
scanner = FakeScanner("esp32", "esp32", connector, True)
unsetup = scanner.async_setup()
cancel = manager.async_register_scanner(scanner, True)
cancel = manager.async_register_scanner(scanner)

scanner.inject_advertisement(switchbot_device, switchbot_device_adv)

Expand Down Expand Up @@ -182,7 +182,7 @@ async def test_remote_scanner_expires_connectable(
)
scanner = FakeScanner("esp32", "esp32", connector, True)
unsetup = scanner.async_setup()
cancel = manager.async_register_scanner(scanner, True)
cancel = manager.async_register_scanner(scanner)

start_time_monotonic = time.monotonic()
scanner.inject_advertisement(switchbot_device, switchbot_device_adv)
Expand Down Expand Up @@ -234,9 +234,9 @@ async def test_remote_scanner_expires_non_connectable(
connector = (
HaBluetoothConnector(MockBleakClient, "mock_bleak_client", lambda: False),
)
scanner = FakeScanner("esp32", "esp32", connector, False)
scanner = FakeScanner("esp32", "esp32", connector, True)
unsetup = scanner.async_setup()
cancel = manager.async_register_scanner(scanner, True)
cancel = manager.async_register_scanner(scanner)

start_time_monotonic = time.monotonic()
scanner.inject_advertisement(switchbot_device, switchbot_device_adv)
Expand Down Expand Up @@ -308,9 +308,9 @@ async def test_base_scanner_connecting_behavior(
connector = (
HaBluetoothConnector(MockBleakClient, "mock_bleak_client", lambda: False),
)
scanner = FakeScanner("esp32", "esp32", connector, False)
scanner = FakeScanner("esp32", "esp32", connector, True)
unsetup = scanner.async_setup()
cancel = manager.async_register_scanner(scanner, True)
cancel = manager.async_register_scanner(scanner)

with scanner.connecting():
assert scanner.scanning is False
Expand Down Expand Up @@ -366,7 +366,7 @@ async def test_restore_history_remote_adapter(
True,
)
unsetup = scanner.async_setup()
cancel = _get_manager().async_register_scanner(scanner, True)
cancel = _get_manager().async_register_scanner(scanner)

assert "EB:0B:36:35:6F:A4" in scanner.discovered_devices_and_advertisement_data
assert "E3:A5:63:3E:5E:23" not in scanner.discovered_devices_and_advertisement_data
Expand All @@ -380,7 +380,7 @@ async def test_restore_history_remote_adapter(
True,
)
unsetup = scanner.async_setup()
cancel = _get_manager().async_register_scanner(scanner, True)
cancel = _get_manager().async_register_scanner(scanner)
assert "EB:0B:36:35:6F:A4" in scanner.discovered_devices_and_advertisement_data
assert "E3:A5:63:3E:5E:23" not in scanner.discovered_devices_and_advertisement_data

Expand Down Expand Up @@ -410,9 +410,9 @@ async def test_device_with_ten_minute_advertising_interval(
connector = (
HaBluetoothConnector(MockBleakClient, "mock_bleak_client", lambda: False),
)
scanner = FakeScanner("esp32", "esp32", connector, False)
scanner = FakeScanner("esp32", "esp32", connector, True)
unsetup = scanner.async_setup()
cancel = manager.async_register_scanner(scanner, True)
cancel = manager.async_register_scanner(scanner)

monotonic_now = time.monotonic()
new_time = monotonic_now
Expand Down Expand Up @@ -501,9 +501,9 @@ async def test_scanner_stops_responding(
connector = (
HaBluetoothConnector(MockBleakClient, "mock_bleak_client", lambda: False),
)
scanner = FakeScanner("esp32", "esp32", connector, False)
scanner = FakeScanner("esp32", "esp32", connector, True)
unsetup = scanner.async_setup()
cancel = manager.async_register_scanner(scanner, True)
cancel = manager.async_register_scanner(scanner)

start_time_monotonic = time.monotonic()

Expand Down
12 changes: 6 additions & 6 deletions tests/components/bluetooth/test_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,9 @@ def inject_advertisement(
connector = (
HaBluetoothConnector(MockBleakClient, "mock_bleak_client", lambda: False),
)
scanner = FakeScanner("esp32", "esp32", connector, False)
scanner = FakeScanner("esp32", "esp32", connector, True)
unsetup = scanner.async_setup()
cancel = manager.async_register_scanner(scanner, True)
cancel = manager.async_register_scanner(scanner)

scanner.inject_advertisement(switchbot_device, switchbot_adv)
inject_advertisement(hass, switchbot_device, switchbot_adv)
Expand Down Expand Up @@ -511,7 +511,7 @@ def inject_advertisement(
-127,
[],
],
"connectable": False,
"connectable": True,
"device": {
"__type": "<class 'bleak.backends.device.BLEDevice'>",
"repr": "BLEDevice(44:44:33:11:23:45, wohand)",
Expand All @@ -537,7 +537,7 @@ def inject_advertisement(
[],
-127,
-127,
[[]],
[],
],
"connectable": True,
"device": {
Expand All @@ -551,7 +551,7 @@ def inject_advertisement(
"rssi": -127,
"service_data": {},
"service_uuids": [],
"source": "local",
"source": "esp32",
"time": ANY,
}
],
Expand Down Expand Up @@ -595,7 +595,7 @@ def inject_advertisement(
"type": "FakeHaScanner",
},
{
"connectable": False,
"connectable": True,
"discovered_device_timestamps": {"44:44:33:11:23:45": ANY},
"discovered_devices_and_advertisement_data": [
{
Expand Down
4 changes: 2 additions & 2 deletions tests/components/bluetooth/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2816,15 +2816,15 @@ async def test_scanner_count_connectable(
) -> None:
"""Test getting the connectable scanner count."""
scanner = FakeScanner("any", "any")
cancel = bluetooth.async_register_scanner(hass, scanner, False)
cancel = bluetooth.async_register_scanner(hass, scanner)
assert bluetooth.async_scanner_count(hass, connectable=True) == 1
cancel()


async def test_scanner_count(hass: HomeAssistant, enable_bluetooth: None) -> None:
"""Test getting the connectable and non-connectable scanner count."""
scanner = FakeScanner("any", "any")
cancel = bluetooth.async_register_scanner(hass, scanner, False)
cancel = bluetooth.async_register_scanner(hass, scanner)
assert bluetooth.async_scanner_count(hass, connectable=False) == 2
cancel()

Expand Down
20 changes: 9 additions & 11 deletions tests/components/bluetooth/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
def register_hci0_scanner(hass: HomeAssistant) -> Generator[None, None, None]:
"""Register an hci0 scanner."""
hci0_scanner = FakeScanner("hci0", "hci0")
cancel = bluetooth.async_register_scanner(hass, hci0_scanner, True)
cancel = bluetooth.async_register_scanner(hass, hci0_scanner)
yield
cancel()

Expand All @@ -64,7 +64,7 @@ def register_hci0_scanner(hass: HomeAssistant) -> Generator[None, None, None]:
def register_hci1_scanner(hass: HomeAssistant) -> Generator[None, None, None]:
"""Register an hci1 scanner."""
hci1_scanner = FakeScanner("hci1", "hci1")
cancel = bluetooth.async_register_scanner(hass, hci1_scanner, True)
cancel = bluetooth.async_register_scanner(hass, hci1_scanner)
yield
cancel()

Expand Down Expand Up @@ -559,9 +559,7 @@ async def test_switching_adapters_when_one_goes_away(
hass: HomeAssistant, enable_bluetooth: None, register_hci0_scanner: None
) -> None:
"""Test switching adapters when one goes away."""
cancel_hci2 = bluetooth.async_register_scanner(
hass, FakeScanner("hci2", "hci2"), True
)
cancel_hci2 = bluetooth.async_register_scanner(hass, FakeScanner("hci2", "hci2"))

address = "44:44:33:11:23:45"

Expand Down Expand Up @@ -611,7 +609,7 @@ async def test_switching_adapters_when_one_stop_scanning(
) -> None:
"""Test switching adapters when stops scanning."""
hci2_scanner = FakeScanner("hci2", "hci2")
cancel_hci2 = bluetooth.async_register_scanner(hass, hci2_scanner, True)
cancel_hci2 = bluetooth.async_register_scanner(hass, hci2_scanner)

address = "44:44:33:11:23:45"

Expand Down Expand Up @@ -730,7 +728,7 @@ def inject_advertisement(
)
unsetup_connectable_scanner = connectable_scanner.async_setup()
cancel_connectable_scanner = _get_manager().async_register_scanner(
connectable_scanner, True
connectable_scanner
)
connectable_scanner.inject_advertisement(
switchbot_device_connectable, switchbot_device_adv
Expand All @@ -752,7 +750,7 @@ def inject_advertisement(
)
unsetup_not_connectable_scanner = not_connectable_scanner.async_setup()
cancel_not_connectable_scanner = _get_manager().async_register_scanner(
not_connectable_scanner, False
not_connectable_scanner
)
not_connectable_scanner.inject_advertisement(
switchbot_device_non_connectable, switchbot_device_adv
Expand Down Expand Up @@ -801,7 +799,7 @@ def _unavailable_callback(service_info: BluetoothServiceInfoBleak) -> None:
)
unsetup_connectable_scanner_2 = connectable_scanner_2.async_setup()
cancel_connectable_scanner_2 = _get_manager().async_register_scanner(
connectable_scanner, True
connectable_scanner
)
connectable_scanner_2.inject_advertisement(
switchbot_device_connectable, switchbot_device_adv
Expand Down Expand Up @@ -902,7 +900,7 @@ def clear_all_devices(self) -> None:
)
unsetup_connectable_scanner = non_connectable_scanner.async_setup()
cancel_connectable_scanner = _get_manager().async_register_scanner(
non_connectable_scanner, True
non_connectable_scanner
)
with patch.object(hass.config_entries.flow, "async_init") as mock_config_flow:
non_connectable_scanner.inject_advertisement(
Expand All @@ -914,7 +912,7 @@ def clear_all_devices(self) -> None:
assert mock_config_flow.mock_calls[0][1][0] == "switchbot"

assert async_ble_device_from_address(hass, "44:44:33:11:23:45", False) is not None
assert async_scanner_count(hass, connectable=True) == 1
assert async_scanner_count(hass, connectable=False) == 1
assert len(callbacks) == 1

assert (
Expand Down
Loading

0 comments on commit e50fe79

Please sign in to comment.