diff --git a/custom_components/parcelsapp/const.py b/custom_components/parcelsapp/const.py index cc3920c..ed734be 100644 --- a/custom_components/parcelsapp/const.py +++ b/custom_components/parcelsapp/const.py @@ -5,4 +5,4 @@ CONF_DESTINATION_COUNTRY = "destination_country" DEFAULT_SCAN_INTERVAL = 900 SERVICE_TRACK_PACKAGE = "track_package" -SERVICE_REMOVE_PACKAGE = "remove_package" # Added new service constant +SERVICE_REMOVE_PACKAGE = "remove_package" diff --git a/custom_components/parcelsapp/sensor.py b/custom_components/parcelsapp/sensor.py index 1c471ae..3ff201a 100644 --- a/custom_components/parcelsapp/sensor.py +++ b/custom_components/parcelsapp/sensor.py @@ -3,6 +3,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.helpers import entity_registry as er from .const import DOMAIN from .coordinator import ParcelsAppCoordinator @@ -44,6 +45,12 @@ async def handle_remove_package(tracking_id: str): await entity_to_remove.async_remove() entities.remove(entity_to_remove) + # Remove entity from entity registry + entity_registry = er.async_get(hass) + entity_id = entity_to_remove.entity_id + if entity_registry.async_is_registered(entity_id): + entity_registry.async_remove(entity_id) + unsub_new_package = async_dispatcher_connect( hass, f"{DOMAIN}_new_package", handle_new_package )