From 8bf885f28857f5e8fb04d5d8363ad72b51109abc Mon Sep 17 00:00:00 2001 From: Bram Date: Sat, 4 Jan 2025 22:29:02 +0100 Subject: [PATCH 1/2] fix: prevent duplicate discovery for device based discovery flow --- custom_components/powercalc/discovery.py | 26 ++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/custom_components/powercalc/discovery.py b/custom_components/powercalc/discovery.py index 6d9ea81c7..5a5f354f2 100644 --- a/custom_components/powercalc/discovery.py +++ b/custom_components/powercalc/discovery.py @@ -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") @@ -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.entity_id) + async def perform_discovery( self, source_provider: Callable[[], Awaitable[list]], From 21a39416d317a3747e325c84d7261333d5b747ac Mon Sep 17 00:00:00 2001 From: Bram Date: Sat, 4 Jan 2025 22:30:22 +0100 Subject: [PATCH 2/2] fix: typing issue --- custom_components/powercalc/discovery.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/powercalc/discovery.py b/custom_components/powercalc/discovery.py index 5a5f354f2..752e95df3 100644 --- a/custom_components/powercalc/discovery.py +++ b/custom_components/powercalc/discovery.py @@ -114,7 +114,7 @@ async def initialize_existing_entries(self) -> None: 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.entity_id) + self.initialized_flows.add(entity_id) async def perform_discovery( self,