diff --git a/custom_components/hacs/__init__.py b/custom_components/hacs/__init__.py index 79d9e17d384..6af3f8fc839 100644 --- a/custom_components/hacs/__init__.py +++ b/custom_components/hacs/__init__.py @@ -15,12 +15,13 @@ from homeassistant.const import Platform, __version__ as HAVERSION from homeassistant.core import HomeAssistant from homeassistant.helpers.aiohttp_client import async_get_clientsession +from homeassistant.helpers.entity_registry import async_get as async_get_entity_registry from homeassistant.helpers.event import async_call_later from homeassistant.helpers.start import async_at_start from homeassistant.loader import async_get_integration from .base import HacsBase -from .const import DOMAIN, MINIMUM_HA_VERSION, STARTUP +from .const import DOMAIN, HACS_SYSTEM_ID, MINIMUM_HA_VERSION, STARTUP from .data_client import HacsDataClient from .enums import HacsDisabledReason, HacsStage, LovelaceMode from .frontend import async_register_frontend @@ -169,6 +170,11 @@ async def async_try_startup(_=None): await async_try_startup() + # Remove old (v0-v1) sensor if it exists, can be removed in v3 + er = async_get_entity_registry(hass) + if old_sensor := er.async_get_entity_id("sensor", DOMAIN, HACS_SYSTEM_ID): + er.async_remove(old_sensor) + # Mischief managed! return True diff --git a/tests/snapshots/api-usage/tests/test_sensor_cleanuptest-sensor-cleanup.json b/tests/snapshots/api-usage/tests/test_sensor_cleanuptest-sensor-cleanup.json new file mode 100644 index 00000000000..b73c9681234 --- /dev/null +++ b/tests/snapshots/api-usage/tests/test_sensor_cleanuptest-sensor-cleanup.json @@ -0,0 +1,17 @@ +{ + "tests/test_sensor_cleanup.py::test_sensor_cleanup": { + "https://api.github.com/repos/hacs/integration": 1, + "https://api.github.com/repos/hacs/integration/contents/custom_components/hacs/manifest.json": 1, + "https://api.github.com/repos/hacs/integration/contents/hacs.json": 1, + "https://api.github.com/repos/hacs/integration/git/trees/main": 1, + "https://api.github.com/repos/hacs/integration/releases": 1, + "https://data-v2.hacs.xyz/appdaemon/data.json": 1, + "https://data-v2.hacs.xyz/critical/data.json": 1, + "https://data-v2.hacs.xyz/integration/data.json": 1, + "https://data-v2.hacs.xyz/plugin/data.json": 1, + "https://data-v2.hacs.xyz/python_script/data.json": 1, + "https://data-v2.hacs.xyz/removed/data.json": 1, + "https://data-v2.hacs.xyz/template/data.json": 1, + "https://data-v2.hacs.xyz/theme/data.json": 1 + } +} \ No newline at end of file diff --git a/tests/test_sensor_cleanup.py b/tests/test_sensor_cleanup.py new file mode 100644 index 00000000000..669d22280a4 --- /dev/null +++ b/tests/test_sensor_cleanup.py @@ -0,0 +1,32 @@ +"""Test the sensor cleanup.""" + +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from custom_components.hacs.const import DOMAIN, HACS_SYSTEM_ID + +from tests.common import create_config_entry, setup_integration + + +async def test_sensor_cleanup(hass: HomeAssistant) -> None: + """Test the sensor cleanup.""" + entity_registry = er.async_get(hass) + assert len(entity_registry.entities) == 0 + entry = entity_registry.async_get_or_create( + domain="sensor", + platform=DOMAIN, + unique_id=HACS_SYSTEM_ID, + ) + + assert entity_registry.async_get(entry.entity_id) is not None + + await setup_integration( + hass, + create_config_entry( + options={ + "appdaemon": True, + }, + ), + ) + + assert entity_registry.async_get(entry.entity_id) is None