Skip to content
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

Use now HomeAssistant Format / Unit Entity parameter for Uv Index #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/uv-index-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ 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';
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} `,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -151,7 +170,7 @@ export class UVIndexCard extends LitElement {
<div>
<p>
<span style="font-weight: bold;">${localize('common.uv_index', '', '', this.config.language)}</span><br/>
${stateValue}
${stateValueStr}
</p>
</div>
<div>
Expand Down