From 5b4c308a45b60a5c089f32043cd153bcc1aa5152 Mon Sep 17 00:00:00 2001 From: bernardpe Date: Wed, 4 Sep 2024 11:49:00 +0000 Subject: [PATCH] Use now HomeAssistant Format / Unit Entity parameter for Uv Index --- src/uv-index-card.ts | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/uv-index-card.ts b/src/uv-index-card.ts index 03715f4..6ea23f1 100644 --- a/src/uv-index-card.ts +++ b/src/uv-index-card.ts @@ -11,6 +11,10 @@ import { getLovelace, } from 'custom-card-helpers'; // This is a community maintained npm module with common helper functions/types. https://github.com/custom-cards/custom-card-helpers +import { + HassEntity +} from 'home-assistant-js-websocket'; // This is a community maintained npm module with common helper functions/types. https://github.com/custom-cards/custom-card-helpers + import type { UVIndexCardConfig } from './types'; import { actionHandler } from './action-handler-directive'; import { CARD_VERSION } from './const'; @@ -18,6 +22,10 @@ import { localize } from './localize/localize'; import './editor'; +export interface UvIndexCardHomeAssistant extends HomeAssistant { + formatEntityState: (stateObj: HassEntity, state?: string) => string; /* Since Home Assistant Core 2023.9 release */ +} + /* eslint no-console: 0 */ console.info( `%c UV-INDEX-CARD \n%c ${localize('common.version')} ${CARD_VERSION} `, @@ -48,7 +56,7 @@ export class UVIndexCard extends LitElement { // TODO Add any properities that should cause your element to re-render here // https://lit.dev/docs/components/properties/ - @property({ attribute: false }) public hass!: HomeAssistant; + @property({ attribute: false }) public hass!: UvIndexCardHomeAssistant; @state() private config!: UVIndexCardConfig; @@ -92,6 +100,17 @@ export class UVIndexCard extends LitElement { const entityId = this.config.entity; const entityState = entityId ? this.hass.states[entityId] : undefined; const stateValue:number = entityState ? parseFloat(entityState.state) : 0; + let stateValueStr: string | undefined = undefined; + + try { + if (entityState != undefined) { + stateValueStr = this.hass.formatEntityState(entityState) + } + } catch { } + + if (stateValueStr == undefined) { + stateValueStr = stateValue.toString() + } let uvRiskStr = 'uv_levels.low' if (stateValue >= 3 && stateValue < 6) { @@ -151,7 +170,7 @@ export class UVIndexCard extends LitElement {

${localize('common.uv_index', '', '', this.config.language)}
- ${stateValue} + ${stateValueStr}