Use SENSOR_DEVICE_CLASS_* constants more (#9982)

This commit is contained in:
Ville Skyttä 2021-09-07 10:21:51 +03:00 committed by GitHub
parent b3dedae115
commit 6ba6b821f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 3 deletions

View File

@ -2,6 +2,7 @@
import { HassEntity } from "home-assistant-js-websocket"; import { HassEntity } from "home-assistant-js-websocket";
import { FIXED_DEVICE_CLASS_ICONS, UNIT_C, UNIT_F } from "../const"; import { FIXED_DEVICE_CLASS_ICONS, UNIT_C, UNIT_F } from "../const";
import { batteryIcon } from "./battery_icon"; import { batteryIcon } from "./battery_icon";
import { SENSOR_DEVICE_CLASS_BATTERY } from "../../data/sensor";
export const sensorIcon = (stateObj?: HassEntity): string | undefined => { export const sensorIcon = (stateObj?: HassEntity): string | undefined => {
const dclass = stateObj?.attributes.device_class; const dclass = stateObj?.attributes.device_class;
@ -10,7 +11,7 @@ export const sensorIcon = (stateObj?: HassEntity): string | undefined => {
return FIXED_DEVICE_CLASS_ICONS[dclass]; return FIXED_DEVICE_CLASS_ICONS[dclass];
} }
if (dclass === "battery") { if (dclass === SENSOR_DEVICE_CLASS_BATTERY) {
return stateObj ? batteryIcon(stateObj) : "hass:battery"; return stateObj ? batteryIcon(stateObj) : "hass:battery";
} }

View File

@ -23,6 +23,7 @@ import {
CallServiceActionConfig, CallServiceActionConfig,
MoreInfoActionConfig, MoreInfoActionConfig,
} from "../../../data/lovelace"; } from "../../../data/lovelace";
import { SENSOR_DEVICE_CLASS_TIMESTAMP } from "../../../data/sensor";
import { HomeAssistant } from "../../../types"; import { HomeAssistant } from "../../../types";
import { actionHandler } from "../common/directives/action-handler-directive"; import { actionHandler } from "../common/directives/action-handler-directive";
import { findEntities } from "../common/find-entities"; import { findEntities } from "../common/find-entities";
@ -316,7 +317,8 @@ export class HuiGlanceCard extends LitElement implements LovelaceCard {
? html` ? html`
<div> <div>
${computeDomain(entityConf.entity) === "sensor" && ${computeDomain(entityConf.entity) === "sensor" &&
stateObj.attributes.device_class === "timestamp" && stateObj.attributes.device_class ===
SENSOR_DEVICE_CLASS_TIMESTAMP &&
!UNAVAILABLE_STATES.includes(stateObj.state) !UNAVAILABLE_STATES.includes(stateObj.state)
? html` ? html`
<hui-timestamp-display <hui-timestamp-display

View File

@ -8,6 +8,7 @@ import { computeStateDisplay } from "../common/entity/compute_state_display";
import { computeRTL } from "../common/util/compute_rtl"; import { computeRTL } from "../common/util/compute_rtl";
import "../components/entity/state-info"; import "../components/entity/state-info";
import { UNAVAILABLE_STATES } from "../data/entity"; import { UNAVAILABLE_STATES } from "../data/entity";
import { SENSOR_DEVICE_CLASS_TIMESTAMP } from "../data/sensor";
import "../panels/lovelace/components/hui-timestamp-display"; import "../panels/lovelace/components/hui-timestamp-display";
import { haStyle } from "../resources/styles"; import { haStyle } from "../resources/styles";
import type { HomeAssistant } from "../types"; import type { HomeAssistant } from "../types";
@ -39,7 +40,8 @@ export class StateCardDisplay extends LitElement {
})}" })}"
> >
${computeDomain(this.stateObj.entity_id) === "sensor" && ${computeDomain(this.stateObj.entity_id) === "sensor" &&
this.stateObj.attributes.device_class === "timestamp" && this.stateObj.attributes.device_class ===
SENSOR_DEVICE_CLASS_TIMESTAMP &&
!UNAVAILABLE_STATES.includes(this.stateObj.state) !UNAVAILABLE_STATES.includes(this.stateObj.state)
? html` <hui-timestamp-display ? html` <hui-timestamp-display
.hass=${this.hass} .hass=${this.hass}