diff --git a/src/common/entity/state_card_type.ts b/src/common/entity/state_card_type.ts index a24994d5b3..2013ee2058 100644 --- a/src/common/entity/state_card_type.ts +++ b/src/common/entity/state_card_type.ts @@ -3,9 +3,10 @@ import { HomeAssistant } from "../../types"; import { DOMAINS_WITH_CARD } from "../const"; import { canToggleState } from "./can_toggle_state"; import { computeStateDomain } from "./compute_state_domain"; +import { UNAVAILABLE } from "../../data/entity"; export const stateCardType = (hass: HomeAssistant, stateObj: HassEntity) => { - if (stateObj.state === "unavailable") { + if (stateObj.state === UNAVAILABLE) { return "display"; } diff --git a/src/components/entity/ha-state-label-badge.ts b/src/components/entity/ha-state-label-badge.ts index b38009042f..3441979436 100644 --- a/src/components/entity/ha-state-label-badge.ts +++ b/src/components/entity/ha-state-label-badge.ts @@ -20,6 +20,7 @@ import { stateIcon } from "../../common/entity/state_icon"; import { timerTimeRemaining } from "../../common/entity/timer_time_remaining"; import { HomeAssistant } from "../../types"; import "../ha-label-badge"; +import { UNAVAILABLE, UNKNOWN } from "../../data/entity"; @customElement("ha-state-label-badge") export class HaStateLabelBadge extends LitElement { @@ -81,7 +82,8 @@ export class HaStateLabelBadge extends LitElement { ? "" : this.image ? this.image - : state.attributes.entity_picture_local || state.attributes.entity_picture}" + : state.attributes.entity_picture_local || + state.attributes.entity_picture}" .label="${this._computeLabel(domain, state, this._timerTimeRemaining)}" .description="${this.name ? this.name : computeStateName(state)}" > @@ -108,7 +110,7 @@ export class HaStateLabelBadge extends LitElement { return null; case "sensor": default: - return state.state === "unknown" + return state.state === UNKNOWN ? "-" : state.attributes.unit_of_measurement ? state.state @@ -121,7 +123,7 @@ export class HaStateLabelBadge extends LitElement { } private _computeIcon(domain: string, state: HassEntity) { - if (state.state === "unavailable") { + if (state.state === UNAVAILABLE) { return null; } switch (domain) { @@ -166,7 +168,7 @@ export class HaStateLabelBadge extends LitElement { private _computeLabel(domain, state, _timerTimeRemaining) { if ( - state.state === "unavailable" || + state.state === UNAVAILABLE || ["device_tracker", "alarm_control_panel", "person"].includes(domain) ) { // Localize the state with a special state_badge namespace, which has variations of diff --git a/src/dialogs/more-info/controls/more-info-automation.ts b/src/dialogs/more-info/controls/more-info-automation.ts index 8b4ff441c2..6a1a789b99 100644 --- a/src/dialogs/more-info/controls/more-info-automation.ts +++ b/src/dialogs/more-info/controls/more-info-automation.ts @@ -12,12 +12,13 @@ import { import "../../../components/ha-relative-time"; import { triggerAutomation } from "../../../data/automation"; import { HomeAssistant } from "../../../types"; +import { UNAVAILABLE_STATES } from "../../../data/entity"; @customElement("more-info-automation") class MoreInfoAutomation extends LitElement { @property({ attribute: false }) public hass!: HomeAssistant; - @property() public stateObj?: HassEntity; + @property({ attribute: false }) public stateObj?: HassEntity; protected render(): TemplateResult { if (!this.hass || !this.stateObj) { @@ -36,7 +37,7 @@ class MoreInfoAutomation extends LitElement {
${this.hass.localize("ui.card.automation.trigger")} diff --git a/src/panels/config/automation/ha-automation-picker.ts b/src/panels/config/automation/ha-automation-picker.ts index 221f7f1e14..d95e80f079 100644 --- a/src/panels/config/automation/ha-automation-picker.ts +++ b/src/panels/config/automation/ha-automation-picker.ts @@ -25,6 +25,7 @@ import { showAutomationEditor, triggerAutomation, } from "../../../data/automation"; +import { UNAVAILABLE_STATES } from "../../../data/entity"; import "../../../layouts/hass-tabs-subpage-data-table"; import { haStyle } from "../../../resources/styles"; import { HomeAssistant, Route } from "../../../types"; @@ -35,9 +36,9 @@ import { showThingtalkDialog } from "./show-dialog-thingtalk"; class HaAutomationPicker extends LitElement { @property({ attribute: false }) public hass!: HomeAssistant; - @property() public isWide!: boolean; + @property({ type: Boolean }) public isWide!: boolean; - @property() public narrow!: boolean; + @property({ type: Boolean }) public narrow!: boolean; @property() public route!: Route; @@ -58,7 +59,7 @@ class HaAutomationPicker extends LitElement { toggle: { title: "", type: "icon", - template: (_toggle, automation) => + template: (_toggle, automation: any) => html` this._execute(ev)} - .disabled=${automation.state === "unavailable"} + .disabled=${UNAVAILABLE_STATES.includes(automation.state)} > ${this.hass.localize("ui.card.automation.trigger")} diff --git a/src/panels/config/entities/ha-config-entities.ts b/src/panels/config/entities/ha-config-entities.ts index 8ac21ad076..366c852fa9 100644 --- a/src/panels/config/entities/ha-config-entities.ts +++ b/src/panels/config/entities/ha-config-entities.ts @@ -59,6 +59,7 @@ import { showEntityEditorDialog, } from "./show-dialog-entity-editor"; import { haStyle } from "../../../resources/styles"; +import { UNAVAILABLE } from "../../../data/entity"; export interface StateEntity extends EntityRegistryEntry { readonly?: boolean; @@ -281,7 +282,7 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) { for (const entry of entities) { const entity = this.hass.states[entry.entity_id]; - const unavailable = entity?.state === "unavailable"; + const unavailable = entity?.state === UNAVAILABLE; const restored = entity?.attributes.restored; if (!showUnavailable && unavailable) { diff --git a/src/panels/lovelace/cards/hui-light-card.ts b/src/panels/lovelace/cards/hui-light-card.ts index bb65f89e1c..f05a1c4c1c 100644 --- a/src/panels/lovelace/cards/hui-light-card.ts +++ b/src/panels/lovelace/cards/hui-light-card.ts @@ -20,7 +20,7 @@ import { computeStateName } from "../../../common/entity/compute_state_name"; import { stateIcon } from "../../../common/entity/state_icon"; import { supportsFeature } from "../../../common/entity/supports-feature"; import "../../../components/ha-card"; -import { UNAVAILABLE_STATES } from "../../../data/entity"; +import { UNAVAILABLE_STATES, UNAVAILABLE } from "../../../data/entity"; import { SUPPORT_BRIGHTNESS } from "../../../data/light"; import { ActionHandlerEvent } from "../../../data/lovelace"; import { HomeAssistant, LightEntity } from "../../../types"; @@ -133,7 +133,7 @@ export class HuiLightCard extends LitElement implements LovelaceCard { SUPPORT_BRIGHTNESS ), "state-on": stateObj.state === "on", - "state-unavailable": stateObj.state === "unavailable", + "state-unavailable": stateObj.state === UNAVAILABLE, })}" .icon=${this._config.icon || stateIcon(stateObj)} .disabled=${UNAVAILABLE_STATES.includes(stateObj.state)} diff --git a/src/panels/lovelace/common/validate-condition.ts b/src/panels/lovelace/common/validate-condition.ts index e50707d871..0aca5f2e11 100644 --- a/src/panels/lovelace/common/validate-condition.ts +++ b/src/panels/lovelace/common/validate-condition.ts @@ -1,4 +1,5 @@ import { HomeAssistant } from "../../../types"; +import { UNAVAILABLE } from "../../../data/entity"; export interface Condition { entity: string; @@ -13,7 +14,7 @@ export function checkConditionsMet( return conditions.every((c) => { const state = hass.states[c.entity] ? hass!.states[c.entity].state - : "unavailable"; + : UNAVAILABLE; return c.state ? state === c.state : state !== c.state_not; }); diff --git a/src/panels/lovelace/components/hui-image.ts b/src/panels/lovelace/components/hui-image.ts index 115e118f83..004bf705ff 100644 --- a/src/panels/lovelace/components/hui-image.ts +++ b/src/panels/lovelace/components/hui-image.ts @@ -17,6 +17,7 @@ import parseAspectRatio from "../../../common/util/parse-aspect-ratio"; import "../../../components/ha-camera-stream"; import { fetchThumbnailUrlWithCache } from "../../../data/camera"; import { CameraEntity, HomeAssistant } from "../../../types"; +import { UNAVAILABLE } from "../../../data/entity"; const UPDATE_INTERVAL = 10000; const DEFAULT_FILTER = "grayscale(100%)"; @@ -73,7 +74,7 @@ export class HuiImage extends LitElement { } const ratio = this.aspectRatio ? parseAspectRatio(this.aspectRatio) : null; const stateObj = this.entity ? this.hass.states[this.entity] : undefined; - const state = stateObj ? stateObj.state : "unavailable"; + const state = stateObj ? stateObj.state : UNAVAILABLE; // Figure out image source to use let imageSrc: string | undefined; diff --git a/src/panels/lovelace/entity-rows/hui-input-datetime-entity-row.ts b/src/panels/lovelace/entity-rows/hui-input-datetime-entity-row.ts index f878c33985..46e5a1e88c 100644 --- a/src/panels/lovelace/entity-rows/hui-input-datetime-entity-row.ts +++ b/src/panels/lovelace/entity-rows/hui-input-datetime-entity-row.ts @@ -11,7 +11,7 @@ import "../../../components/ha-date-input"; import type { HaDateInput } from "../../../components/ha-date-input"; import "../../../components/paper-time-input"; import type { PaperTimeInput } from "../../../components/paper-time-input"; -import { UNAVAILABLE_STATES } from "../../../data/entity"; +import { UNAVAILABLE_STATES, UNKNOWN } from "../../../data/entity"; import { setInputDateTimeValue } from "../../../data/input_datetime"; import type { HomeAssistant } from "../../../types"; import { hasConfigOrEntityChanged } from "../common/has-changed"; @@ -70,10 +70,10 @@ class HuiInputDatetimeEntityRow extends LitElement implements LovelaceRow { ? html` ${stateObj.attributes.device_class === SENSOR_DEVICE_CLASS_TIMESTAMP && - stateObj.state !== "unavailable" && - stateObj.state !== "unknown" + !UNAVAILABLE_STATES.includes(stateObj.state) ? html` { // Mock Localize function for testing @@ -72,7 +73,7 @@ describe("computeStateDisplay", () => { }; const stateObj: any = { entity_id: "sensor.test", - state: "unknown", + state: UNKNOWN, attributes: { unit_of_measurement: "m", },