diff --git a/homeassistant/components/shelly/__init__.py b/homeassistant/components/shelly/__init__.py index 5ca58ec7d01..55b75b3face 100644 --- a/homeassistant/components/shelly/__init__.py +++ b/homeassistant/components/shelly/__init__.py @@ -12,7 +12,7 @@ from aioshelly.exceptions import ( InvalidAuthError, MacAddressMismatchError, ) -from aioshelly.rpc_device import RpcDevice +from aioshelly.rpc_device import RpcDevice, bluetooth_mac_from_primary_mac import voluptuous as vol from homeassistant.components.bluetooth import async_remove_scanner @@ -339,4 +339,5 @@ async def async_remove_entry(hass: HomeAssistant, entry: ShellyConfigEntry) -> N if get_device_entry_gen(entry) in RPC_GENERATIONS and ( mac_address := entry.unique_id ): - async_remove_scanner(hass, mac_address) + source = dr.format_mac(bluetooth_mac_from_primary_mac(mac_address)).upper() + async_remove_scanner(hass, source) diff --git a/homeassistant/components/shelly/bluetooth/__init__.py b/homeassistant/components/shelly/bluetooth/__init__.py index d7eb020d671..cad1b9f044d 100644 --- a/homeassistant/components/shelly/bluetooth/__init__.py +++ b/homeassistant/components/shelly/bluetooth/__init__.py @@ -9,7 +9,6 @@ from aioshelly.ble.const import BLE_SCAN_RESULT_EVENT, BLE_SCAN_RESULT_VERSION 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 @@ -26,8 +25,7 @@ async def async_connect_scanner( """Connect scanner.""" device = coordinator.device entry = coordinator.config_entry - source = format_mac(coordinator.mac).upper() - scanner = create_scanner(source, entry.title) + scanner = create_scanner(coordinator.bluetooth_source, entry.title) unload_callbacks = [ async_register_scanner( hass, diff --git a/homeassistant/components/shelly/coordinator.py b/homeassistant/components/shelly/coordinator.py index 7b4da241043..bebf8efbdd7 100644 --- a/homeassistant/components/shelly/coordinator.py +++ b/homeassistant/components/shelly/coordinator.py @@ -18,6 +18,7 @@ from aioshelly.exceptions import ( RpcCallError, ) from aioshelly.rpc_device import RpcDevice, RpcUpdateType +from aioshelly.rpc_device.utils import bluetooth_mac_from_primary_mac from propcache.api import cached_property from homeassistant.components.bluetooth import async_remove_scanner @@ -496,6 +497,15 @@ class ShellyRpcCoordinator(ShellyCoordinatorBase[RpcDevice]): self._connect_task: asyncio.Task | None = None entry.async_on_unload(entry.add_update_listener(self._async_update_listener)) + @cached_property + def bluetooth_source(self) -> str: + """Return the Bluetooth source address. + + This is the Bluetooth MAC address of the device that is used + for the Bluetooth scanner. + """ + return format_mac(bluetooth_mac_from_primary_mac(self.mac)).upper() + async def async_device_online(self, source: str) -> None: """Handle device going online.""" if not self.sleep_period: @@ -706,7 +716,7 @@ class ShellyRpcCoordinator(ShellyCoordinatorBase[RpcDevice]): ) if ble_scanner_mode == BLEScannerMode.DISABLED and self.connected: await async_stop_scanner(self.device) - async_remove_scanner(self.hass, format_mac(self.mac).upper()) + async_remove_scanner(self.hass, self.bluetooth_source) return if await async_ensure_ble_enabled(self.device): # BLE enable required a reboot, don't bother connecting diff --git a/homeassistant/components/shelly/diagnostics.py b/homeassistant/components/shelly/diagnostics.py index a5fe1f5b6c0..d56a2884e17 100644 --- a/homeassistant/components/shelly/diagnostics.py +++ b/homeassistant/components/shelly/diagnostics.py @@ -8,7 +8,6 @@ from homeassistant.components.bluetooth import async_scanner_by_source from homeassistant.components.diagnostics import async_redact_data from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant -from homeassistant.helpers.device_registry import format_mac from .coordinator import ShellyConfigEntry from .utils import get_rpc_ws_url @@ -86,8 +85,7 @@ async def async_get_config_entry_diagnostics( if k in ["sys", "wifi"] } - source = format_mac(rpc_coordinator.mac).upper() - if scanner := async_scanner_by_source(hass, source): + if scanner := async_scanner_by_source(hass, rpc_coordinator.bluetooth_source): bluetooth = { "scanner": await scanner.async_diagnostics(), } diff --git a/tests/components/shelly/test_diagnostics.py b/tests/components/shelly/test_diagnostics.py index c0f78d48d9b..3826631c580 100644 --- a/tests/components/shelly/test_diagnostics.py +++ b/tests/components/shelly/test_diagnostics.py @@ -134,17 +134,17 @@ async def test_rpc_config_entry_diagnostics( -62, [], ], - "details": {"source": "12:34:56:78:9A:BC"}, + "details": {"source": "12:34:56:78:9A:BE"}, "name": None, "rssi": -62, } ], "last_detection": ANY, "monotonic_time": ANY, - "name": "Mock Title (12:34:56:78:9A:BC)", + "name": "Mock Title (12:34:56:78:9A:BE)", "scanning": True, "start_time": ANY, - "source": "12:34:56:78:9A:BC", + "source": "12:34:56:78:9A:BE", "time_since_last_device_detection": {"AA:BB:CC:DD:EE:FF": ANY}, "type": "ShellyBLEScanner", } diff --git a/tests/components/shelly/test_init.py b/tests/components/shelly/test_init.py index 036da1bfd64..c9e4ce253e4 100644 --- a/tests/components/shelly/test_init.py +++ b/tests/components/shelly/test_init.py @@ -11,6 +11,7 @@ from aioshelly.exceptions import ( InvalidAuthError, MacAddressMismatchError, ) +from aioshelly.rpc_device.utils import bluetooth_mac_from_primary_mac import pytest from homeassistant.components.shelly.const import ( @@ -27,7 +28,7 @@ from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState from homeassistant.const import CONF_HOST, CONF_PORT, STATE_ON, STATE_UNAVAILABLE from homeassistant.core import HomeAssistant from homeassistant.helpers import issue_registry as ir -from homeassistant.helpers.device_registry import DeviceRegistry +from homeassistant.helpers.device_registry import DeviceRegistry, format_mac from homeassistant.setup import async_setup_component from . import init_integration, mutate_rpc_device_status @@ -545,4 +546,6 @@ async def test_bluetooth_cleanup_on_remove_entry( await hass.config_entries.async_remove(entry.entry_id) await hass.async_block_till_done() - remove_mock.assert_called_once_with(hass, entry.unique_id.upper()) + remove_mock.assert_called_once_with( + hass, format_mac(bluetooth_mac_from_primary_mac(entry.unique_id)).upper() + )