Add device registry cleanup to github (#64380)
This commit is contained in:
parent
c285743621
commit
13fe5d8216
2 changed files with 60 additions and 1 deletions
|
@ -8,12 +8,13 @@ from aiogithubapi import GitHubAPI
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_ACCESS_TOKEN, Platform
|
from homeassistant.const import CONF_ACCESS_TOKEN, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers import device_registry as dr
|
||||||
from homeassistant.helpers.aiohttp_client import (
|
from homeassistant.helpers.aiohttp_client import (
|
||||||
SERVER_SOFTWARE,
|
SERVER_SOFTWARE,
|
||||||
async_get_clientsession,
|
async_get_clientsession,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .const import CONF_REPOSITORIES, DOMAIN
|
from .const import CONF_REPOSITORIES, DOMAIN, LOGGER
|
||||||
from .coordinator import (
|
from .coordinator import (
|
||||||
DataUpdateCoordinators,
|
DataUpdateCoordinators,
|
||||||
RepositoryCommitDataUpdateCoordinator,
|
RepositoryCommitDataUpdateCoordinator,
|
||||||
|
@ -64,12 +65,39 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
|
|
||||||
hass.data[DOMAIN][repository] = coordinators
|
hass.data[DOMAIN][repository] = coordinators
|
||||||
|
|
||||||
|
await async_cleanup_device_registry(hass=hass, entry=entry)
|
||||||
|
|
||||||
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
||||||
entry.async_on_unload(entry.add_update_listener(async_reload_entry))
|
entry.async_on_unload(entry.add_update_listener(async_reload_entry))
|
||||||
|
|
||||||
return True
|
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:
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||||
|
|
31
tests/components/github/test_init.py
Normal file
31
tests/components/github/test_init.py
Normal file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue