diff --git a/homeassistant/components/ruuvi_gateway/__init__.py b/homeassistant/components/ruuvi_gateway/__init__.py index 53f0deacecf0..663a398b501c 100644 --- a/homeassistant/components/ruuvi_gateway/__init__.py +++ b/homeassistant/components/ruuvi_gateway/__init__.py @@ -2,6 +2,7 @@ import logging +from homeassistant.components.bluetooth import async_remove_scanner from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant @@ -32,3 +33,11 @@ async def async_unload_entry( hass: HomeAssistant, entry: RuuviGatewayConfigEntry ) -> None: """Unload a config entry.""" + + +async def async_remove_entry( + hass: HomeAssistant, entry: RuuviGatewayConfigEntry +) -> None: + """Remove a config entry.""" + if mac_address := entry.unique_id: + async_remove_scanner(hass, mac_address.upper()) diff --git a/homeassistant/components/ruuvi_gateway/bluetooth.py b/homeassistant/components/ruuvi_gateway/bluetooth.py index 8a0eaebe215c..6813d3ee22ee 100644 --- a/homeassistant/components/ruuvi_gateway/bluetooth.py +++ b/homeassistant/components/ruuvi_gateway/bluetooth.py @@ -12,6 +12,7 @@ from homeassistant.components.bluetooth import ( from homeassistant.config_entries import ConfigEntry from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback +from .const import DOMAIN from .coordinator import RuuviGatewayUpdateCoordinator _LOGGER = logging.getLogger(__name__) @@ -71,7 +72,9 @@ def async_connect_scanner( ) -> tuple[RuuviGatewayScanner, CALLBACK_TYPE]: """Connect scanner and start polling.""" assert entry.unique_id is not None - source = str(entry.unique_id) + # Scanner sources are upper case MAC addresses everywhere else in core, and + # the device registry does not normalise CONNECTION_BLUETOOTH values. + source = str(entry.unique_id).upper() _LOGGER.debug( "%s [%s]: Connecting scanner", entry.title, @@ -83,7 +86,12 @@ def async_connect_scanner( coordinator=coordinator, ) unload_callbacks = [ - async_register_scanner(hass, scanner), + async_register_scanner( + hass, + scanner, + source_domain=DOMAIN, + source_config_entry_id=entry.entry_id, + ), scanner.async_setup(), scanner.start_polling(), ] diff --git a/tests/components/ruuvi_gateway/test_init.py b/tests/components/ruuvi_gateway/test_init.py new file mode 100644 index 000000000000..e846e1325173 --- /dev/null +++ b/tests/components/ruuvi_gateway/test_init.py @@ -0,0 +1,73 @@ +"""Test the Ruuvi Gateway setup.""" + +from homeassistant.components.bluetooth import DOMAIN as BLUETOOTH_DOMAIN +from homeassistant.components.ruuvi_gateway.const import DOMAIN +from homeassistant.config_entries import ConfigEntryState +from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr + +from .consts import BASE_DATA, EXPECTED_TITLE, GATEWAY_MAC, GATEWAY_MAC_LOWER + +from tests.common import MockConfigEntry + + +async def test_scanner_device( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, +) -> None: + """Test the scanner gets a device entry that can hold an area.""" + entry = MockConfigEntry( + domain=DOMAIN, + unique_id=GATEWAY_MAC_LOWER, + title=EXPECTED_TITLE, + data=BASE_DATA, + ) + entry.add_to_hass(hass) + + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + assert entry.state is ConfigEntryState.LOADED + + scanner_entry = hass.config_entries.async_entry_for_domain_unique_id( + BLUETOOTH_DOMAIN, GATEWAY_MAC + ) + assert scanner_entry is not None + + device_entry = device_registry.async_get_device_by_connection( + (dr.CONNECTION_BLUETOOTH, GATEWAY_MAC), scanner_entry.entry_id + ) + assert device_entry is not None + + +async def test_scanner_removed_with_entry( + hass: HomeAssistant, +) -> None: + """Test removing the entry also removes the scanner's Bluetooth entry.""" + entry = MockConfigEntry( + domain=DOMAIN, + unique_id=GATEWAY_MAC_LOWER, + title=EXPECTED_TITLE, + data=BASE_DATA, + ) + entry.add_to_hass(hass) + + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + assert ( + hass.config_entries.async_entry_for_domain_unique_id( + BLUETOOTH_DOMAIN, GATEWAY_MAC + ) + is not None + ) + + assert await hass.config_entries.async_remove(entry.entry_id) + await hass.async_block_till_done() + + assert ( + hass.config_entries.async_entry_for_domain_unique_id( + BLUETOOTH_DOMAIN, GATEWAY_MAC + ) + is None + )