core icon indicating copy to clipboard operation
core copied to clipboard

Add has_value function/test to Jinja2 template

Open ehendrix23 opened this issue 1 year ago • 18 comments

Proposed change

Add a function and test to determine if an entity is available ('unknown' or 'unavailable).

Checking if an entity is available is something done regularly within template sensors.

Currently this is done through: states('<entity>') not in (('unknown', 'unavailable')) And often when multiple entities are to be checked combined with AND (if at least one should be available) or OR (if none can be unavailable). {{ states('<entity1>') not in (('unknown', 'unavailable')) or states('<entity2>') not in (('unknown', 'unavailable')) or states('<entity3>') not in (('unknown', 'unavailable')) }}

With this change this can be simplified to: {{ has_value('<entity1>') or has_value('<entity2>') or has_value('<entity3>')

Type of change

  • [ ] Dependency upgrade
  • [ ] Bugfix (non-breaking change which fixes an issue)
  • [ ] New integration (thank you!)
  • [X] 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

  • This PR fixes or closes issue: fixes https://community.home-assistant.io/t/wth-we-dont-have-a-is-available-filter/468986
  • This PR is related to issue:
  • Link to documentation pull request: https://github.com/home-assistant/home-assistant.io/pull/24360

Checklist

  • [X] The code change is tested and works locally.
  • [X] Local tests pass. Your PR cannot be merged unless tests pass
  • [X] There is no commented out code in this PR.
  • [X] I have followed the development checklist
  • [X] The code has been formatted using Black (black --fast homeassistant tests)
  • [X] 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.
  • [ ] Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • [ ] No score or internal
  • [ ] 🥈 Silver
  • [ ] 🥇 Gold
  • [ ] 🏆 Platinum

To help with the load of incoming pull requests:

ehendrix23 avatar Oct 03 '22 18:10 ehendrix23