From 3963b3994b14ccf5aa37bd9755e88fed7355ba72 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 15 Apr 2024 06:42:28 -0500 Subject: [PATCH] Small cleanups to the rate limit helper (#115621) --- homeassistant/helpers/ratelimit.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/homeassistant/helpers/ratelimit.py b/homeassistant/helpers/ratelimit.py index 516d4134f76..020c7c3a0d3 100644 --- a/homeassistant/helpers/ratelimit.py +++ b/homeassistant/helpers/ratelimit.py @@ -30,7 +30,7 @@ class KeyedRateLimit: @callback def async_has_timer(self, key: Hashable) -> bool: """Check if a rate limit timer is running.""" - return bool(self._rate_limit_timers and key in self._rate_limit_timers) + return key in self._rate_limit_timers @callback def async_triggered(self, key: Hashable, now: float | None = None) -> None: @@ -41,10 +41,8 @@ class KeyedRateLimit: @callback def async_cancel_timer(self, key: Hashable) -> None: """Cancel a rate limit time that will call the action.""" - if not self._rate_limit_timers or key not in self._rate_limit_timers: - return - - self._rate_limit_timers.pop(key).cancel() + if handle := self._rate_limit_timers.pop(key, None): + handle.cancel() @callback def async_remove(self) -> None: