Skip to content

Commit

Permalink
Fix services when integration is setup multiple times
Browse files Browse the repository at this point in the history
Fixes #429
  • Loading branch information
wlcrs committed Jul 25, 2023
1 parent db9f7a1 commit 1b39063
Show file tree
Hide file tree
Showing 4 changed files with 404 additions and 302 deletions.
10 changes: 5 additions & 5 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
CONF_ENABLE_PARAMETER_CONFIGURATION,
CONF_SLAVE_IDS,
CONFIGURATION_UPDATE_INTERVAL,
DATA_BRIDGES_WITH_DEVICEINFOS,
DATA_CONFIGURATION_UPDATE_COORDINATORS,
DATA_OPTIMIZER_UPDATE_COORDINATORS,
DATA_UPDATE_COORDINATORS,
Expand All @@ -37,7 +38,7 @@
SERVICES,
UPDATE_INTERVAL,
)
from .services import async_setup_services
from .services import async_setup_services, async_cleanup_services

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -180,6 +181,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
optimizers_device_infos = {}

hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
DATA_BRIDGES_WITH_DEVICEINFOS: bridges_with_device_infos,
DATA_UPDATE_COORDINATORS: update_coordinators,
DATA_CONFIGURATION_UPDATE_COORDINATORS: configuration_update_coordinators,
DATA_OPTIMIZER_UPDATE_COORDINATORS: optimizer_update_coordinators,
Expand All @@ -199,7 +201,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
raise err

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
await async_setup_services(hass, entry, bridges_with_device_infos)
await async_setup_services(hass, entry)

return True

Expand All @@ -213,9 +215,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
for update_coordinator in update_coordinators:
await update_coordinator.bridge.stop()

for service in SERVICES:
if hass.services.has_service(DOMAIN, service):
hass.services.async_remove(DOMAIN, service)
await async_cleanup_services(hass)

hass.data[DOMAIN].pop(entry.entry_id)

Expand Down
1 change: 1 addition & 0 deletions const.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
CONF_ENABLE_PARAMETER_CONFIGURATION = "enable_parameter_configuration"


DATA_BRIDGES_WITH_DEVICEINFOS = "bridges"
DATA_UPDATE_COORDINATORS = "update_coordinators"
DATA_CONFIGURATION_UPDATE_COORDINATORS = "configuration_update_coordinators"
DATA_OPTIMIZER_UPDATE_COORDINATORS = "optimizer_update_coordinators"
Expand Down
Loading

0 comments on commit 1b39063

Please sign in to comment.