diff --git a/CHANGELOG.md b/CHANGELOG.md index 1160ecf..c0a657e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ All notable changes to this project are documented here. The format follows [Kee ### Changed +- Every sensor, binary sensor and event entity now has a fitting icon instead of Home Assistant's default eye. The CI state, the last workflow result and the "workflow running" sensor change their icon with their state, and custom query sensors use a code icon. - Repositories are now requested in batches of 10 instead of 50, so the first refresh of a long repository list no longer risks running into Home Assistant's startup limits. - When GitHub fails for one batch of repositories (for example with a 502), the other batches are still updated and the affected repositories keep their last values, instead of every repository sensor turning unavailable. diff --git a/README.md b/README.md index 2fe0e04..75b7d5c 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,8 @@ To replace the token, open the integration, choose **Reconfigure** and enter the | 🧪 | Custom queries | Your own read-only GraphQL query as a sensor, added under **Add custom query**. Rendered with templates over `value_json`. Presets included; see the [guide](docs/custom-queries.md) | | ➕ | Optional sensors | Reviews, issues, active days, strongest weekday, watchers, release downloads, days since last commit or release, pull requests awaiting your review, most active hour and night-owl score (approximate, from GitHub's last 300 events), average time to merge (last 50 merged pull requests), stale pull requests (open, untouched for 14 days), year in review (one-line summary with the details as attributes), followers and following counts, repository traffic (unique visitors and cloners and page views and clones over the last 14 days, peak visitor day, top referrer and page, visitor trend, cloners per visitor; needs write access, see [Access token](#-access-token)) | +Every entity has its own icon. The CI state, the last workflow result and the "workflow running" binary sensor change their icon with their state, and custom query sensors use a code icon. You can still override any icon per entity in Home Assistant. + Everything marked optional is created disabled. GitHub Pulse only fetches data that an enabled entity needs, so unused sensors cost no API calls. > [!NOTE] diff --git a/custom_components/github_pulse/icons.json b/custom_components/github_pulse/icons.json new file mode 100644 index 0000000..6ba98ac --- /dev/null +++ b/custom_components/github_pulse/icons.json @@ -0,0 +1,91 @@ +{ + "entity": { + "sensor": { + "followers": {"default": "mdi:account-multiple"}, + "following": {"default": "mdi:account-arrow-right"}, + "contributions_this_month": {"default": "mdi:source-commit"}, + "commits_this_month": {"default": "mdi:source-commit"}, + "pull_requests_this_month": {"default": "mdi:source-pull"}, + "commits_vs_last_month": {"default": "mdi:chart-line-variant"}, + "commits_vs_last_year": {"default": "mdi:chart-timeline-variant"}, + "busiest_month": {"default": "mdi:calendar-star"}, + "current_streak": {"default": "mdi:fire"}, + "longest_streak": {"default": "mdi:trophy-outline"}, + "reviews_this_month": {"default": "mdi:eye-check-outline"}, + "issues_this_month": {"default": "mdi:alert-circle-outline"}, + "active_days_this_month": {"default": "mdi:calendar-check"}, + "most_productive_weekday": {"default": "mdi:calendar-week"}, + "most_active_hour": {"default": "mdi:clock-outline"}, + "night_owl_score": {"default": "mdi:owl"}, + "average_time_to_merge": {"default": "mdi:source-merge"}, + "stale_pull_requests": {"default": "mdi:timer-sand"}, + "year_in_review": {"default": "mdi:calendar-text"}, + "top_language": {"default": "mdi:code-tags"}, + "language_shift": {"default": "mdi:swap-horizontal"}, + "review_requests": {"default": "mdi:account-eye-outline"}, + "authored_open_pull_requests": {"default": "mdi:source-branch"}, + "stars": {"default": "mdi:star-outline"}, + "forks": {"default": "mdi:source-fork"}, + "open_issues": {"default": "mdi:alert-circle-outline"}, + "open_pull_requests": {"default": "mdi:source-pull"}, + "latest_release": {"default": "mdi:tag-outline"}, + "watchers": {"default": "mdi:bell-outline"}, + "latest_release_downloads": {"default": "mdi:download-circle-outline"}, + "total_downloads": {"default": "mdi:download-multiple"}, + "days_since_last_commit": {"default": "mdi:source-commit"}, + "days_since_last_release": {"default": "mdi:tag-text-outline"}, + "last_push": {"default": "mdi:cloud-upload-outline"}, + "ci_state": { + "default": "mdi:check-network-outline", + "state": { + "success": "mdi:check-circle-outline", + "failure": "mdi:close-circle-outline", + "pending": "mdi:progress-clock", + "error": "mdi:alert-circle-outline", + "expected": "mdi:clock-outline" + } + }, + "unique_visitors_14d": {"default": "mdi:account-group-outline"}, + "views_14d": {"default": "mdi:eye-outline"}, + "unique_cloners_14d": {"default": "mdi:account-arrow-down-outline"}, + "clones_14d": {"default": "mdi:content-copy"}, + "peak_visitor_day": {"default": "mdi:calendar-arrow-right"}, + "top_referrer": {"default": "mdi:link-variant"}, + "top_path": {"default": "mdi:file-document-outline"}, + "visitor_trend": {"default": "mdi:trending-up"}, + "clone_visitor_ratio": {"default": "mdi:percent-outline"}, + "actions_last_result": { + "default": "mdi:play-circle-outline", + "state": { + "success": "mdi:check-circle-outline", + "failure": "mdi:close-circle-outline", + "cancelled": "mdi:cancel", + "skipped": "mdi:skip-next-circle-outline", + "timed_out": "mdi:timer-off-outline", + "neutral": "mdi:minus-circle-outline", + "action_required": "mdi:alert-circle-outline", + "stale": "mdi:history", + "startup_failure": "mdi:alert-octagon-outline" + } + }, + "actions_runs_today": {"default": "mdi:play-box-multiple-outline"}, + "actions_queued_runs": {"default": "mdi:tray-full"}, + "actions_failure_rate_7d": {"default": "mdi:alert-decagram-outline"}, + "actions_failure_rate_30d": {"default": "mdi:alert-decagram-outline"}, + "actions_average_duration": {"default": "mdi:timer-outline"} + }, + "binary_sensor": { + "actions_running": { + "default": "mdi:cog-pause-outline", + "state": { + "on": "mdi:cog-sync-outline", + "off": "mdi:cog-pause-outline" + } + } + }, + "event": { + "repository_events": {"default": "mdi:bell-ring-outline"}, + "account_events": {"default": "mdi:source-pull"} + } + } +} diff --git a/custom_components/github_pulse/sensor.py b/custom_components/github_pulse/sensor.py index 54360c6..9330e94 100644 --- a/custom_components/github_pulse/sensor.py +++ b/custom_components/github_pulse/sensor.py @@ -225,6 +225,7 @@ def __init__(self, coordinator: QueryCoordinator) -> None: self._attr_device_info = None data = subentry.data self._attr_name = subentry.title + self._attr_icon = "mdi:code-braces" self._attr_native_unit_of_measurement = data.get(CONF_UNIT) or None if device_class := data.get(CONF_DEVICE_CLASS): self._attr_device_class = SensorDeviceClass(device_class) diff --git a/tests/test_icons.py b/tests/test_icons.py new file mode 100644 index 0000000..54729fa --- /dev/null +++ b/tests/test_icons.py @@ -0,0 +1,75 @@ +"""Every entity the integration describes carries an icon.""" + +from __future__ import annotations + +import json +from pathlib import Path +from typing import Any + +import pytest + +PACKAGE = Path(__file__).parents[1] / "custom_components/github_pulse" +STATE_ICON_KEYS = { + ("sensor", "ci_state"), + ("sensor", "actions_last_result"), + ("binary_sensor", "actions_running"), +} + + +def _load(name: str) -> dict[str, Any]: + return json.loads((PACKAGE / name).read_text())["entity"] + + +@pytest.fixture(scope="module") +def icons() -> dict[str, Any]: + return _load("icons.json") + + +@pytest.fixture(scope="module") +def entities() -> dict[str, Any]: + return _load("translations/en.json") + + +def _icon_strings(definition: dict[str, Any]) -> list[str]: + return [definition["default"], *definition.get("state", {}).values()] + + +def test_every_translated_entity_has_an_icon( + icons: dict[str, Any], entities: dict[str, Any] +) -> None: + for platform, keys in entities.items(): + assert set(keys) <= set(icons.get(platform, {})), platform + + +def test_icons_describe_no_unknown_entity( + icons: dict[str, Any], entities: dict[str, Any] +) -> None: + for platform, keys in icons.items(): + assert set(keys) <= set(entities.get(platform, {})), platform + + +def test_every_icon_is_a_material_design_icon(icons: dict[str, Any]) -> None: + for keys in icons.values(): + for definition in keys.values(): + assert all(icon.startswith("mdi:") for icon in _icon_strings(definition)) + + +def test_no_entity_falls_back_to_the_default_eye(icons: dict[str, Any]) -> None: + eyes = { + key + for keys in icons.values() + for key, definition in keys.items() + if "mdi:eye" in _icon_strings(definition) + } + assert eyes == set() + + +def test_state_icons_cover_every_option( + icons: dict[str, Any], entities: dict[str, Any] +) -> None: + for platform, key in STATE_ICON_KEYS: + states = icons[platform][key]["state"] + if platform == "binary_sensor": + assert set(states) == {"on", "off"} + else: + assert set(states) == set(entities[platform][key]["state"]) diff --git a/tests/test_query_sensor.py b/tests/test_query_sensor.py index 6e570e1..27efcbd 100644 --- a/tests/test_query_sensor.py +++ b/tests/test_query_sensor.py @@ -102,6 +102,7 @@ async def test_the_sensor_shows_the_rendered_result( assert state.attributes["login"] == "octocat" assert state.attributes["unit_of_measurement"] == "repos" assert state.attributes["state_class"] == "measurement" + assert state.attributes["icon"] == "mdi:code-braces" async def test_device_class_is_applied(