Allow use of home-assistant icons in template card secondary. #1397
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR allows mushroom card's to use home-assistant icons in their secondary state area by defining these as
:mdi:home-assistant:
within the templates.This allows template cards for example to be setup to show an icons for any device that is currently turned on, e.g.
This can also be used to allow for inline icons, for example next to displayed temperature values such as in the link ticket.
This has been implemented via adding a
parseIcons
function to thestate-info.ts
, which uses a rejex to swap out bracketed values, e.g.:mdi:icon-name:
for<ha-icon icon="mdi:icon-name"></ha-icon>
. I've wrapped this in a fragment in order to render the added HTML as part of the TemplateResult. I've additionally tweaked the css to ensure the icons are an appropriate size for the context.I've never worked with TypeScript or Lit components before, so apologies if I've set them up wrong/butchered them a bit. Please let me know if there are cleaner ways of approaching this.
I'd be appreciative of any guidance as to whether this change would warrant a documentation update, assuming this functionality is something that is seen as desirable to have added.
Re: code style - I ran the prettier command to format my change. I wasn't able to easily locate any additional document on the code style choices, beyond attempting to be similar to what is already there.
Note:
{mdi:sofa}
as its nice and simple, although it'd be easy to swap the regex to look for{ icon:'mdi:sofa' }
instead.Related Issue
This PR fixes or closes issue: fixes #327
Motivation and Context
I currently use template cards for each room of my house, which then contain logic to show a Unicode icon for each device that's is turned on in the given room within the cards secondary area. Unicode is far more limited in the range of icons i can use, so I had a go at this in order to allow for much better identification of specific devices - ie. is it a lamp or main light that's on.
Other use cases include in-line use of icons next to figures displayed (power usage by a device, a temperature reading etc)
How Has This Been Tested
I've primarily tested this on my own home assistant instances by swapping out the build JS imported by hacs.
I've tested a variety of icons, although as far as I'm aware only the template card allows me to directly control what text (and thus icons) are output in the secondary area.
I could make the behaviour available to
content
on the chip templates or names, if that was a desirable extension to this.Tested in
Types of changes
Checklist