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

Use aiohasupervisor for addon info calls #125926

Merged
merged 3 commits into from
Sep 17, 2024
Merged

Conversation

mdegat01
Copy link
Contributor

Proposed change

Begin the process of replacing hassio/handler with a published client library on pip in aiohasupervisor. As this is our guidance to all other integration developers, our supervisor integration should follow this as well.

Since the hassio component's internal library is used all over integrations for access to supervisor information replacing handler will likely take a good number of PRs. This PR seeks only to replace the method used for obtaining addon info with the equivalent method from the library.

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 Ruff (ruff format 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.

To help with the load of incoming pull requests:

@mdegat01
Copy link
Contributor Author

Not sure how the errors in the hassio/issues tests didn't show up when I was running them locally but I'll fix those. The bluetooth ones I'm seeing the same failures when I switch to dev branch locally so I don't think those are a result of this PR? Will circle back.

@mdegat01 mdegat01 force-pushed the use-aiohasupervisor-library branch from ef4b501 to ff6a53b Compare September 16, 2024 20:03
@mdegat01 mdegat01 marked this pull request as ready for review September 16, 2024 21:11
@mdegat01
Copy link
Contributor Author

I'm going to call this ready for review. Yes there is a failure in a test in the Plugwise integration but the test appears to be flaky. I ran it locally against dev branch and it failed 2 times in 10 with no changes in between. I opened #126086 for it.

@mdegat01 mdegat01 force-pushed the use-aiohasupervisor-library branch from ff6a53b to 87f04ee Compare September 16, 2024 21:12
homeassistant/components/hassio/addon_manager.py Outdated Show resolved Hide resolved
Comment on lines +327 to +334
self._client = SupervisorClient(
base_url, os.environ.get("SUPERVISOR_TOKEN", ""), session=websession
)

@property
def client(self) -> SupervisorClient:
"""Return aiohasupervisor client."""
return self._client
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
self._client = SupervisorClient(
base_url, os.environ.get("SUPERVISOR_TOKEN", ""), session=websession
)
@property
def client(self) -> SupervisorClient:
"""Return aiohasupervisor client."""
return self._client
self.client = SupervisorClient(
base_url, os.environ.get("SUPERVISOR_TOKEN", ""), session=websession
)

Why make it protected if you're going to expose it anyway :)

Copy link
Contributor Author

@mdegat01 mdegat01 Sep 17, 2024

Choose a reason for hiding this comment

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

Because if you do it that way then someone can do this:

hassio = HassIO(...)
hassio.client = SupervisorClient(...)

By making it a property like that client is read-only on instances of HassIO. Others can access it to make calls to supervisor, they cannot change its value to something new.

Well They can but they'd have to set hassio._client in my example above and then linting would tell them not to do that.

Copy link
Member

Choose a reason for hiding this comment

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

I am not aware if we have places in the code where we explictly do this, let me check with others

Copy link
Member

Choose a reason for hiding this comment

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

Okay, talked with Martin and this would be good in its current form

homeassistant/components/hassio/handler.py Show resolved Hide resolved
Comment on lines +613 to +616
def get_supervisor_client(hass: HomeAssistant) -> SupervisorClient:
"""Return supervisor client."""
hassio: HassIO = hass.data[DOMAIN]
return hassio.client
Copy link
Member

Choose a reason for hiding this comment

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

You can use HassKey in a future PR to add typing to the hass.data so you can avoid this type overwrite (if wanted)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wasn't aware of that, that would be nice. It would definitely have to be a follow-up PR though as doing that would require a significant refactor to this integration looking at how much we use that currently.

tests/components/hassio/test_binary_sensor.py Outdated Show resolved Hide resolved
tests/components/hassio/test_init.py Show resolved Hide resolved
@home-assistant home-assistant bot marked this pull request as draft September 17, 2024 14:11
@home-assistant
Copy link

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.

@mdegat01 mdegat01 force-pushed the use-aiohasupervisor-library branch from 87f04ee to 31b51bf Compare September 17, 2024 16:42
@mdegat01 mdegat01 force-pushed the use-aiohasupervisor-library branch from 31b51bf to 8312ef4 Compare September 17, 2024 18:02
@mdegat01 mdegat01 marked this pull request as ready for review September 17, 2024 18:29
@home-assistant home-assistant bot requested a review from joostlek September 17, 2024 18:29
@joostlek joostlek merged commit 97d0d91 into dev Sep 17, 2024
39 of 40 checks passed
@joostlek joostlek deleted the use-aiohasupervisor-library branch September 17, 2024 21:22
@github-actions github-actions bot locked and limited conversation to collaborators Sep 18, 2024
@@ -43,6 +39,12 @@ class AlreadyConfigured(HomeAssistantError):
"""Raised when the router is already configured."""


@callback
def get_addon_manager(hass: HomeAssistant, slug: str) -> AddonManager:
Copy link
Member

Choose a reason for hiding this comment

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

Each add-on manager should be a singleton per add-on. We can use the singleton decorator. See other integrations that create add-on managers.

@home-assistant home-assistant unlocked this conversation Sep 19, 2024
@sdb9696
Copy link
Contributor

sdb9696 commented Sep 19, 2024

Hi, I just did a fresh installation of HA core from dev using script/setup and got a missing import error for aiohasupervisor. I think this PR is the cause although I'm not clear why a core install loads hassio as I thought that was the supervisor.

To reproduce

@MartinHjelmare
Copy link
Member

We're aware and a solution is under way.

@MartinHjelmare
Copy link
Member

The missing import problem should be solved by:
#126225

@github-actions github-actions bot locked and limited conversation to collaborators Sep 20, 2024
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.

4 participants