From 5444d7bba31598aabf1b3a52af66f1c50cf8981b Mon Sep 17 00:00:00 2001 From: yoali11 Date: Wed, 21 Oct 2026 18:29:06 +0000 Subject: [PATCH] Poll longer summary periods less often The year sensor splits its report into ~10 requests of 30 days, and all four summary sensors polled every 5 minutes, which now hits the report API's rate limit (429). Poll per period instead: today 5 min, week 15 min, month 30 min, year 60 min. Starting or stopping a timer still refreshes all of them immediately. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- custom_components/clockify/manifest.json | 2 +- custom_components/clockify/sensor.py | 13 +++++++++++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 39b3053..8205a8e 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ Each configured project gets a device named `Clockify - ` with these en | `sensor.clockify_my_project_hours_this_month` | Hours logged this month | | `sensor.clockify_my_project_hours_this_year` | Hours logged this year | -The hours sensors refresh every 5 minutes, and immediately when a timer starts or stops. +The hours sensors refresh immediately when a timer starts or stops, and otherwise on a schedule: today every 5 minutes, this week every 15 minutes, this month every 30 minutes and this year every hour. Longer periods need more report requests (the year is split into ~10), so they are polled less often to stay within the rate limit. ## Examples diff --git a/custom_components/clockify/manifest.json b/custom_components/clockify/manifest.json index fb37aa9..a5816a9 100644 --- a/custom_components/clockify/manifest.json +++ b/custom_components/clockify/manifest.json @@ -13,5 +13,5 @@ "iot_class": "cloud_push", "issue_tracker": "https://github.com/yoali11/clockify/issues", "requirements": [], - "version": "1.1.4" + "version": "1.1.5" } \ No newline at end of file diff --git a/custom_components/clockify/sensor.py b/custom_components/clockify/sensor.py index 8fc4bc2..2a01bd7 100644 --- a/custom_components/clockify/sensor.py +++ b/custom_components/clockify/sensor.py @@ -23,6 +23,15 @@ # Poll summary statistics every 5 minutes so hours-worked totals stay current SCAN_INTERVAL = timedelta(minutes=5) +# Longer periods change little between polls but cost more report requests (the year +# is split into ~10 chunks), so poll them less often to stay under the rate limit +PERIOD_SCAN_INTERVALS = { + "today": SCAN_INTERVAL, + "week": timedelta(minutes=15), + "month": timedelta(minutes=30), + "year": timedelta(minutes=60), +} + # Clockify's Free plan rejects summary report requests spanning more than 31 days MAX_REPORT_RANGE_DAYS = 30 @@ -128,12 +137,12 @@ async def async_added_to_hass(self) -> None: self._handle_signal, ) ) - # Refresh stats every SCAN_INTERVAL automatically + # Refresh stats on a per-period interval automatically self.async_on_remove( async_track_time_interval( self.hass, self._async_interval_update, - SCAN_INTERVAL, + PERIOD_SCAN_INTERVALS.get(self._period_type, SCAN_INTERVAL), ) ) await self.async_update()