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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Opower: Call async_create_clientsession instead of async_get_clientsession #97541

Closed
wants to merge 1 commit into from

Conversation

tronikos
Copy link
Contributor

Proposed change

Call async_create_clientsession instead of async_get_clientsession in the coordinator.py. config_flow.py calls async_create_clientsession and login works there.
This is an attempt to fix tronikos/opower-hacs#10, #97516, and #97349

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:

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
  • I have followed the perfect PR recommendations
  • 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.

To help with the load of incoming pull requests:

@@ -50,7 +50,7 @@ def __init__(
update_interval=timedelta(hours=12),
)
self.api = Opower(
aiohttp_client.async_get_clientsession(hass),
aiohttp_client.async_create_clientsession(hass),
Copy link
Member

Choose a reason for hiding this comment

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

So, why is that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not quite sure. The 2 core bugs fail for the same reason. The library fails to parse the login page but only while getting updated data, not in the config flow. The only difference is the config flow creates its own session while coordinator uses a common session. I suspect it's because the library modifies the default user agent instead of specifying it on every HTTP request.

Copy link
Member

Choose a reason for hiding this comment

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

I suspect it's because the library modifies the default user agent instead of specifying it on every HTTP request.

That sounds familiar! Got a pointer to me for that upstream?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

@frenck frenck Jul 31, 2023

Choose a reason for hiding this comment

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

Aah yes, that shouldn't be used/set and, indeed passed on request.
We've "monkeypatched" it in Home Assistant to prevent it to work, as modifying internals of shared sessions like that causes issues (and has caused many issues in the past).

Ref:

clientsession._default_headers = MappingProxyType( # type: ignore[assignment]

TBH, from that perspective, I don't agree on the solution provided in this PR. I think we should update upstream to pass the headers in on request.

../Frenck

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I modified the library to pass the headers in each request and created #97548 to bump the dependency

@tronikos tronikos closed this Jul 31, 2023
@tronikos tronikos deleted the patch-1 branch July 31, 2023 22:38
@github-actions github-actions bot locked and limited conversation to collaborators Aug 1, 2023
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.

Forbidden request
2 participants