Skip to content

Add range support to icon translations #145340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open

Add range support to icon translations #145340

wants to merge 1 commit into from

Conversation

frenck
Copy link
Member

@frenck frenck commented May 20, 2025

Proposed change

Add range support to icon translations, meaning we can now provide icons based on the numeric state through the icons translation files.

Implements the proposal from architectural discussion: home-assistant/architecture#1190

As an example, the battery sensor has been enriched with range definitions, for which I've adjusted and cleaned up the frontend.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@home-assistant
Copy link

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (sensor) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of sensor can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign sensor Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds support for numeric range-based icons in the icon translation schema and enriches the battery sensor with range definitions.

  • Introduces range_key_validator and ensure_range_is_sorted in script/hassfest/icons.py to validate numeric keys and enforce ascending order.
  • Extends the icon_schema_slug to accept an optional top‐level range mapping and applies sorting checks under state_attributes.
  • Provides a new battery icon definition with range entries in homeassistant/components/sensor/icons.json.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
script/hassfest/icons.py Added numeric‐key and sorted‐range validators; integrated range into the icon schema
homeassistant/components/sensor/icons.json Added battery sensor with a range of icons based on percentage
Comments suppressed due to low confidence (2)

script/hassfest/icons.py:61

  • Add unit tests for ensure_range_is_sorted to cover both valid (sorted) and invalid (unsorted or non‐numeric) range definitions.
def ensure_range_is_sorted(value: dict) -> dict:

homeassistant/components/sensor/icons.json:18

  • [nitpick] This adds a range mapping for icons; please update the developer documentation to explain how to define and use range‐based icons in integration translation files.
"battery": {

@frenck frenck force-pushed the frenck-2025-0232 branch from 6de4d86 to f71873d Compare May 20, 2025 17:57
@frenck frenck marked this pull request as ready for review May 20, 2025 18:24
@frenck frenck requested a review from a team as a code owner May 20, 2025 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant