Skip to content

fix: don't block HA startup on first nmap scan#42

Open
Nino6689 wants to merge 1 commit into
parvez:mainfrom
Nino6689:fix/non-blocking-startup
Open

fix: don't block HA startup on first nmap scan#42
Nino6689 wants to merge 1 commit into
parvez:mainfrom
Nino6689:fix/non-blocking-startup

Conversation

@Nino6689

Copy link
Copy Markdown

Closes #41. Refs #35, #37.

Summary

  • One-line change in custom_components/network_scanner/sensor.py: async_add_entities([scanner], True)async_add_entities([scanner], False).
  • The first nmap scan no longer runs synchronously inside async_setup_entry, so HA platform setup completes immediately.
  • The entity appears in unknown state and is populated on the first scheduled async_update. With the existing 15-minute SCAN_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

  • HA starts without the `Setup of sensor platform network_scanner is taking over 10 seconds` warning.
  • After ≤ `SCAN_INTERVAL`, `sensor.network_scanner` populates with device count + `devices` attribute as before.
  • No regression to options-flow or config-flow paths (untouched).

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
Copilot AI review requested due to automatic review settings June 13, 2026 15:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_add when calling async_add_entities for the NetworkScanner entity.

💡 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Halts startup for a long time

2 participants