diff --git a/homeassistant/components/shelly/coordinator.py b/homeassistant/components/shelly/coordinator.py index f980ba8f914..e4af35484c8 100644 --- a/homeassistant/components/shelly/coordinator.py +++ b/homeassistant/components/shelly/coordinator.py @@ -33,7 +33,11 @@ from homeassistant.const import ( from homeassistant.core import CALLBACK_TYPE, Event, HomeAssistant, callback from homeassistant.helpers import device_registry as dr, issue_registry as ir from homeassistant.helpers.debounce import Debouncer -from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, format_mac +from homeassistant.helpers.device_registry import ( + CONNECTION_BLUETOOTH, + CONNECTION_NETWORK_MAC, + format_mac, +) from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from .bluetooth import async_connect_scanner @@ -160,6 +164,11 @@ class ShellyCoordinatorBase[_DeviceT: BlockDevice | RpcDevice]( """Sleep period of the device.""" return self.config_entry.data.get(CONF_SLEEP_PERIOD, 0) + @property + def connections(self) -> set[tuple[str, str]]: + """Connections of the device.""" + return {(CONNECTION_NETWORK_MAC, self.mac)} + def async_setup(self, pending_platforms: list[Platform] | None = None) -> None: """Set up the coordinator.""" self._pending_platforms = pending_platforms @@ -167,7 +176,7 @@ class ShellyCoordinatorBase[_DeviceT: BlockDevice | RpcDevice]( device_entry = dev_reg.async_get_or_create( config_entry_id=self.config_entry.entry_id, name=self.name, - connections={(CONNECTION_NETWORK_MAC, self.mac)}, + connections=self.connections, identifiers={(DOMAIN, self.mac)}, manufacturer="Shelly", model=get_shelly_model_name(self.model, self.sleep_period, self.device), @@ -523,6 +532,14 @@ class ShellyRpcCoordinator(ShellyCoordinatorBase[RpcDevice]): """ return format_mac(bluetooth_mac_from_primary_mac(self.mac)).upper() + @property + def connections(self) -> set[tuple[str, str]]: + """Connections of the device.""" + connections = super().connections + if not self.sleep_period: + connections.add((CONNECTION_BLUETOOTH, self.bluetooth_source)) + return connections + async def async_device_online(self, source: str) -> None: """Handle device going online.""" if not self.sleep_period: diff --git a/tests/components/shelly/test_coordinator.py b/tests/components/shelly/test_coordinator.py index cf7f82014a0..aae452538bb 100644 --- a/tests/components/shelly/test_coordinator.py +++ b/tests/components/shelly/test_coordinator.py @@ -1078,3 +1078,21 @@ async def test_xmod_model_lookup( ) assert device assert device.model == xmod_model + + +async def test_device_entry_bt_address( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + mock_rpc_device: Mock, +) -> None: + """Check if BT address is added to device entry connections.""" + entry = await init_integration(hass, 2) + + device = device_registry.async_get_device( + identifiers={(DOMAIN, entry.entry_id)}, + connections={(dr.CONNECTION_NETWORK_MAC, dr.format_mac(entry.unique_id))}, + ) + + assert device + assert len(device.connections) == 2 + assert (dr.CONNECTION_BLUETOOTH, "12:34:56:78:9A:BE") in device.connections