diff --git a/homeassistant/components/github/__init__.py b/homeassistant/components/github/__init__.py index abc376e0337..6e83ef72f68 100644 --- a/homeassistant/components/github/__init__.py +++ b/homeassistant/components/github/__init__.py @@ -8,12 +8,13 @@ from aiogithubapi import GitHubAPI from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_ACCESS_TOKEN, Platform from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr from homeassistant.helpers.aiohttp_client import ( SERVER_SOFTWARE, async_get_clientsession, ) -from .const import CONF_REPOSITORIES, DOMAIN +from .const import CONF_REPOSITORIES, DOMAIN, LOGGER from .coordinator import ( DataUpdateCoordinators, RepositoryCommitDataUpdateCoordinator, @@ -64,12 +65,39 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass.data[DOMAIN][repository] = coordinators + await async_cleanup_device_registry(hass=hass, entry=entry) + hass.config_entries.async_setup_platforms(entry, PLATFORMS) entry.async_on_unload(entry.add_update_listener(async_reload_entry)) return True +async def async_cleanup_device_registry( + hass: HomeAssistant, + entry: ConfigEntry, +) -> None: + """Remove entries form device registry if we no longer track the repository.""" + device_registry = dr.async_get(hass) + devices = dr.async_entries_for_config_entry( + registry=device_registry, + config_entry_id=entry.entry_id, + ) + for device in devices: + for item in device.identifiers: + if DOMAIN == item[0] and item[1] not in entry.options[CONF_REPOSITORIES]: + LOGGER.debug( + "Unlinking device %s for untracked repository %s from config entry %s", + device.id, + item[1], + entry.entry_id, + ) + device_registry.async_update_device( + device.id, remove_config_entry_id=entry.entry_id + ) + break + + async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a config entry.""" if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): diff --git a/tests/components/github/test_init.py b/tests/components/github/test_init.py new file mode 100644 index 00000000000..95bad95fd4c --- /dev/null +++ b/tests/components/github/test_init.py @@ -0,0 +1,31 @@ +"""Test the GitHub init file.""" +from pytest import LogCaptureFixture + +from homeassistant.components.github import async_cleanup_device_registry +from homeassistant.components.github.const import DOMAIN +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry, mock_device_registry + + +async def test_device_registry_cleanup( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + caplog: LogCaptureFixture, +) -> None: + """Test that we remove untracked repositories from the decvice registry.""" + registry = mock_device_registry(hass) + + device = registry.async_get_or_create( + identifiers={(DOMAIN, "test/repository")}, + config_entry_id=mock_config_entry.entry_id, + ) + + assert registry.async_get_device({(DOMAIN, "test/repository")}) == device + await async_cleanup_device_registry(hass, mock_config_entry) + + assert ( + f"Unlinking device {device.id} for untracked repository test/repository from config entry {mock_config_entry.entry_id}" + in caplog.text + ) + assert registry.async_get_device({(DOMAIN, "test/repository")}) is None