Skip to content

Allow integrations to contribute serial port scanning helpers#168660

Draft
puddly wants to merge 7 commits intohome-assistant:devfrom
puddly:puddly/serial-port-scanning-contributions
Draft

Allow integrations to contribute serial port scanning helpers#168660
puddly wants to merge 7 commits intohome-assistant:devfrom
puddly:puddly/serial-port-scanning-contributions

Conversation

@puddly
Copy link
Copy Markdown
Contributor

@puddly puddly commented Apr 20, 2026

Proposed change

This PR adds a new feature to the USB integration: usb.async_register_serial_port_scanner. This function allows an integration to register itself to contribute serial devices to serial scans. I've added two integrations to the default implementation as the motivating use cases:

  • ESPHome: serial proxies are contributed as esphome:// URIs. Note: this simplified implementation will be replaced in a future PR with one that avoids sharing the device IP or noise PSK in URIs.
  • Yellow: the Yellow radio replaces the nameless /dev/ttyAMA1 serial port with one that has metadata.

Screenshot:
image

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies a diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

Copilot AI review requested due to automatic review settings April 20, 2026 21:55
@home-assistant home-assistant bot added cla-signed has-tests integration: esphome integration: homeassistant_yellow integration: usb new-feature Top 100 Integration is ranked within the top 100 by usage Top 200 Integration is ranked within the top 200 by usage Top 50 Integration is ranked within the top 50 by usage labels Apr 20, 2026
@home-assistant
Copy link
Copy Markdown

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (homeassistant_yellow) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of homeassistant_yellow can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign homeassistant_yellow Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

@home-assistant
Copy link
Copy Markdown

Hey there @bdraco, mind taking a look at this pull request as it has been labeled with an integration (usb) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of usb can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign usb Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

@home-assistant
Copy link
Copy Markdown

Hey there @jesserockz, @kbx81, @bdraco, mind taking a look at this pull request as it has been labeled with an integration (esphome) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of esphome can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign esphome Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR extends the usb system integration with a registry mechanism so other integrations can contribute/override results returned by serial port scans (e.g., virtual serial proxies and enhanced metadata for known ports).

Changes:

  • Add usb.async_register_serial_port_scanner and route usb.async_scan_serial_ports through the USB integration runtime to merge real scan results with contributed ports.
  • Register scanner contributors in ESPHome (serial proxies as esphome:// URIs) and Home Assistant Yellow (enrich /dev/ttyAMA1 with metadata).
  • Add/adjust test coverage for contributed scanners and the new scan behavior.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
homeassistant/components/usb/init.py Adds scanner registration API and merges contributed serial ports into scans.
homeassistant/components/usb/utils.py Removes the old standalone async_scan_serial_ports helper.
homeassistant/components/esphome/init.py Registers an ESPHome serial proxy scanner that emits esphome:// ports.
homeassistant/components/esphome/manifest.json Ensures ESPHome setup ordering includes usb.
homeassistant/components/homeassistant_yellow/init.py Registers a Yellow scanner to add metadata to the built-in radio port.
homeassistant/components/homeassistant_yellow/manifest.json Declares usb as a dependency for Yellow.
tests/components/usb/init.py Updates patch target for serial scanning to the new module location.
tests/components/usb/test_init.py Adds a test verifying contributed ports override/append to scan results.
tests/components/esphome/test_init.py Adds tests for ESPHome scanner output and availability handling.
tests/components/homeassistant_yellow/test_init.py Adds test asserting Yellow contributes/enriches its radio serial port.

self.initial_scan_done = False
self._initial_scan_callbacks: list[CALLBACK_TYPE] = []
self._port_event_callbacks: set[PORT_EVENT_CALLBACK_TYPE] = set()
self._serial_port_scanners: set[SERIAL_PORT_SCANNER_TYPE] = set()
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

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

Make serial port scanner overrides deterministic by preserving registration order instead of using a set, since set iteration order makes the 'last writer wins' behavior undefined when multiple scanners contribute the same device path.

Copilot uses AI. Check for mistakes.
Comment on lines +359 to +360
for port in scanner(self.hass):
ports[port.device] = port
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

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

Isolate failures in contributed scanners by catching and logging exceptions per scanner so one misbehaving integration doesn't break serial port listing and discovery flows.

Suggested change
for port in scanner(self.hass):
ports[port.device] = port
try:
for port in scanner(self.hass):
ports[port.device] = port
except Exception:
_LOGGER.exception(
"Error while scanning serial ports with %s", scanner
)

Copilot uses AI. Check for mistakes.
Comment on lines +60 to +78
noise_psk = entry.data.get(CONF_NOISE_PSK) or None

for index, proxy in enumerate(device_info.serial_proxies):
query = {"port_name": proxy.name}

if noise_psk is not None:
query["key"] = noise_psk

if password is not None:
query["password"] = password

ports.append(
SerialDevice(
device=str(
URL.build(
scheme="esphome",
host=client.connected_address,
port=client.port,
query=query,
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

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

Avoid embedding secrets (Noise PSK/password) and potentially sensitive connection info in the contributed esphome:// port URI, since it can be surfaced in the UI/logs and leak credentials; prefer an opaque identifier (e.g., entry_id + proxy name) that ESPHome can resolve to connection details internally.

Copilot uses AI. Check for mistakes.
Comment on lines +223 to +244
),
SerialDevice(
device=f"esphome://10.0.0.2:6053/?port_name=uart0&key={quote(noise_psk, safe='')}",
serial_number="11:22:33:44:55:AA-0",
manufacturer="Espressif",
description="Noise ESP (uart0)",
),
SerialDevice(
device="esphome://10.0.0.3:6053/?port_name=uart0&password=secret",
serial_number="11:22:33:44:55:AA-0",
manufacturer="Espressif",
description="Password ESP (uart0)",
),
SerialDevice(
device="esphome://10.0.0.3:6053/?port_name=uart1&password=secret",
serial_number="11:22:33:44:55:AA-1",
manufacturer="Espressif",
description="Password ESP (uart1)",
),
]


Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

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

Update the test expectations to avoid asserting that secrets are present in the serial port URI (e.g., password=secret / key=...), so the tests don't lock in credential leakage as the intended behavior.

Copilot uses AI. Check for mistakes.
Comment thread homeassistant/components/usb/__init__.py
query=query,
)
),
serial_number=f"{device_info.mac_address}-{index}",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is okay as long as we all agree that these are ephemeral. Otherwise they should include the port name instead

device=RADIO_DEVICE,
serial_number=None,
manufacturer=MANUFACTURER,
description="Yellow Radio",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we say it's connected to zigbee chip?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants