feat: on-demand refresh — reload service + auto-refresh on container events#211
Open
lebdim wants to merge 1 commit into
Open
feat: on-demand refresh — reload service + auto-refresh on container events#211lebdim wants to merge 1 commit into
lebdim wants to merge 1 commit into
Conversation
Author
|
cc @ualex73 — this implements the |
7e95fc8 to
3e4b583
Compare
…events Add a `monitor_docker.reload` service and make the Docker event stream trigger an immediate refresh, so monitored data updates on demand instead of only every scan_interval. The sensors are push-based (should_poll=False) and the background poll loops sleep for scan_interval between fetches, so previously there was no way to refresh on demand short of restarting Home Assistant (homeassistant.update_entity is a no-op on these entities). In addition, _run_docker_events only acted on create/destroy/rename and merely logged start/stop/health, so those were reflected only on the next poll. Implementation: - Each poll loop (the docker-info loop and every container loop) owns an asyncio.Event and waits on it via asyncio.wait_for in place of the plain interval sleep, clearing it after every wake. request_refresh() sets the events; because each loop clears its own event, a request is never lost even if a loop is mid-fetch when it arrives (e.g. the rapid die+start of a container restart). - A new monitor_docker.reload service calls request_refresh() on every configured instance. - _run_docker_events calls request_refresh() on container start/stop/die/ kill/oom/pause/unpause/restart and health_status events, so any state or health change from any source (HA switch, CLI, Portainer, Watchtower, ...) refreshes immediately. This also allows raising scan_interval to reduce recorder churn without losing responsiveness. - Existing entities are reused (no teardown/re-add); entity IDs and history are preserved. Retry sleeps are left untouched. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
3e4b583 to
28ad386
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two related improvements that make monitor_docker data update on demand instead of only every
scan_interval:monitor_docker.reloadservice — force an immediate refresh of all monitored Docker data (container state, stats and engine info).start/stop/die/kill/pause/unpause/restart.Motivation
The sensors are push-based (
should_poll=False) and the background poll loopsawait asyncio.sleep(scan_interval)between fetches, so there was previously no way to refresh on demand short of restarting Home Assistant (homeassistant.update_entityis a no-op on these entities). Also,_run_docker_eventsonly acted on create/destroy/rename and merely logged start/stop, so a plain start/stop was reflected only on the next poll.This is useful for:
scan_interval(to reduce recorder churn) but still want a manual "Refresh now" button and instant reaction to start/stop.Note: the reload service registration was already stubbed out in the code (commented
async_setup_reload_service+ an emptyasync_reset_platform). This implements it via a lightweight refresh rather than a full platform reload.Implementation
asyncio.EventperDockerAPI, passed to eachDockerContainerAPI. The plain intervalasyncio.sleep(self._interval)in both the docker-info loop and each container loop is replaced by_sleep_or_refresh()=asyncio.wait_for(event.wait(), timeout=interval).request_refresh()doesevent.set()+event.clear(), waking every current waiter exactly once so they fetch immediately and push fresh values to the existing entities (no teardown/re-add; entity IDs and history preserved).monitor_docker.reloadservice callsrequest_refresh()on every configured instance._run_docker_eventsnow callsrequest_refresh()on containerstart/stop/die/kill/pause/unpause/restartactions.Testing
Live Docker host (25 containers,
scan_interval: 60,allinone):monitor_docker.reloadappears in Developer Tools → Actions; calling it refreshes container sensors within ~1s (verified a container's CPU% changing off-cycle) vs. no change without it.