-
-
Notifications
You must be signed in to change notification settings - Fork 372
Allow use of home-assistant icons in template card secondary. #1397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…room cards. Converts {mdi:icon-name} to <ha-icon icon="mdi:icon-name"></ha-icon>, before rendering.
Hello 👋 Also, for the icon format with brackets, I'm not sure because bracket already have a role in jinja template. Github is using |
- Icon format changed to :mdi🔤 - Added logic to skip creating fragment if no icons found in string - Icon size CSS left in state-info as it relates to display of that area. - Lint & retest on local
Hi @piitaya Thank you for the quick feedback! I've reworked the logic as you suggested so that
I've also retested on my local and everything still appears to work happily for me. Re: the icon format, I think the only thing that feels a bit off with |
- Make var naming more consistent with existing (calling it Text) - No need to handle `text` being a TemplateResult at this point, so skip the extra assignments.
If you added markdown support, we could use the |
Looking at my code change, i actually think a general html solution would be pretty trivial (my original change enables it as a side effect), so updating everything to the latest and wrapping everything in the In the older ticket I saw piitaya wasn't 100% sold on the idea of allowing markdown or html, given it'd no longer be possible to keep styling consistent for all the possible combinations. I guess on the flip side, most the people using html/markdown are likely the sort to have cardmod adding custom styling/design choices anyway so guess it'd be on them to make stuff look nice if they changed anything substantially. Be happy to have a go at adding the feature if @piitaya is open to the idea. |
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