Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add re-auth flow for OpenUV #79691

Merged
merged 5 commits into from Nov 8, 2022
Merged

Add re-auth flow for OpenUV #79691

merged 5 commits into from Nov 8, 2022

Conversation

bachya
Copy link
Contributor

@bachya bachya commented Oct 6, 2022

Proposed change

This PR adds a re-auth flow to OpenUV.

There is a peculiarity to keep in mind. The OpenUV API will, at times, return an HTTP 401/403 instead of an HTTP 5xx when there is a service issue (which eventually corrects itself). Thus, it isn't always clear whether HTTP 401/403 represents a service issue or an invalid API key. Therefore, the PR does the following:

  1. We increment a counter upon every HTTP 401/403.
  2. After a threshold is hit, a re-auth flow is initiated.
  3. If the API recovers and returns an HTTP 200 response after the re-auth flow has been created, we assume that the prior HTTP 401/403 responses were a service issue and abort the re-auth flow.

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: N/A
  • This PR is related to issue: N/A
  • Link to documentation pull request: N/A

Checklist

  • 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
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

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 link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 馃 Silver
  • 馃 Gold
  • 馃弳 Platinum

To help with the load of incoming pull requests:

@bachya
Copy link
Contributor Author

bachya commented Oct 20, 2022

Marking as draft until #80705 is merged.

@bachya bachya force-pushed the openuv-reauth branch 2 times, most recently from ef564fd to db40d4a Compare October 21, 2022 01:30
@bachya bachya marked this pull request as ready for review October 21, 2022 01:39
Comment on lines +56 to +71
@callback
def _get_active_reauth_flow(self) -> FlowResult | None:
"""Get an active reauth flow (if it exists)."""
try:
[reauth_flow] = [
flow
for flow in self.hass.config_entries.flow.async_progress_by_handler(
DOMAIN
)
if flow["context"]["source"] == "reauth"
and flow["context"]["entry_id"] == self.entry.entry_id
]
except ValueError:
return None

return reauth_flow
Copy link
Member

Choose a reason for hiding this comment

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

This looks like we could move this into the base config_entries class as #81753 will make it do the same thing.

Copy link
Member

Choose a reason for hiding this comment

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

In a follow PR we could replace https://github.com/home-assistant/core/pull/81753/files#diff-bcb932c3549daf52e1d6d8b02211ed0cc15557f5d17968f33a9daacf7ddac81bR663 with something like this code that yielded matching config entries than we could call that new function from both places and eliminate the duplicate code between here and the config_entries

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you clarify the recommendation? Wait for #81753, roll forward with this, then do a follow-on PR with your second comment?

Copy link
Member

@bdraco bdraco Nov 8, 2022

Choose a reason for hiding this comment

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

Let's merge this now and and then in a followup it would be good to make a new function that in homeassistant/config_entries.py that takes

        if any(
            flow
            for flow in hass.config_entries.flow.async_progress()
            if flow["context"].get("source") == SOURCE_REAUTH
            and flow["context"].get("entry_id") == self.entry_id
        )

and

        try:
            [reauth_flow] = [
                flow
                for flow in self.hass.config_entries.flow.async_progress_by_handler(
                    DOMAIN
                )
                if flow["context"]["source"] == "reauth"
                and flow["context"]["entry_id"] == self.entry.entry_id
            ]
        except ValueError:
            return None

and makes a new function in homeassistant/config_entries.py that yields matching flows (I should have left it as async_progress_by_handler as it would have been a bit more efficient) which both places can call instead of writing it out.

@bachya bachya merged commit 45be2a2 into home-assistant:dev Nov 8, 2022
@bachya bachya deleted the openuv-reauth branch November 8, 2022 19:02
@github-actions github-actions bot locked and limited conversation to collaborators Nov 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants