Skip to content

Migrate unifi_direct from DeviceScanner to ScannerEntity and add ConfigFlow#171991

Open
PaulVanSchayck wants to merge 28 commits into
home-assistant:devfrom
PaulVanSchayck:unifi_direct2
Open

Migrate unifi_direct from DeviceScanner to ScannerEntity and add ConfigFlow#171991
PaulVanSchayck wants to merge 28 commits into
home-assistant:devfrom
PaulVanSchayck:unifi_direct2

Conversation

@PaulVanSchayck

@PaulVanSchayck PaulVanSchayck commented May 23, 2026

Copy link
Copy Markdown

Breaking change

This PR introduces ConfigFlow support for unifi_direct. It also has automatic migration from the existing yaml.

Proposed change

Migrate unifi_direct from DeviceScanner to ScannerEntity and add ConfigFlow.

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

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 May 23, 2026 19:32
@home-assistant home-assistant Bot added cla-needed config-flow This integration migrates to the UI by adding a config flow has-tests labels May 23, 2026

@home-assistant home-assistant Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi @PaulVanSchayck

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant

Copy link
Copy Markdown
Contributor

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant

Copy link
Copy Markdown
Contributor

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

Code owner commands

Code owners of unifi_direct 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 unifi_direct 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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 modernizes the unifi_direct integration by adding a config flow and refactoring device tracking to use a DataUpdateCoordinator, along with new tests and generated metadata updates.

Changes:

  • Added config flow support (including YAML import) and marked the integration as config_flow: true.
  • Reworked device tracker to be config-entry based and coordinator-driven.
  • Introduced test suite + fixtures for config flow and device tracker behavior.

Reviewed changes

Copilot reviewed 12 out of 14 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
homeassistant/components/unifi_direct/__init__.py Sets up/unloads the integration via config entries and forwards to platforms.
homeassistant/components/unifi_direct/config_flow.py Implements UI + YAML import config flow for UniFi AP Direct.
homeassistant/components/unifi_direct/coordinator.py Adds coordinator and connection validation helpers.
homeassistant/components/unifi_direct/const.py Introduces integration constants.
homeassistant/components/unifi_direct/device_tracker.py Migrates legacy YAML setup to import + adds coordinator-backed tracker entities.
homeassistant/components/unifi_direct/manifest.json Declares config_flow and sets integration_type to device.
homeassistant/components/unifi_direct/strings.json Adds issue translations for YAML import connection failures.
homeassistant/generated/config_flows.py Registers unifi_direct as having a config flow (generated).
homeassistant/generated/integrations.json Updates integration metadata for config flow and type (generated).
tests/components/unifi_direct/conftest.py Adds fixtures for config entry and UniFiAP mocking.
tests/components/unifi_direct/test_config_flow.py Adds config flow tests (success + cannot_connect + import).
tests/components/unifi_direct/test_device_tracker.py Adds device tracker entity creation + legacy YAML import tests.
tests/components/unifi_direct/__init__.py Adds test package marker/docstring.
CODEOWNERS Adds code ownership for the new test directory.

Comment thread homeassistant/components/unifi_direct/device_tracker.py
Comment thread homeassistant/components/unifi_direct/device_tracker.py
Comment thread homeassistant/components/unifi_direct/config_flow.py Outdated
Comment thread homeassistant/components/unifi_direct/coordinator.py Outdated
Comment thread homeassistant/components/unifi_direct/device_tracker.py
Comment thread tests/components/unifi_direct/test_config_flow.py
Comment thread tests/components/unifi_direct/test_config_flow.py
Copilot AI review requested due to automatic review settings May 24, 2026 18:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 12 out of 14 changed files in this pull request and generated 4 comments.

Comment thread tests/components/unifi_direct/test_config_flow.py
Comment thread homeassistant/components/unifi_direct/device_tracker.py Outdated
Comment thread homeassistant/components/unifi_direct/device_tracker.py
Comment thread homeassistant/components/unifi_direct/coordinator.py Outdated
@PaulVanSchayck PaulVanSchayck marked this pull request as ready for review May 24, 2026 19:12
Copilot AI review requested due to automatic review settings May 24, 2026 19:12
@home-assistant home-assistant Bot dismissed their stale review May 24, 2026 19:12

Stale

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 12 out of 14 changed files in this pull request and generated 3 comments.

Comment thread homeassistant/components/unifi_direct/device_tracker.py Outdated
Comment thread homeassistant/components/unifi_direct/device_tracker.py
Comment thread homeassistant/components/unifi_direct/coordinator.py Outdated
Copilot AI review requested due to automatic review settings June 7, 2026 21:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Comment thread homeassistant/components/unifi_direct/coordinator.py
Comment thread homeassistant/components/unifi_direct/device_tracker.py
Comment thread tests/components/unifi_direct/test_device_tracker.py
Comment thread homeassistant/components/unifi_direct/config_flow.py
Comment on lines +52 to +59
"""Initialize the coordinator using a config entry."""
self.config_entry = config_entry
self.host = config_entry.data[CONF_HOST]
self.username = config_entry.data[CONF_USERNAME]
self.password = config_entry.data[CONF_PASSWORD]
self.port = config_entry.data.get(CONF_PORT, DEFAULT_SSH_PORT)

self.ap = UniFiAP(

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

test_device_tracker_entities_created already tests the controller

Copilot AI review requested due to automatic review settings June 7, 2026 21:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 12 out of 14 changed files in this pull request and generated 3 comments.

Comment thread homeassistant/components/unifi_direct/device_tracker.py
Comment thread homeassistant/components/unifi_direct/device_tracker.py
Comment thread homeassistant/components/unifi_direct/coordinator.py
@PaulVanSchayck PaulVanSchayck marked this pull request as ready for review June 7, 2026 22:07
Copilot AI review requested due to automatic review settings June 7, 2026 22:07
@home-assistant home-assistant Bot requested a review from joostlek June 7, 2026 22:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 12 out of 14 changed files in this pull request and generated 3 comments.

Comment thread homeassistant/components/unifi_direct/device_tracker.py
Comment thread tests/components/unifi_direct/test_device_tracker.py Outdated
Comment thread homeassistant/components/unifi_direct/coordinator.py Outdated
Comment thread homeassistant/components/unifi_direct/coordinator.py Outdated
Comment on lines +32 to +42
self.host = config_entry.data[CONF_HOST]
self.username = config_entry.data[CONF_USERNAME]
self.password = config_entry.data[CONF_PASSWORD]
self.port = config_entry.data.get(CONF_PORT, DEFAULT_SSH_PORT)

self.ap = UniFiAP(
target=self.host,
username=self.username,
password=self.password,
port=self.port,
)

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.

you don't need the parameters after assignment right? might as well just pick them from the config entry directly

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I've kept self.host

Comment thread homeassistant/components/unifi_direct/device_tracker.py Outdated
Comment thread homeassistant/components/unifi_direct/device_tracker.py Outdated
@home-assistant home-assistant Bot marked this pull request as draft June 8, 2026 10:10
Comment on lines +39 to +56
@pytest.fixture
def mock_unifiap() -> Generator[MagicMock]:
"""Mock UniFiAP to return known clients."""
with patch("homeassistant.components.unifi_direct.coordinator.UniFiAP") as mock:
ap_instance = MagicMock()
ap_instance.get_clients.return_value = MOCK_DEVICE_DATA
mock.return_value = ap_instance
yield mock


@pytest.fixture
def mock_unifiap_validate() -> Generator[MagicMock]:
"""Mock UniFiAP for validate connection path."""
with patch("homeassistant.components.unifi_direct.coordinator.UniFiAP") as mock:
ap_instance = MagicMock()
ap_instance.get_clients.return_value = MOCK_DEVICE_DATA
mock.return_value = ap_instance
yield mock

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 isn't fixed yet, you can combine these, cheak mealie for an example

Comment thread tests/components/unifi_direct/test_config_flow.py
Comment thread tests/components/unifi_direct/test_config_flow.py Outdated
@PaulVanSchayck

Copy link
Copy Markdown
Author

I'm seeing the following in my log file, without having enabled any extra logging

2026-06-08 22:13:31.052 INFO (Thread-10) [paramiko.transport] Connected (version 2.0, client dropbear_2022.83)
2026-06-08 22:13:31.340 INFO (Thread-10) [paramiko.transport] Authentication (password) successful

I'm wondering if something is wrong with the log setup, and I should somehow mark paramiko.

@PaulVanSchayck PaulVanSchayck marked this pull request as ready for review June 8, 2026 20:20
Copilot AI review requested due to automatic review settings June 8, 2026 20:20
@home-assistant home-assistant Bot requested a review from joostlek June 8, 2026 20:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 12 out of 14 changed files in this pull request and generated 2 comments.

Comment on lines +110 to +115
def __init__(self, coordinator: UniFiDirectDataUpdateCoordinator, mac: str) -> None:
"""Initialize the tracked device."""
super().__init__(coordinator)
self._mac = mac
device = coordinator.data.get(mac, {})
self._attr_name = device.get("hostname") or mac

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I don't think that's needed. ScannerEntity already defines a unique_id(), and uses the mac address for that. This is a globally unique identifier.

Comment on lines +24 to +28
def validate_connection_data(data: dict[str, Any]) -> None:
"""Validate connection using a config-style dict.

Kept for config flow compatibility.
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrate unifi_direct from DeviceScanner to ScannerEntity

3 participants