Skip to content

Commit eef119c

Browse files
committed
CR: harden discovery callback during full scan
1 parent 15ae851 commit eef119c

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

plugwise_usb/network/registry.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from asyncio import Task, create_task, sleep
5+
from asyncio import CancelledError, Task, create_task, sleep
66
from collections.abc import Awaitable, Callable
77
from copy import deepcopy
88
import logging
@@ -109,6 +109,24 @@ def scan_completed_callback(self, callback: Callable[[], Awaitable[None]]) -> No
109109
"""Register method to be called when a registry scan has completed."""
110110
self._scan_completed_callback = callback
111111

112+
async def _exec_node_discover_callback(
113+
self, mac: str, node_type: NodeType | None, output: bool,
114+
) -> None:
115+
"""Protect _start_node_discover() callback execution."""
116+
if self._start_node_discover is not None:
117+
try:
118+
await self._start_node_discover(mac, node_type, output)
119+
except CancelledError:
120+
raise
121+
except Exception:
122+
_LOGGER.exception(
123+
"start_node_discover callback failed for %s", mac
124+
)
125+
else:
126+
_LOGGER.debug(
127+
"No start_node_discover callback set; skipping for %s", mac
128+
)
129+
112130
# endregion
113131

114132
async def start(self) -> None:
@@ -188,7 +206,7 @@ async def update_missing_registrations_circleplus(self) -> None:
188206
continue
189207
_maintenance_registry.append(mac)
190208
if self.update_network_registration(mac):
191-
await self._start_node_discover(mac, None, False)
209+
await self._exec_node_discover_callback(mac, None, False)
192210
await sleep(self._registration_scan_delay)
193211
_LOGGER.debug("CirclePlus registry scan finished")
194212
self._scan_completed = True
@@ -209,7 +227,7 @@ async def load_registrations_from_cache(self) -> None:
209227
)
210228
for mac, nodetype in self._network_cache.nodetypes.items():
211229
self.update_network_registration(mac)
212-
await self._start_node_discover(mac, nodetype, True)
230+
await self._exec_node_discover_callback(mac, nodetype, True)
213231
await sleep(0.1)
214232
_LOGGER.info("Cache network registration discovery finished")
215233
if self._scan_completed_callback is not None:
@@ -236,7 +254,7 @@ async def register_node(self, mac: str) -> None:
236254
except StickError as exc:
237255
raise NodeError(f"{exc}") from exc
238256
if self.update_network_registration(mac):
239-
await self._start_node_discover(mac, None, False)
257+
await self._exec_node_discover_callback(mac, None, False)
240258

241259
async def unregister_node(self, mac: str) -> None:
242260
"""Unregister node from current Plugwise network."""

0 commit comments

Comments
 (0)