fix: don't block HA startup on first nmap scan#42
Open
Nino6689 wants to merge 1 commit into
Open
Conversation
Passing update_before_add=True to async_add_entities forces HA to await the first nmap scan before completing platform setup. On a /24 network this regularly takes 30s+, producing repeated 'Setup of sensor platform network_scanner is taking over 10 seconds' warnings (issue parvez#35) and delaying overall HA startup (issue parvez#41). Switch to update_before_add=False. The entity appears in unknown state and is populated on the first scheduled poll, which matches the user request in parvez#41 ('scan in the background after HASS starts'). For a 15-minute SCAN_INTERVAL the wait is bounded and acceptable. Closes parvez#41 Refs parvez#35, parvez#37
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR adjusts how the NetworkScanner entity is added to Home Assistant by changing whether an initial update is performed before the entity is registered.
Changes:
- Disable
update_before_addwhen callingasync_add_entitiesfor theNetworkScannerentity.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # Set up the network scanner entity | ||
| scanner = NetworkScanner(hass, ip_range, mac_mappings) | ||
| async_add_entities([scanner], True) | ||
| async_add_entities([scanner], False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #41. Refs #35, #37.
Summary
custom_components/network_scanner/sensor.py:async_add_entities([scanner], True)→async_add_entities([scanner], False).async_setup_entry, so HA platform setup completes immediately.unknownstate and is populated on the first scheduledasync_update. With the existing 15-minuteSCAN_INTERVAL, that's a bounded wait.Why
`update_before_add=True` makes `async_add_entities` await `async_update` before returning. On any non-trivial network, `nmap -sn` over a /24 takes 30s+, which triggers HA's "Setup of sensor platform network_scanner is taking over 10 seconds" warning (#35) and noticeably delays overall startup (#41). `async_update` already uses `async_add_executor_job`, so the event loop wasn't being blocked — but the platform setup itself was waiting.
`update_before_add=False` is the standard HA pattern for slow polling integrations and matches the user request in #41 ("scan in the background after HASS starts").
Test plan