Skip to content

Commit

Permalink
Support removing entities from integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
milkboy committed Mar 8, 2022
1 parent c3cb69f commit 34c7e3c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
17 changes: 10 additions & 7 deletions custom_components/volkswagencarnet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
SERVICE_UPDATE_SCHEDULE,
SERVICE_UPDATE_PROFILE,
SERVICE_SET_CHARGER_MAX_CURRENT,
CONF_AVAILABLE_RESOURCES,
)
from .services import (
SchedulerService,
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions custom_components/volkswagencarnet/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -307,15 +308,21 @@ 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(
self._config_entry,
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)

Expand Down

0 comments on commit 34c7e3c

Please sign in to comment.