Skip to content

Conversation

mdegat01
Copy link
Contributor

@mdegat01 mdegat01 commented Nov 9, 2022

Proposed change

If is_state is passed a list of values it returns true if the state of the entity is in the list. Essentially this:

{{ states('binary_sensor.test') is in ['on', 'off'] }}

would become this after this PR:

{{ is_state('binary_sensor.test', ['on', 'off']) }}

Also this really helps with expand use cases as this:

{{ expand(my_list_of_entities) | selectattr('state', 'in', ['on', 'off'])
    | map(attribute='entity_id') | list }}

becomes this:

{{ my_list_of_entities | select('is_state', ['on', 'off']) | list }}

This is not a breaking change because a state cannot be a list. That's also why is_state_attr is not modified in the same way because an attribute can be a list.

Came up because I was auditing my templates after #79473 and realized now expand is really only needed in two cases:

  1. When using last_updated, last_changed or context
  2. When you want to select or reject based on the state or an attribute and the comparator is not equals (in, gt, lt, etc.)

This small change further whittled down the need for complex templates with expand as it means in comparisons based on state can be done without it.

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
  • The code has been formatted using Black (black --fast 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.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@mdegat01 mdegat01 requested a review from a team as a code owner November 9, 2022 16:52
@home-assistant home-assistant bot added cla-signed core new-feature small-pr PRs with less than 30 lines. labels Nov 9, 2022
@frenck frenck self-requested a review November 16, 2022 12:18
@frenck frenck self-assigned this Nov 21, 2022
@frenck
Copy link
Member

frenck commented Nov 23, 2022

Since @mdegat01 is not available soon, I've taken the freedom to add a small change. Could use a second pair of eyes on it now (as I've changed it).

Also, opened up a documentation PR: home-assistant/home-assistant.io#25031

@frenck frenck removed their assignment Nov 23, 2022
@frenck frenck added smash Indicator this PR is close to finish for merging or closing second-opinion-wanted Add this label when a reviewer needs a second opinion from another member. labels Nov 23, 2022
@emontnemery emontnemery merged commit b3deb47 into dev Nov 30, 2022
@emontnemery emontnemery deleted the is-state-in-list branch November 30, 2022 21:54
@github-actions github-actions bot locked and limited conversation to collaborators Dec 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla-signed core new-feature second-opinion-wanted Add this label when a reviewer needs a second opinion from another member. small-pr PRs with less than 30 lines. smash Indicator this PR is close to finish for merging or closing templates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants