From 4e01eaf865bf9d2c4a69e7916e9de88daa1b8609 Mon Sep 17 00:00:00 2001 From: yoali11 Date: Wed, 14 Oct 2026 13:52:30 +0000 Subject: [PATCH] Stay within Free plan API rate limit - Disable HA's built-in polling; the sensors already schedule their own updates, so every poll ran twice. - Compute the running timer's elapsed time from the known start time instead of calling the in-progress endpoint on every poll (Free plan allows only 30 regular API requests per hour). - Keep the previous value when a summary report request fails instead of publishing 0. Co-Authored-By: Claude Opus 5.5 --- custom_components/clockify/entity.py | 2 ++ custom_components/clockify/manifest.json | 2 +- custom_components/clockify/sensor.py | 43 ++++++------------------ 3 files changed, 14 insertions(+), 33 deletions(-) diff --git a/custom_components/clockify/entity.py b/custom_components/clockify/entity.py index a5645e4..7e6d887 100644 --- a/custom_components/clockify/entity.py +++ b/custom_components/clockify/entity.py @@ -12,6 +12,8 @@ class ClockifyEntity(Entity): """Base entity that groups Clockify entities under a per-project device.""" _attr_has_entity_name = True + # Updates are pushed by webhooks or scheduled explicitly; HA's own polling would double API calls + _attr_should_poll = False def __init__(self, entry: ConfigEntry) -> None: """Initialize the entity and its associated device.""" diff --git a/custom_components/clockify/manifest.json b/custom_components/clockify/manifest.json index 54e93a4..e8a9f4b 100644 --- a/custom_components/clockify/manifest.json +++ b/custom_components/clockify/manifest.json @@ -11,5 +11,5 @@ "iot_class": "cloud_push", "issue_tracker": "https://github.com/yoali11/clockify/issues", "requirements": [], - "version": "1.0.0" + "version": "1.0.1" } \ No newline at end of file diff --git a/custom_components/clockify/sensor.py b/custom_components/clockify/sensor.py index 53b8c30..8fc4bc2 100644 --- a/custom_components/clockify/sensor.py +++ b/custom_components/clockify/sensor.py @@ -15,7 +15,7 @@ from homeassistant.helpers.event import async_track_time_interval from homeassistant.util import dt as dt_util -from .const import DOMAIN, get_in_progress_time_entries_url, get_update_signal +from .const import DOMAIN, get_update_signal from .entity import ClockifyEntity _LOGGER = logging.getLogger(__name__) @@ -206,6 +206,7 @@ async def async_update(self) -> None: data = await response.json() total_seconds += self._extract_total_time(data) else: + # Keep the previous value rather than publishing a partial total _LOGGER.warning( "Clockify summary report API returned status %s for %s (%s - %s)", response.status, @@ -213,9 +214,10 @@ async def async_update(self) -> None: chunk_start, chunk_end, ) + return # Add running timer elapsed duration - total_seconds += await self._get_running_seconds(entry_data, start_dt, end_dt) + total_seconds += self._get_running_seconds(entry_data, start_dt, end_dt) if self._grace_value is not None: if ( @@ -246,41 +248,18 @@ def _split_into_chunks( chunk_start = chunk_end return chunks - async def _get_running_seconds( + def _get_running_seconds( self, entry_data: dict[str, Any], start_dt: datetime, end_dt: datetime ) -> float: - """Fetch the live in-progress entry so edits to its start time in Clockify are reflected.""" - if not entry_data.get("is_running"): - return 0.0 + """Return elapsed time of the running timer from its known start time. - workspace_id = entry_data.get("workspace_id") - user_id = entry_data.get("user_id") - api_key = entry_data.get("api_key") - if not workspace_id or not user_id or not api_key: - return 0.0 - - session = async_get_clientsession(self.hass) - url = get_in_progress_time_entries_url(workspace_id, user_id) - headers = {"X-Api-Key": api_key} - - try: - async with session.get(url, headers=headers, timeout=10) as response: - if response.status != 200: - return 0.0 - entries = await response.json() - except Exception as err: - _LOGGER.debug("Failed to fetch running Clockify timer: %s", err) - return 0.0 - - if not isinstance(entries, list) or not entries: - return 0.0 - - active = entries[0] - project_filter = entry_data.get("project_id_config") or None - if project_filter and active.get("projectId") != project_filter: + The start time comes from webhooks, the switch or the boot sync, so no API + call is needed here (the Free plan allows only 30 regular API requests/hour). + """ + if not entry_data.get("is_running"): return 0.0 - start_time = dt_util.parse_datetime(active.get("timeInterval", {}).get("start") or "") + start_time = dt_util.parse_datetime(entry_data.get("start_time") or "") if start_time is None: return 0.0