Skip to content

Conversation

ZephireNZ
Copy link
Contributor

@ZephireNZ ZephireNZ commented Oct 30, 2021

Proposed change

This adds a slugify filter for templates. This is useful, for example, if you have a string and want to translate it to a entity_id.

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)
  • 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.

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:

@tdejneka
Copy link

Thank you! It's a long-standing Feature Request:
https://community.home-assistant.io/t/slugify-please/224879

@frenck frenck added smash Indicator this PR is close to finish for merging or closing and removed smash Indicator this PR is close to finish for merging or closing labels Nov 29, 2021
@frenck
Copy link
Member

frenck commented Nov 29, 2021

@ZephireNZ A wild merge conflict appeared out of nowhere! 🙈
Could you take a look?

@emontnemery
Copy link
Contributor

emontnemery commented Nov 29, 2021

Just out of curiosity, how is this intended to be used?
There's no guarantee an entity's entity id is identical to a slugified version of its name etc.

@ZephireNZ
Copy link
Contributor Author

ZephireNZ commented Nov 29, 2021

Fixed merge conflict, did it in the GitHub UI so here's hoping I've not messed up the formatting checks 😅

@ZephireNZ
Copy link
Contributor Author

Just out of curiosity, how is this intended to be used? There's no guarantee an entity's entity id is identical to a slugified version of its name etc.

My use case:

I want to create an automation that changes my lights to a scene I created previously, based on the TV show playing.

Now, I could create a buuuunch of case statements in that automation, each with their own service call for specific lights/scenes, but that's very tedious to say the least.

As far as I could tell, there's no simple way to find an entity based on its name - so my idea is to do the same slugification of the show name, and check if a scene entity existed with that id. If for some reason scene.friends is actually scene.friends2, I can easily fix that manually.

I am sure there are other use cases too, but that's my own.

@ZephireNZ
Copy link
Contributor Author

@frenck all good to merge now 😊

@balloob
Copy link
Member

balloob commented Nov 29, 2021

States have a name attribute:

{{ states.light.desk_lamp.name }}

@emontnemery
Copy link
Contributor

emontnemery commented Nov 30, 2021

@ZephireNZ The use case you describe has the problem I mentioned: There's no guarantee that an entity's entity_id is the slugified form of it's name:

  • If the entity is not in the entity registry, the entity_id is not guaranteed to be stable across restarts of Home Assistant
  • If the entity is in the entity registry, the entity_id can be changed by the user from the frontend

Edit: Your example use case should be solved by looping over the available scene states, finding the scene with the right name.

@frenck
Copy link
Member

frenck commented Nov 30, 2021

I agree that the use case is incorrect and probably not the best example for advertising/adding a feature.

@ZephireNZ
Copy link
Contributor Author

@tdejneka what was your use case?

@frenck frenck force-pushed the feature/slugify_template branch from d9c5408 to 76e4b7d Compare December 19, 2021 19:08
@frenck
Copy link
Member

frenck commented Dec 19, 2021

Rebased the PR to deal with a merge conflict.

Copy link
Member

@frenck frenck left a comment

Choose a reason for hiding this comment

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

Ok, so the use-case described is wrong. You should not use the slugify method to "guess" entity IDs (there are many other method for that as shown above).

Nevertheless, we have discussed this on Discord and there are some cases where this can be useful. E.g., when needing an action key based on a title in an actional notification; working with REST APIs.

The use cases for these are fairly limited, but this addition is fairly clean as well.

Therefore accepting this addition. Thanks @ZephireNZ 👍

@frenck frenck merged commit 26dc526 into home-assistant:dev Dec 21, 2021
@ZephireNZ
Copy link
Contributor Author

Thanks! I knew it was a useful addition, just couldn't come up with a good example use case 😅

@github-actions github-actions bot locked and limited conversation to collaborators Dec 22, 2021
@ZephireNZ ZephireNZ deleted the feature/slugify_template branch February 9, 2025 04:24
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants