Skip to content

Trigger reauth on UniFi authentication errors - #183033

Open
Dzhuneyt wants to merge 4 commits into
home-assistant:devfrom
Dzhuneyt:fix-unifi-auth
Open

Dzhuneyt wants to merge 4 commits into
home-assistant:devfrom
Dzhuneyt:fix-unifi-auth

Conversation

@Dzhuneyt

@Dzhuneyt Dzhuneyt commented Sep 24, 2026 •

Copy link
Copy Markdown

Proposed change

Unauthorized and LoginRequired raised by aiounifi are not handled in UnifiDataUpdateCoordinator._async_update_data, so they reach the catch-all in DataUpdateCoordinator._async_refresh.

That branch has three properties the other branches do not:

  • It never raises ConfigEntryAuthFailed and never calls async_start_reauth_if_available(), so the config entry never enters reauth and the user is never prompted.
  • It is not gated on last_update_success, so it logs a full traceback on every refresh rather than once per failure streak.
  • Polling continues at POLL_INTERVAL (10s, per coordinator), so the request rate against the controller never drops.

The practical result is that an expired or revoked session produces an unbounded traceback loop that never self-heals, and the sustained login attempts can trip AUTHENTICATION_FAILED_LIMIT_REACHED on the controller — which is what #163913 reports. On my own instance this produced 53 MB of home-assistant.log in 11 hours.

This raises ConfigEntryAuthFailed for both exception types instead, so the coordinator stops polling and the user gets a reauth flow.

Worth noting for reviewers: aiounifi also latches Connectivity.can_retry_login to False after one failed relogin and only restores it on a successful login. Since that object is shared by every coordinator on a hub, the session cannot recover on its own once the flag is down. That is a library-side concern and is out of scope here, but it is why the loop is stable rather than self-correcting.

Note on behavior: entities now become unavailable while authentication is failing, where previously they retained their last known state indefinitely. That is the intended consequence of ConfigEntryAuthFailed and matches how every other integration signals an auth failure, so I have not treated it as a deprecation — happy to revisit if you disagree.

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:

Unauthorized and LoginRequired raised by aiounifi were not handled in
UnifiDataUpdateCoordinator._async_update_data, so they reached the
catch-all in DataUpdateCoordinator._async_refresh. That branch neither
raises ConfigEntryAuthFailed nor calls async_start_reauth_if_available,
and it is not gated on last_update_success, so an expired or revoked
session produced a full traceback on every refresh and never surfaced a
reauth flow. Polling continued at POLL_INTERVAL against the controller,
which can trip AUTHENTICATION_FAILED_LIMIT_REACHED.

Raise ConfigEntryAuthFailed instead so the coordinator stops polling and
the user is prompted to reauthenticate.
@Dzhuneyt
Dzhuneyt requested a review from Kane610 as a code owner September 24, 2026 06:58
Copilot AI balanced review requested due to automatic review settings September 24, 2026 06:58

@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 @Dzhuneyt

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 home-assistant Bot added bugfix cla-needed has-tests integration: unifi small-pr PRs with less than 30 lines. Top 100 Integration is ranked within the top 100 by usage Top 200 Integration is ranked within the top 200 by usage labels Sep 24, 2026
@home-assistant
home-assistant Bot marked this pull request as draft September 24, 2026 06:58
@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 @Kane610, mind taking a look at this pull request as it has been labeled with an integration (unifi) you are listed as a code owner for? Thanks!

Code owner commands

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

Copilot review overview

🟡 Changes recommended

Add coverage proving failed coordinators stop polling and restore the omitted required PR checklist items.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Updates UniFi authentication handling to trigger reauthentication instead of repeated polling and traceback logging.

Changes:

  • Converts Unauthorized and LoginRequired into ConfigEntryAuthFailed.
  • Adds parametrized tests for reauthentication flows.
File Description
tests/​components/​unifi/​test_hub.py Tests reauthentication for authentication errors; lacks a regression assertion that polling stops.
homeassistant/​components/​unifi/​coordinator.py Maps UniFi authentication errors to config-entry reauthentication.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread tests/components/unifi/test_hub.py
Copilot AI review requested due to automatic review settings September 24, 2026 07:24

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.

Copilot review overview

🔵 Needs a closer look

Restore all omitted PR-template checklist items before merging.

Review effort: Balanced
Findings: None

Resolved since last review (1)
Previously missed (1)

In code that hasn't changed since last review

Low severity Restore omitted PR template checklist items

homeassistant/​components/​unifi/​coordinator.py:60

Restore the omitted PR-template checklist items before merging. The description is missing the documentation checkbox and all three device/web-service dependency checkboxes; every template item must remain present even when unchecked.

Comment thread tests/components/unifi/test_hub.py Outdated
) -> None:
"""Ensure an authentication error starts reauth instead of looping."""
coordinator = (
config_entry_setup.runtime_data.entity_loader.get_data_update_coordinator(

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.

Do not get and trigger the coordinator directly. Use a freezer to let it tick instead.

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.

Fixed in 620dec1

The test no longer gets the coordinator or calls async_refresh() directly. It patches api.traffic_rules.update, then uses freezer.tick(POLL_INTERVAL) with async_fire_time_changed(hass) to trigger each poll, the same pattern as test_polling_coordinator_refreshes_after_interval.

@home-assistant
home-assistant Bot marked this pull request as draft September 24, 2026 08:06
Copilot AI review requested due to automatic review settings September 24, 2026 11:55

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.

Copilot review overview

🟡 Changes recommended

Authentication failure must stop all UniFi polling coordinators, with sibling-coordinator regression coverage.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 1 High severity

Open (1)

Comment on lines +60 to +61
except (Unauthorized, LoginRequired) as err:
raise ConfigEntryAuthFailed(str(err)) from err
Copilot AI review requested due to automatic review settings September 24, 2026 11:58

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.

Copilot review overview

🟢 Approval recommended

The authentication failures are handled appropriately and covered by regression tests.

Review effort: Balanced
Findings: 1 High severity

Open (1)

@Dzhuneyt
Dzhuneyt marked this pull request as ready for review September 24, 2026 12:03
@home-assistant
home-assistant Bot requested a review from zweckj September 24, 2026 12:03

This branch has not been deployed

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

Labels

bugfix cla-signed has-tests integration: unifi Quality Scale: silver small-pr PRs with less than 30 lines. Top 100 Integration is ranked within the top 100 by usage Top 200 Integration is ranked within the top 200 by usage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UniFi integration repeatedly polls trafficrules/trafficroutes and logs 401 every ~10s (can trigger AUTHENTICATION_FAILED_LIMIT_REACHED)

4 participants