Skip to content

Commit 9b9c665

Browse files
committed
reduce complexity by splitting full and quick version of registration collection
1 parent c2174c9 commit 9b9c665

File tree

1 file changed

+41
-28
lines changed

1 file changed

+41
-28
lines changed

plugwise_usb/network/registry.py

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ async def start(self) -> None:
108108
if self._cache_enabled:
109109
await self.restore_network_cache()
110110
await self.load_registry_from_cache()
111-
await self.update_missing_registrations(quick=True)
111+
await self.update_missing_registrations_quick()
112112

113113
async def restore_network_cache(self) -> None:
114114
"""Restore previously saved cached network and node information."""
@@ -179,10 +179,10 @@ def update_network_registration(
179179
if self._network_cache is not None:
180180
self._network_cache.update_registration(address, mac, node_type)
181181

182-
async def update_missing_registrations(self, quick: bool = False) -> None: # noqa: PLR0912
183-
"""Retrieve all unknown network registrations from network controller."""
182+
async def update_missing_registrations_full(self) -> None:
183+
"""Full retrieval of all unknown network registrations from network controller."""
184184
for address in range(0, 64):
185-
if self._registry.get(address) is not None and not quick:
185+
if self._registry.get(address) is not None:
186186
mac, _ = self._registry[address]
187187
if mac == "":
188188
self._first_free_address = min(self._first_free_address, address)
@@ -194,37 +194,50 @@ async def update_missing_registrations(self, quick: bool = False) -> None: # no
194194
self._first_free_address = min(
195195
self._first_free_address, nextaddress
196196
)
197-
if quick:
198-
break
199197
_LOGGER.debug(
200198
"Network registration at address %s is %s",
201199
str(nextaddress),
202200
"'empty'" if mac == "" else f"set to {mac}",
203201
)
204202
self.update_network_registration(nextaddress, mac, None)
205-
await sleep(0.1)
206-
if not quick:
207-
await sleep(10)
208-
if quick:
209-
if self._registration_task is None or self._registration_task.done():
210-
self._registration_task = create_task(
211-
self.update_missing_registrations(quick=False)
203+
await sleep(10)
204+
_LOGGER.debug("Full network registration finished")
205+
self._scan_completed = True
206+
if self._cache_enabled:
207+
_LOGGER.debug("Full network registration finished, save to cache")
208+
await self.save_registry_to_cache()
209+
_LOGGER.debug("Full network registration finished, post")
210+
_LOGGER.info("Full network discovery completed")
211+
if self._full_scan_finished is not None:
212+
await self._full_scan_finished()
213+
self._full_scan_finished = None
214+
215+
async def update_missing_registrations_quick(self) -> None:
216+
"""Quick retrieval of all unknown network registrations from network controller."""
217+
for address in range(0, 64):
218+
registration = await self.retrieve_network_registration(address, False)
219+
if registration is not None:
220+
nextaddress, mac = registration
221+
if mac == "":
222+
self._first_free_address = min(
223+
self._first_free_address, nextaddress
224+
)
225+
break
226+
_LOGGER.debug(
227+
"Network registration at address %s is %s",
228+
str(nextaddress),
229+
"'empty'" if mac == "" else f"set to {mac}",
212230
)
213-
if self._quick_scan_finished is not None:
214-
await self._quick_scan_finished()
215-
self._quick_scan_finished = None
216-
_LOGGER.info("Quick network registration discovery finished")
217-
else:
218-
_LOGGER.debug("Full network registration finished")
219-
self._scan_completed = True
220-
if self._cache_enabled:
221-
_LOGGER.debug("Full network registration finished, save to cache")
222-
await self.save_registry_to_cache()
223-
_LOGGER.debug("Full network registration finished, post")
224-
_LOGGER.info("Full network discovery completed")
225-
if self._full_scan_finished is not None:
226-
await self._full_scan_finished()
227-
self._full_scan_finished = None
231+
self.update_network_registration(nextaddress, mac, None)
232+
await sleep(0.1)
233+
if self._registration_task is None or self._registration_task.done():
234+
self._registration_task = create_task(
235+
self.update_missing_registrations_full()
236+
)
237+
if self._quick_scan_finished is not None:
238+
await self._quick_scan_finished()
239+
self._quick_scan_finished = None
240+
_LOGGER.info("Quick network registration discovery finished")
228241

229242
def update_node_registration(self, mac: str) -> int:
230243
"""Register (re)joined node to Plugwise network and return network address."""

0 commit comments

Comments
 (0)