Skip to content

Add human-readable daysToText attribute to sensors (#6534)#6733

Open
Coderzz69 wants to merge 3 commits into
mampfes:masterfrom
Coderzz69:feature/issue-6534-human-readable-days
Open

Add human-readable daysToText attribute to sensors (#6534)#6733
Coderzz69 wants to merge 3 commits into
mampfes:masterfrom
Coderzz69:feature/issue-6534-human-readable-days

Conversation

@Coderzz69

Copy link
Copy Markdown
Contributor

Summary

Fixes #6534

This PR adds a human-readable daysToText string to the extra_state_attributes of the sensor (e.g. "Today", "Tomorrow", "in X days").

Currently, the default state can be overwritten by users with a value_template to 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 surfacing daysToText directly in the attributes alongside the existing numeric daysTo, 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.py to ensure the core logic handles the integer mappings correctly across edge cases (0, 1, > 1 days).

Type of change

  • New source
  • Bug fix / source fix
  • Documentation update
  • Other (Feature enhancement & tests)

Checklist

  • python -m pytest tests/test_source_components.py -q passes
  • ruff check --fix and ruff format run on changed source files
  • No generated files in diff (README.md, info.md, sources.json, translations/*.json — CI regenerates these post-merge)
  • doc/source/<name>.md created for new sources (N/A)
  • TEST_CASES use real, publicly accessible addresses (not my own) (N/A)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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) -> str to map daysTo to "Today", "Tomorrow", or "in X days".
  • Exposed the formatted string as a new daysToText extra state attribute alongside the existing daysTo.
  • Added a new tests/test_sensor.py module to validate the mapping logic for 0, 1, and >1 days.

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.

Comment thread tests/test_sensor.py
@@ -0,0 +1,22 @@
"""Tests for sensor logic."""
Comment thread tests/test_sensor.py
@bbr111

bbr111 commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

Good idea, but what about translations?

@bbr111 bbr111 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.py won't actually run in CI — pytest.ini restricts discovery to python_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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Add a human-readable attribute for displaying time until collection

3 participants