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

Cancel timer on enphase_envoy config entry unload #111406

Merged
merged 8 commits into from
Apr 24, 2024

Conversation

catsmanac
Copy link
Contributor

@catsmanac catsmanac commented Feb 25, 2024

Proposed change

Cancel timer on enphase_envoy config entry unload

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

Hey there @bdraco, @cgarwood, @dgomes, @joostlek, mind taking a look at this pull request as it has been labeled with an integration (enphase_envoy) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of enphase_envoy 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 enphase_envoy 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.

@bdraco bdraco changed the title Fix lingeringtimer warning in tests Fix lingeringtimer warning in enphase_envoy tests Feb 25, 2024
@bdraco bdraco changed the title Fix lingeringtimer warning in enphase_envoy tests Fix lingering timer warning in enphase_envoy tests Feb 25, 2024
@bdraco
Copy link
Member

bdraco commented Feb 25, 2024

I just realized the timer is marked cancel_on_shutdown so it seems like we have a bug in the test tooling where its not being cancelled

@catsmanac
Copy link
Contributor Author

catsmanac commented Feb 28, 2024

I just realized the timer is marked cancel_on_shutdown so it seems like we have a bug in the test tooling where its not being cancelled

Tested with a cleanup method added to coordinator which executes the self._cancel_token_refresh callback if existing and called from __init__.async_unload_entry. That solves the lingering timer warnings. No need anymore for the patch of this first attempt. Haven't committed it yet as it still may be hidden the potential structural problem in the test tooling you refer too.

It is called on reload as well, not on system restart.

__init__

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Unload a config entry."""
    coordinator = hass.data[DOMAIN][entry.entry_id]
    await coordinator.async_cleanup()

coordinator,py

    async def async_cleanup(self) -> None:
        """Cleanup coordinator."""
        if self._cancel_token_refresh:
            self._cancel_token_refresh()
            self._cancel_token_refresh = None

@frenck frenck added the smash Indicator this PR is close to finish for merging or closing label Mar 13, 2024
@catsmanac
Copy link
Contributor Author

Implemented envoy coordinator.async_cleanup called from async_uncload_entry to cancel token check timer and reverted the envoy_mock change.

@emontnemery emontnemery changed the title Fix lingering timer warning in enphase_envoy tests Cancel timer on enphase_envoy config entry unload Apr 24, 2024
Copy link
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @catsmanac 👍

@emontnemery emontnemery merged commit bbaa0c1 into home-assistant:dev Apr 24, 2024
24 checks passed
@bdraco
Copy link
Member

bdraco commented Apr 24, 2024

Suggested cleanup

diff --git a/homeassistant/components/enphase_envoy/__init__.py b/homeassistant/components/enphase_envoy/__init__.py
index 2cdba43453e..322f909437a 100644
--- a/homeassistant/components/enphase_envoy/__init__.py
+++ b/homeassistant/components/enphase_envoy/__init__.py
@@ -46,8 +46,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
 
 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
     """Unload a config entry."""
-    coordinator = hass.data[DOMAIN][entry.entry_id]
-    await coordinator.async_cleanup()
+    coordinator: EnphaseUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
+    coordinator.async_cancel_token_refresh()
     unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
     if unload_ok:
         hass.data[DOMAIN].pop(entry.entry_id)
diff --git a/homeassistant/components/enphase_envoy/coordinator.py b/homeassistant/components/enphase_envoy/coordinator.py
index c0852fca807..04f93098ad9 100644
--- a/homeassistant/components/enphase_envoy/coordinator.py
+++ b/homeassistant/components/enphase_envoy/coordinator.py
@@ -83,9 +83,7 @@ class EnphaseUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]):
     def _async_mark_setup_complete(self) -> None:
         """Mark setup as complete and setup token refresh if needed."""
         self._setup_complete = True
-        if self._cancel_token_refresh:
-            self._cancel_token_refresh()
-            self._cancel_token_refresh = None
+        self.async_cancel_token_refresh()
         if not isinstance(self.envoy.auth, EnvoyTokenAuth):
             return
         self._cancel_token_refresh = async_track_time_interval(
@@ -160,8 +158,9 @@ class EnphaseUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]):
 
         raise RuntimeError("Unreachable code in _async_update_data")  # pragma: no cover
 
-    async def async_cleanup(self) -> None:
-        """Cleanup coordinator."""
+    @callback
+    def async_cancel_token_refresh(self) -> None:
+        """Cancel token refresh."""
         if self._cancel_token_refresh:
             self._cancel_token_refresh()
             self._cancel_token_refresh = None

@github-actions github-actions bot locked and limited conversation to collaborators Apr 25, 2024
@catsmanac catsmanac deleted the LingeringTimer branch April 27, 2024 07:25
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
by-code-owner cla-signed code-quality has-tests integration: enphase_envoy Quality Scale: No score small-pr PRs with less than 30 lines. smash Indicator this PR is close to finish for merging or closing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants