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

Fix for getting the gateway. #253

Merged
merged 4 commits into from
Aug 9, 2024
Merged

Fix for getting the gateway. #253

merged 4 commits into from
Aug 9, 2024

Conversation

mdeweerd
Copy link
Owner

@mdeweerd mdeweerd commented Aug 8, 2024

No description provided.

@wbyoung
Copy link
Contributor

wbyoung commented Aug 8, 2024

This seems like a better path forward than #252. Thanks for the work!

@mdeweerd
Copy link
Owner Author

mdeweerd commented Aug 8, 2024

@wbyoung Can you test it?

I can make a beta release.

@wbyoung
Copy link
Contributor

wbyoung commented Aug 8, 2024

I gave it a test, but got the following:

2024-08-08 14:45:24.931 ERROR (MainThread) [homeassistant.components.automation.store_zha_bindings] While executing automation automation.store_zha_bindings
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/automation/__init__.py", line 755, in async_trigger
    return await self.action_script.async_run(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1799, in async_run
    return await asyncio.shield(create_eager_task(run.async_run()))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 463, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 527, in _async_step
    self._handle_exception(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 557, in _handle_exception
    raise exception
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 525, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/trace.py", line 283, in async_wrapper
    await func(*args)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 939, in _async_repeat_step
    await async_run_sequence(iteration, extra_msg)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 890, in async_run_sequence
    await self._async_run_script(script)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1269, in _async_run_script
    result = await self._async_run_long_action(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 727, in _async_run_long_action
    return await long_task
           ^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1799, in async_run
    return await asyncio.shield(create_eager_task(run.async_run()))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 463, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 527, in _async_step
    self._handle_exception(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 557, in _handle_exception
    raise exception
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 525, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 764, in _async_call_service_step
    response_data = await self._async_run_long_action(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 727, in _async_run_long_action
    return await long_task
           ^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2763, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2806, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/zha_toolkit/__init__.py", line 828, in toolkit_service
    raise handler_exception
  File "/config/custom_components/zha_toolkit/__init__.py", line 784, in toolkit_service
    handler_result = await handler(
                     ^^^^^^^^^^^^^^
  File "/config/custom_components/zha_toolkit/__init__.py", line 894, in command_handler_default
    return await default.default(
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/zha_toolkit/default.py", line 45, in default
    m = await listener.hass.async_add_import_executor_job(
              ^^^^^^^^^^^^^
AttributeError: 'Gateway' object has no attribute 'hass'

zha_gw = zha.get("zha_gateway", None)
else:
zha_gw = zha.gateway
zha_gw = u.get_zha_gateway(hass)
Copy link
Contributor

Choose a reason for hiding this comment

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

In #252, I tried this as a first attempt as well, but ended up changing to getting the proxy object instead as it still had the hass property whereas the new gateway that's in the new library no longer has any knowledge of HASS.

If you're moving to requiring 2024.8, then why not just change this to the proxy object:

zha_gw_proxy: ZHAGatewayProxy = zha.gateway_proxy

And then change to get access to the application_controller to zha_gw_proxy.gateway.application_controller everywhere?

Copy link
Owner Author

Choose a reason for hiding this comment

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

I am requiring 2024.8 mainly to avoid that those on older versions update..

As far as zha_gw_proxy.gateway.application_controller goes, it's preferable to refer to a function in utils.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, the utils thing makes sense style wise. I was just getting at the fact that now it seems you have to access hass and application_controller from different objects.

@@ -50,6 +53,15 @@
MANIFEST: dict[str, str | list[str]] = {}


def get_zha_gateway(hass: HomeAssistant) -> ZHAGateway:
"""Get the ZHA gateway object."""
if parse_version(HA_VERSION) >= parse_version("2024.8"):
Copy link
Contributor

Choose a reason for hiding this comment

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

Why type check the version if in hacs.json 2024.8 is now required?

Copy link
Owner Author

Choose a reason for hiding this comment

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

To be able to restore backward compatibility. The requirement in hacs.json is to avoid updates for users that currently have something working on earlier versions.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I think I see. So this will be set so that only those who are running 2024.8 receive an update notice via HACS, but will eventually be reverted post-release?

If so, that makes sense.


try:
from homeassistant.components.zha import Gateway as ZHAGateway
except ImportError:
from homeassistant.components.zha.core.gateway import ZHAGateway

from homeassistant.components import zha
from homeassistant.components.zha import helpers as zha_helpers
Copy link
Contributor

Choose a reason for hiding this comment

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

These will fail to import if running before 2024.8, right? (The PR seems to code defensively around assuming the version may be less while also making a change to require it, so it's hard to see which direction it's going.)

Copy link
Owner Author

Choose a reason for hiding this comment

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

Thanks for the feedback.
zha and the helpers are stil valid in version before 2024.8 as far as I know, I'll test this on 2024.7.4 anyway.

@wbyoung
Copy link
Contributor

wbyoung commented Aug 8, 2024

I added some changes that fix it for me and opened a PR against the dev branch, but feel free to put them into this PR.

git remote add wbyoung [email protected]:wbyoung/zha-toolkit.git
git fetch wbyoung
git checkout dev
git reset --hard wbyoung/fix-253
git push origin dev

I'm not sure what your process is. Hopefully the changes help you get something out that fixes the issue!

@mdeweerd
Copy link
Owner Author

mdeweerd commented Aug 9, 2024

@wbyoung I adapt to the context - maybe a cherry-pick is possible.

FYI, I have this funcdtion to fetch a PR:

$ declare -f gitpr
gitpr () 
{ 
    local pr=$1;
    git fetch origin "pull/$pr/head:pr$pr" && git checkout "pr$pr"
}

@mdeweerd mdeweerd merged commit f66969f into main Aug 9, 2024
14 checks passed
@mdeweerd
Copy link
Owner Author

mdeweerd commented Aug 9, 2024

I published v1.1.18 as a beta/pre-release function that includes #254 (which I merged here).

Thank you for your help - I'll publish it once you confirm it's ok (I assume it is for @wbyoung because it matches #254 )

@wbyoung
Copy link
Contributor

wbyoung commented Aug 9, 2024

Thanks! I see the following in the HACS UI:

You are running Home Assistant version '2024.8.0', but this repository requires minimum version '2024.8' to be installed.

@mdeweerd
Copy link
Owner Author

mdeweerd commented Aug 9, 2024

I'ld call that a HACS bug, but I'll update the manifest.

@wbyoung
Copy link
Contributor

wbyoung commented Aug 9, 2024

I'ld call that a HACS bug

Yeah, probably.


For what it's worth, I was thinking about this version field after seeing that error message. I looked it up in the HACS docs, and it just describes it as a minimum required version of HA to run the integration. Your prior explanation of changing the version made sense to me, but it's inconsistent with how I've seen (and personally used) this type of field before. I think it does make sense for anyone/everyone to get the upgrade even if they're not running 2024.8 because they could update HA at some point in the future, and it's nice to have all the extras already updated and possibly working. It may be annoying to update for something that doesn't change anything for your HA version, but I doubt everyone is reading release notes carefully. Also, HACS seems to make the same assumption in the UI based on that error message I saw. I'd be concerned as a user to see something like that and think that this integration was heading towards no longer supporting older versions of HA. Just some thoughts—feel free to ignore me! 😜

@mdeweerd
Copy link
Owner Author

mdeweerd commented Aug 9, 2024

@wbyoung I agree - I also prefer to update components before the upgrade, and some do not allow that.
Currently I have set 2023.8.0 as the minimum to avoid that users are impacted while the code is adjusted for the latest version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants