From 34c7e3ca623581aa555f9cf730860327d8bcf673 Mon Sep 17 00:00:00 2001 From: Michael Wikberg Date: Tue, 8 Mar 2022 18:02:54 +0200 Subject: [PATCH] Support removing entities from integration. --- custom_components/volkswagencarnet/__init__.py | 17 ++++++++++------- .../volkswagencarnet/config_flow.py | 11 +++++++++-- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/custom_components/volkswagencarnet/__init__.py b/custom_components/volkswagencarnet/__init__.py index d2e9a74c..584d6d91 100755 --- a/custom_components/volkswagencarnet/__init__.py +++ b/custom_components/volkswagencarnet/__init__.py @@ -53,6 +53,7 @@ SERVICE_UPDATE_SCHEDULE, SERVICE_UPDATE_PROFILE, SERVICE_SET_CHARGER_MAX_CURRENT, + CONF_AVAILABLE_RESOURCES, ) from .services import ( SchedulerService, @@ -133,14 +134,16 @@ def is_enabled(attr): """Return true if the user has enabled the resource.""" return attr in entry.options.get(CONF_RESOURCES, [attr]) + def is_new(attr): + """Return true if the resource is new.""" + return attr not in entry.options.get(CONF_AVAILABLE_RESOURCES, [attr]) + components = set() - for instrument in ( - instrument - for instrument in instruments - if instrument.component in COMPONENTS and is_enabled(instrument.slug_attr) - ): - data.instruments.add(instrument) - components.add(COMPONENTS[instrument.component]) + for instrument in (instrument for instrument in instruments if instrument.component in COMPONENTS): + # Add resource if it's enabled or new + if is_enabled(instrument.slug_attr) or (is_new(instrument.slug_attr) and not entry.pref_disable_new_entities): + data.instruments.add(instrument) + components.add(COMPONENTS[instrument.component]) for component in components: coordinator.platforms.append(component) diff --git a/custom_components/volkswagencarnet/config_flow.py b/custom_components/volkswagencarnet/config_flow.py index 822f9fd1..ce33440f 100644 --- a/custom_components/volkswagencarnet/config_flow.py +++ b/custom_components/volkswagencarnet/config_flow.py @@ -14,6 +14,7 @@ ) from homeassistant.core import callback from homeassistant.helpers.aiohttp_client import async_get_clientsession +from homeassistant.helpers.entity_registry import async_get from volkswagencarnet.vw_connection import Connection from volkswagencarnet.vw_vehicle import Vehicle @@ -307,8 +308,8 @@ async def async_step_select_instruments(self, user_input=None): removed_resources = set(data.get("options", {}).get("resources", {})) - set(options[CONF_RESOURCES]) added_resources = set(options[CONF_RESOURCES]) - set(data.get("options", {}).get("resources", {})) - # TODO: actually remove removedentities - _LOGGER.info(f"Added resources: {added_resources}, removed resoures: {removed_resources}") + + _LOGGER.info(f"Adding resources: {added_resources}, removing resources: {removed_resources}") # Update the data first self.hass.config_entries.async_update_entry( @@ -316,6 +317,12 @@ async def async_step_select_instruments(self, user_input=None): data={**data["data"]}, ) + if len(removed_resources) > 0: + entity_registry = async_get(self.hass) + # Remove all HA entities because we don't know which entities a resource has created :/ + # All entities will be recreated automatically anyway. + entity_registry.async_clear_config_entry(self._config_entry.entry_id) + # Set options return self.async_create_entry(title="", data=options)