Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Each configured project gets a device named `Clockify - <project>` 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

Expand Down
2 changes: 1 addition & 1 deletion custom_components/clockify/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
13 changes: 11 additions & 2 deletions custom_components/clockify/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down
Loading