3 from __future__
import annotations
6 from collections.abc
import Callable
7 from logging
import Logger
13 """Class to rate limit calls to a specific command."""
22 function: Callable[[], _R_co] |
None =
None,
23 background: bool =
False,
25 """Initialize debounce.
27 immediate: indicate if the function needs to be called right away and
28 wait <cooldown> until executing next invocation.
29 function: optional and can be instantiated later.
36 self.
_timer_task_timer_task: asyncio.TimerHandle |
None =
None
40 self.
_job_job: HassJob[[], _R_co] |
None = (
44 function, f
"debouncer cooldown={cooldown}, immediate={immediate}"
50 def function(self) -> Callable[[], _R_co] | None:
51 """Return the function being wrapped by the Debouncer."""
55 def function(self, function: Callable[[], _R_co]) ->
None:
56 """Update the function being wrapped by the Debouncer."""
58 if self.
_job_job
is None or function != self.
_job_job.target:
61 f
"debouncer cooldown={self.cooldown}, immediate={self.immediate}",
66 """Schedule a call to the function."""
72 """Check if a call should be scheduled.
74 Returns True if the function should be called immediately.
76 Returns False if there is nothing to do.
79 self.
loggerlogger.debug(
"Debouncer call ignored as shutdown has been requested.")
100 """Call the function."""
109 assert self.
_job_job
is not None
111 if task := self.
hasshass.async_run_hass_job(
119 """Handle a finished timer."""
120 assert self.
_job_job
is not None
134 if task := self.
hasshass.async_run_hass_job(
146 """Cancel any scheduled call, and prevent new runs."""
152 """Cancel any scheduled call."""
161 """Create job task, but only if pending."""
166 name = f
"debouncer {self._job} finish cooldown={self.cooldown}, immediate={self.immediate}"
168 self.
hasshass.async_create_task(
172 self.
hasshass.async_create_background_task(
178 """Schedule a timer."""
Callable[[], _R_co]|None function(self)
None _schedule_timer(self)
None async_shutdown(self)
None __init__(self, HomeAssistant hass, Logger logger, *float cooldown, bool immediate, Callable[[], _R_co]|None function=None, bool background=False)
bool _async_schedule_or_call_now(self)
None async_schedule_call(self)
None _handle_timer_finish(self)
None function(self, Callable[[], _R_co] function)