Skip to content

Commit

Permalink
Merge pull request #2886 from bramstroker/fix/duplicate-discovery2
Browse files Browse the repository at this point in the history
prevent duplicate discovery for device based discovery flow
  • Loading branch information
bramstroker authored Jan 4, 2025
2 parents 8448f07 + 21a3941 commit c0c1ce8
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions custom_components/powercalc/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,7 @@ async def update_library_and_rediscover(self) -> None:

async def start_discovery(self) -> None:
"""Start the discovery procedure."""

# Build a list of config entries which are already setup, to prevent duplicate discovery flows
for entry in self.hass.config_entries.async_entries(DOMAIN):
if entry.unique_id:
self.initialized_flows.add(entry.unique_id)
entity_id = str(entry.data.get(CONF_ENTITY_ID))
if entity_id != DUMMY_ENTITY_ID:
self.initialized_flows.add(entity_id)
await self.initialize_existing_entries()

_LOGGER.debug("Start auto discovery")

Expand All @@ -106,6 +99,23 @@ async def start_discovery(self) -> None:

_LOGGER.debug("Done auto discovery")

async def initialize_existing_entries(self) -> None:
"""Build a list of config entries which are already setup, to prevent duplicate discovery flows"""
for entry in self.hass.config_entries.async_entries(DOMAIN):
if not entry.unique_id:
continue

self.initialized_flows.add(entry.unique_id)
entity_id = entry.data.get(CONF_ENTITY_ID)
if not entity_id or entity_id == DUMMY_ENTITY_ID:
continue

entity = await create_source_entity(str(entity_id), self.hass)
if entity and entity.device_entry:
self.initialized_flows.add(f"pc_{entity.device_entry.primary_config_entry}")
self.initialized_flows.add(f"pc_{entity.device_entry.id}")
self.initialized_flows.add(entity_id)

async def perform_discovery(
self,
source_provider: Callable[[], Awaitable[list]],
Expand Down

0 comments on commit c0c1ce8

Please sign in to comment.