diff --git a/homeassistant/components/shelly/bluetooth/__init__.py b/homeassistant/components/shelly/bluetooth/__init__.py index 2f9019ba5e6cfd5b68ea2f816a134dfa545019a1..5432ceb3a12e547a092f886bef5e4a40fb3fe316 100644 --- a/homeassistant/components/shelly/bluetooth/__init__.py +++ b/homeassistant/components/shelly/bluetooth/__init__.py @@ -3,7 +3,7 @@ from __future__ import annotations from typing import TYPE_CHECKING -from aioshelly.ble import async_start_scanner +from aioshelly.ble import async_start_scanner, create_scanner from aioshelly.ble.const import ( BLE_SCAN_RESULT_EVENT, BLE_SCAN_RESULT_VERSION, @@ -12,15 +12,11 @@ from aioshelly.ble.const import ( DEFAULT_WINDOW_MS, ) -from homeassistant.components.bluetooth import ( - HaBluetoothConnector, - async_register_scanner, -) +from homeassistant.components.bluetooth import async_register_scanner from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback as hass_callback from homeassistant.helpers.device_registry import format_mac from ..const import BLEScannerMode -from .scanner import ShellyBLEScanner if TYPE_CHECKING: from ..coordinator import ShellyRpcCoordinator @@ -35,13 +31,7 @@ async def async_connect_scanner( device = coordinator.device entry = coordinator.entry source = format_mac(coordinator.mac).upper() - connector = HaBluetoothConnector( - # no active connections to shelly yet - client=None, # type: ignore[arg-type] - source=source, - can_connect=lambda: False, - ) - scanner = ShellyBLEScanner(source, entry.title, connector, False) + scanner = create_scanner(source, entry.title) unload_callbacks = [ async_register_scanner(hass, scanner), scanner.async_setup(), diff --git a/homeassistant/components/shelly/bluetooth/scanner.py b/homeassistant/components/shelly/bluetooth/scanner.py deleted file mode 100644 index 7c0dc3c792ad98665a7bf2ed8c2e2b099c90d565..0000000000000000000000000000000000000000 --- a/homeassistant/components/shelly/bluetooth/scanner.py +++ /dev/null @@ -1,48 +0,0 @@ -"""Bluetooth scanner for shelly.""" -from __future__ import annotations - -from typing import Any - -from aioshelly.ble import parse_ble_scan_result_event -from aioshelly.ble.const import BLE_SCAN_RESULT_EVENT, BLE_SCAN_RESULT_VERSION - -from homeassistant.components.bluetooth import MONOTONIC_TIME, BaseHaRemoteScanner -from homeassistant.core import callback - -from ..const import LOGGER - - -class ShellyBLEScanner(BaseHaRemoteScanner): - """Scanner for shelly.""" - - @callback - def async_on_event(self, event: dict[str, Any]) -> None: - """Process an event from the shelly and ignore if its not a ble.scan_result.""" - if event.get("event") != BLE_SCAN_RESULT_EVENT: - return - - data = event["data"] - - if data[0] != BLE_SCAN_RESULT_VERSION: - LOGGER.warning("Unsupported BLE scan result version: %s", data[0]) - return - - try: - address, rssi, parsed = parse_ble_scan_result_event(data) - except Exception as err: # pylint: disable=broad-except - # Broad exception catch because we have no - # control over the data that is coming in. - LOGGER.error("Failed to parse BLE event: %s", err, exc_info=True) - return - - self._async_on_advertisement( - address, - rssi, - parsed.local_name, - parsed.service_uuids, - parsed.service_data, - parsed.manufacturer_data, - parsed.tx_power, - {}, - MONOTONIC_TIME(), - ) diff --git a/homeassistant/components/shelly/manifest.json b/homeassistant/components/shelly/manifest.json index 82833bf34af1ff6475e5bfbe01ef4afbb0c46546..94e2e9d70f03c68c3be259c711a0967807e644dc 100644 --- a/homeassistant/components/shelly/manifest.json +++ b/homeassistant/components/shelly/manifest.json @@ -9,7 +9,7 @@ "iot_class": "local_push", "loggers": ["aioshelly"], "quality_scale": "platinum", - "requirements": ["aioshelly==7.1.0"], + "requirements": ["aioshelly==8.0.0"], "zeroconf": [ { "type": "_http._tcp.local.", diff --git a/requirements_all.txt b/requirements_all.txt index c402377c4dee2809eb5295f21bbe44a5e289fb0b..d59389e7aa823c7a9c53f0a2630ad0410994e72c 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -359,7 +359,7 @@ aioruuvigateway==0.1.0 aiosenz==1.0.0 # homeassistant.components.shelly -aioshelly==7.1.0 +aioshelly==8.0.0 # homeassistant.components.skybell aioskybell==22.7.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 8ba2348dacdc7e2e7f6121048d08bd0615bf3e0f..bc8483c42e952958d5c9ee0ea3418db659dc3ec8 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -332,7 +332,7 @@ aioruuvigateway==0.1.0 aiosenz==1.0.0 # homeassistant.components.shelly -aioshelly==7.1.0 +aioshelly==8.0.0 # homeassistant.components.skybell aioskybell==22.7.0 diff --git a/tests/components/shelly/bluetooth/test_scanner.py b/tests/components/shelly/bluetooth/test_scanner.py index 9fe5f77f00c9460912dc0e0b01f386035f310188..d9ec006460611c06e4e9b3bbfea248e02e3e8b11 100644 --- a/tests/components/shelly/bluetooth/test_scanner.py +++ b/tests/components/shelly/bluetooth/test_scanner.py @@ -11,8 +11,8 @@ from homeassistant.core import HomeAssistant from .. import init_integration, inject_rpc_device_event -async def test_scanner(hass: HomeAssistant, mock_rpc_device, monkeypatch) -> None: - """Test injecting data into the scanner.""" +async def test_scanner_v1(hass: HomeAssistant, mock_rpc_device, monkeypatch) -> None: + """Test injecting data into the scanner v1.""" await init_integration( hass, 2, options={CONF_BLE_SCANNER_MODE: BLEScannerMode.ACTIVE} ) @@ -49,6 +49,48 @@ async def test_scanner(hass: HomeAssistant, mock_rpc_device, monkeypatch) -> Non assert ble_device is None +async def test_scanner_v2(hass: HomeAssistant, mock_rpc_device, monkeypatch) -> None: + """Test injecting data into the scanner v2.""" + await init_integration( + hass, 2, options={CONF_BLE_SCANNER_MODE: BLEScannerMode.ACTIVE} + ) + assert mock_rpc_device.initialized is True + inject_rpc_device_event( + monkeypatch, + mock_rpc_device, + { + "events": [ + { + "component": "script:1", + "data": [ + 2, + [ + [ + "aa:bb:cc:dd:ee:ff", + -62, + "AgEGCf9ZANH7O3TIkA==", + "EQcbxdWlAgC4n+YRTSIADaLLBhYADUgQYQ==", + ] + ], + ], + "event": BLE_SCAN_RESULT_EVENT, + "id": 1, + "ts": 1668522399.2, + } + ], + "ts": 1668522399.2, + }, + ) + ble_device = bluetooth.async_ble_device_from_address( + hass, "AA:BB:CC:DD:EE:FF", connectable=False + ) + assert ble_device is not None + ble_device = bluetooth.async_ble_device_from_address( + hass, "AA:BB:CC:DD:EE:FF", connectable=True + ) + assert ble_device is None + + async def test_scanner_ignores_non_ble_events( hass: HomeAssistant, mock_rpc_device, monkeypatch ) -> None: