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

Add an event filter to the alexa state report state change listener #115076

Merged
merged 5 commits into from
Apr 7, 2024

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented Apr 7, 2024

Proposed change

Add an event filter to the alexa state report state change listener

I am migrating more integrations away from async_track_state_change because it adds additional overhead. I do not have alexa to be able to test this change.

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.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@home-assistant
Copy link

home-assistant bot commented Apr 7, 2024

Hey there @home-assistant/cloud, @ochlocracy, @jbouwh, mind taking a look at this pull request as it has been labeled with an integration (alexa) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of alexa can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign alexa Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@jbouwh jbouwh closed this Apr 7, 2024
@jbouwh jbouwh reopened this Apr 7, 2024
@jbouwh
Copy link
Contributor

jbouwh commented Apr 7, 2024

Sorry I'll do some testing later today.

jbouwh
jbouwh previously approved these changes Apr 7, 2024
Copy link
Contributor

@jbouwh jbouwh left a comment

Choose a reason for hiding this comment

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

Did some testing, and it looks good.
Thnx @bdraco 👍

@jbouwh jbouwh dismissed their stale review April 7, 2024 10:51

Error with test

@jbouwh
Copy link
Contributor

jbouwh commented Apr 7, 2024

When I run the test locally I see some issues that seem related. We must make sure the mocking is working correctly.

FAILED tests/components/alexa/test_state_report.py::test_proactive_mode_filter_states - AssertionError: No mock registered for POST https://api.amazonalexa.com/v3/events None

Results (15.60s):
     244 passed
       1 error
ERROR:homeassistant:Error doing job: Task exception was never retrieved:   File "/usr/local/py-utils/bin/pytest", line 8, in <module>
    sys.exit(console_main())
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/config/__init__.py", line 198, in console_main
    code = main()
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/config/__init__.py", line 175, in main
    ret: Union[ExitCode, int] = config.hook.pytest_cmdline_main(
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_hooks.py", line 501, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_manager.py", line 119, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_callers.py", line 102, in _multicall
    res = hook_impl.function(*args)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/main.py", line 320, in pytest_cmdline_main
    return wrap_session(config, _main)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/main.py", line 273, in wrap_session
    session.exitstatus = doit(config, session) or 0
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/main.py", line 327, in _main
    config.hook.pytest_runtestloop(session=session)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_hooks.py", line 501, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_manager.py", line 119, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_callers.py", line 102, in _multicall
    res = hook_impl.function(*args)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/main.py", line 352, in pytest_runtestloop
    item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_hooks.py", line 501, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_manager.py", line 119, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_callers.py", line 102, in _multicall
    res = hook_impl.function(*args)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/runner.py", line 115, in pytest_runtest_protocol
    runtestprotocol(item, nextitem=nextitem)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/runner.py", line 134, in runtestprotocol
    reports.append(call_and_report(item, "call", log))
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/runner.py", line 223, in call_and_report
    call = call_runtest_hook(item, when, **kwds)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/runner.py", line 262, in call_runtest_hook
    return CallInfo.from_call(
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/runner.py", line 342, in from_call
    result: Optional[TResult] = func()
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/runner.py", line 263, in <lambda>
    lambda: ihook(item=item, **kwds), when=when, reraise=reraise
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_hooks.py", line 501, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_manager.py", line 119, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_callers.py", line 102, in _multicall
    res = hook_impl.function(*args)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/runner.py", line 170, in pytest_runtest_call
    item.runtest()
  File "/usr/local/lib/python3.12/site-packages/pytest_asyncio/plugin.py", line 436, in runtest
    super().runtest()
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/python.py", line 1831, in runtest
    self.ihook.pytest_pyfunc_call(pyfuncitem=self)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_hooks.py", line 501, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_manager.py", line 119, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_callers.py", line 102, in _multicall
    res = hook_impl.function(*args)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/python.py", line 194, in pytest_pyfunc_call
    result = testfunction(**testargs)
  File "/usr/local/lib/python3.12/site-packages/pytest_asyncio/plugin.py", line 897, in inner
    _loop.run_until_complete(task)
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 672, in run_until_complete
    self.run_forever()
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 639, in run_forever
    self._run_once()
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 1977, in _run_once
    handle._run()
  File "/usr/local/lib/python3.12/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/workspaces/core/tests/components/alexa/test_state_report.py", line 748, in test_proactive_mode_filter_states
    hass.states.async_set(
  File "/workspaces/core/homeassistant/core.py", line 2196, in async_set
    self._bus._async_fire(  # pylint: disable=protected-access
  File "/workspaces/core/homeassistant/core.py", line 1456, in _async_fire
    self._hass.async_run_hass_job(job, event)
  File "/workspaces/core/homeassistant/core.py", line 843, in async_run_hass_job
    return self.async_add_hass_job(
  File "/workspaces/core/homeassistant/core.py", line 683, in async_add_hass_job
    task = create_eager_task(
  File "/workspaces/core/homeassistant/util/async_.py", line 27, in create_eager_task
    return Task(
Traceback (most recent call last):
  File "/workspaces/core/homeassistant/components/alexa/state_report.py", line 337, in _async_entity_state_listener
    await async_send_changereport_message(
  File "/workspaces/core/homeassistant/components/alexa/state_report.py", line 390, in async_send_changereport_message
    response = await session.post(
               ^^^^^^^^^^^^^^^^^^^
  File "/workspaces/core/tests/test_util/aiohttp.py", line 157, in match_request
    raise AssertionError(f"No mock registered for {method.upper()} {url} {params}")
AssertionError: No mock registered for POST https://api.amazonalexa.com/v3/events None
ERROR:homeassistant:Error doing job: Task exception was never retrieved:   File "/usr/local/py-utils/bin/pytest", line 8, in <module>
    sys.exit(console_main())
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/config/__init__.py", line 198, in console_main
    code = main()
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/config/__init__.py", line 175, in main
    ret: Union[ExitCode, int] = config.hook.pytest_cmdline_main(
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_hooks.py", line 501, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_manager.py", line 119, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_callers.py", line 102, in _multicall
    res = hook_impl.function(*args)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/main.py", line 320, in pytest_cmdline_main
    return wrap_session(config, _main)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/main.py", line 273, in wrap_session
    session.exitstatus = doit(config, session) or 0
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/main.py", line 327, in _main
    config.hook.pytest_runtestloop(session=session)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_hooks.py", line 501, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_manager.py", line 119, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_callers.py", line 102, in _multicall
    res = hook_impl.function(*args)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/main.py", line 352, in pytest_runtestloop
    item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_hooks.py", line 501, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_manager.py", line 119, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_callers.py", line 102, in _multicall
    res = hook_impl.function(*args)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/runner.py", line 115, in pytest_runtest_protocol
    runtestprotocol(item, nextitem=nextitem)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/runner.py", line 134, in runtestprotocol
    reports.append(call_and_report(item, "call", log))
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/runner.py", line 223, in call_and_report
    call = call_runtest_hook(item, when, **kwds)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/runner.py", line 262, in call_runtest_hook
    return CallInfo.from_call(
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/runner.py", line 342, in from_call
    result: Optional[TResult] = func()
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/runner.py", line 263, in <lambda>
    lambda: ihook(item=item, **kwds), when=when, reraise=reraise
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_hooks.py", line 501, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_manager.py", line 119, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_callers.py", line 102, in _multicall
    res = hook_impl.function(*args)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/runner.py", line 170, in pytest_runtest_call
    item.runtest()
  File "/usr/local/lib/python3.12/site-packages/pytest_asyncio/plugin.py", line 436, in runtest
    super().runtest()
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/python.py", line 1831, in runtest
    self.ihook.pytest_pyfunc_call(pyfuncitem=self)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_hooks.py", line 501, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_manager.py", line 119, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/pluggy/_callers.py", line 102, in _multicall
    res = hook_impl.function(*args)
  File "/usr/local/py-utils/venvs/pytest/lib/python3.12/site-packages/_pytest/python.py", line 194, in pytest_pyfunc_call
    result = testfunction(**testargs)
  File "/usr/local/lib/python3.12/site-packages/pytest_asyncio/plugin.py", line 897, in inner
    _loop.run_until_complete(task)
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 672, in run_until_complete
    self.run_forever()
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 639, in run_forever
    self._run_once()
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 1977, in _run_once
    handle._run()
  File "/usr/local/lib/python3.12/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/workspaces/core/tests/components/alexa/test_state_report.py", line 761, in test_proactive_mode_filter_states
    hass.states.async_set(
  File "/workspaces/core/homeassistant/core.py", line 2196, in async_set
    self._bus._async_fire(  # pylint: disable=protected-access
  File "/workspaces/core/homeassistant/core.py", line 1456, in _async_fire
    self._hass.async_run_hass_job(job, event)
  File "/workspaces/core/homeassistant/core.py", line 843, in async_run_hass_job
    return self.async_add_hass_job(
  File "/workspaces/core/homeassistant/core.py", line 683, in async_add_hass_job
    task = create_eager_task(
  File "/workspaces/core/homeassistant/util/async_.py", line 27, in create_eager_task
    return Task(
Traceback (most recent call last):
  File "/workspaces/core/homeassistant/components/alexa/state_report.py", line 337, in _async_entity_state_listener
    await async_send_changereport_message(
  File "/workspaces/core/homeassistant/components/alexa/state_report.py", line 390, in async_send_changereport_message
    response = await session.post(
               ^^^^^^^^^^^^^^^^^^^
  File "/workspaces/core/tests/test_util/aiohttp.py", line 157, in match_request
    raise AssertionError(f"No mock registered for {method.upper()} {url} {params}")
AssertionError: No mock registered for POST https://api.amazonalexa.com/v3/events None

@bdraco
Copy link
Member Author

bdraco commented Apr 7, 2024

Mine passes locally as well as the CI so I’m going to have a hard time figuring out what’s wrong there. Any chance you get a better trace?

@jbouwh
Copy link
Contributor

jbouwh commented Apr 7, 2024

Mine passes locally as well as the CI so I’m going to have a hard time figuring out what’s wrong there. Any chance you get a better trace?

I could have a look

Copy link
Contributor

@jbouwh jbouwh left a comment

Choose a reason for hiding this comment

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

LGTM,
Thnx @bdraco 👍

@bdraco bdraco marked this pull request as ready for review April 7, 2024 12:53
@bdraco bdraco requested a review from ochlocracy as a code owner April 7, 2024 12:53
@bdraco
Copy link
Member Author

bdraco commented Apr 7, 2024

Thank you for fixing the test 🚀

@jbouwh
Copy link
Contributor

jbouwh commented Apr 7, 2024

Code coverage is not affected:

homeassistant/components/alexa/state_report.py        228      3    99%   270, 316, 335

The uncovered lines are not modified by this PR

@bdraco bdraco merged commit 5630b36 into dev Apr 7, 2024
33 of 34 checks passed
@bdraco bdraco deleted the alexa_filter branch April 7, 2024 13:14
@github-actions github-actions bot locked and limited conversation to collaborators Apr 8, 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.

None yet

3 participants