Add human-readable daysToText attribute to sensors (#6534)#6733
Add human-readable daysToText attribute to sensors (#6534)#6733Coderzz69 wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a small formatting helper to the Home Assistant sensor integration so users can access a human-readable “days until collection” string via extra_state_attributes, and introduces unit tests for that mapping.
Changes:
- Added
get_days_to_text(days: int) -> strto mapdaysToto"Today","Tomorrow", or"in X days". - Exposed the formatted string as a new
daysToTextextra state attribute alongside the existingdaysTo. - Added a new
tests/test_sensor.pymodule to validate the mapping logic for0,1, and>1days.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
custom_components/waste_collection_schedule/sensor.py |
Adds get_days_to_text() and exposes daysToText in sensor extra state attributes. |
tests/test_sensor.py |
Adds unit tests for get_days_to_text() edge cases. |
| @@ -0,0 +1,22 @@ | |||
| """Tests for sensor logic.""" | |||
|
Good idea, but what about translations? |
There was a problem hiding this comment.
Thanks for this, and for adding tests — the ergonomics idea behind daysToText is reasonable. Before this can go in, though, there's a design question that needs resolving:
Translations (blocking). The helper hardcodes English "Today" / "Tomorrow" / "in {days} days" into extra_state_attributes. This integration is used predominantly in non-English locales, and Home Assistant doesn't translate arbitrary attribute values, so a fixed-English attribute would ship untranslated to everyone. Could you outline how you'd like to handle localisation here? Options include exposing the string only through a proper i18n path, or keeping the numeric daysTo (which users already template today) and dropping the baked-in English string.
Two smaller things while you're in there:
tests/test_sensor.pywon't actually run in CI —pytest.inirestricts discovery topython_files = test_source_components.py, so these assertions are collected only when the file is invoked by name. Worth confirming how you'd like them wired into the collected suite (or folding them into an already-discovered module).- Please drop the unused
__author__variable to keep the test module minimal (per the Copilot note).
Happy to re-review once the translation approach is settled.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Summary
Fixes #6534
This PR adds a human-readable
daysToTextstring to theextra_state_attributesof the sensor (e.g. "Today", "Tomorrow", "in X days").Currently, the default state can be overwritten by users with a
value_templateto isolate the integer for conditions, but this strips out the human-readable formatting, making it difficult to use in dashboard elements like Badges that do not natively support templating. By surfacingdaysToTextdirectly in the attributes alongside the existing numericdaysTo, users can easily map the raw integer to automations and the formatted string to UI Badges without needing to create secondary template sensors.Additionally, this PR introduces a standalone test suite in
tests/test_sensor.pyto ensure the core logic handles the integer mappings correctly across edge cases (0,1,> 1days).Type of change
Checklist
python -m pytest tests/test_source_components.py -qpassesruff check --fixandruff formatrun on changed source filesdoc/source/<name>.mdcreated for new sources (N/A)