mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-26 18:56:39 +00:00
Use proper constants for "unavailable" checks (#6922)
* Use proper constants for "unavailable" * Additional usage of constants
This commit is contained in:
parent
aa4bc2ce03
commit
2139a80a7a
@ -3,9 +3,10 @@ import { HomeAssistant } from "../../types";
|
|||||||
import { DOMAINS_WITH_CARD } from "../const";
|
import { DOMAINS_WITH_CARD } from "../const";
|
||||||
import { canToggleState } from "./can_toggle_state";
|
import { canToggleState } from "./can_toggle_state";
|
||||||
import { computeStateDomain } from "./compute_state_domain";
|
import { computeStateDomain } from "./compute_state_domain";
|
||||||
|
import { UNAVAILABLE } from "../../data/entity";
|
||||||
|
|
||||||
export const stateCardType = (hass: HomeAssistant, stateObj: HassEntity) => {
|
export const stateCardType = (hass: HomeAssistant, stateObj: HassEntity) => {
|
||||||
if (stateObj.state === "unavailable") {
|
if (stateObj.state === UNAVAILABLE) {
|
||||||
return "display";
|
return "display";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@ import { stateIcon } from "../../common/entity/state_icon";
|
|||||||
import { timerTimeRemaining } from "../../common/entity/timer_time_remaining";
|
import { timerTimeRemaining } from "../../common/entity/timer_time_remaining";
|
||||||
import { HomeAssistant } from "../../types";
|
import { HomeAssistant } from "../../types";
|
||||||
import "../ha-label-badge";
|
import "../ha-label-badge";
|
||||||
|
import { UNAVAILABLE, UNKNOWN } from "../../data/entity";
|
||||||
|
|
||||||
@customElement("ha-state-label-badge")
|
@customElement("ha-state-label-badge")
|
||||||
export class HaStateLabelBadge extends LitElement {
|
export class HaStateLabelBadge extends LitElement {
|
||||||
@ -81,7 +82,8 @@ export class HaStateLabelBadge extends LitElement {
|
|||||||
? ""
|
? ""
|
||||||
: this.image
|
: this.image
|
||||||
? 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)}"
|
.label="${this._computeLabel(domain, state, this._timerTimeRemaining)}"
|
||||||
.description="${this.name ? this.name : computeStateName(state)}"
|
.description="${this.name ? this.name : computeStateName(state)}"
|
||||||
></ha-label-badge>
|
></ha-label-badge>
|
||||||
@ -108,7 +110,7 @@ export class HaStateLabelBadge extends LitElement {
|
|||||||
return null;
|
return null;
|
||||||
case "sensor":
|
case "sensor":
|
||||||
default:
|
default:
|
||||||
return state.state === "unknown"
|
return state.state === UNKNOWN
|
||||||
? "-"
|
? "-"
|
||||||
: state.attributes.unit_of_measurement
|
: state.attributes.unit_of_measurement
|
||||||
? state.state
|
? state.state
|
||||||
@ -121,7 +123,7 @@ export class HaStateLabelBadge extends LitElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _computeIcon(domain: string, state: HassEntity) {
|
private _computeIcon(domain: string, state: HassEntity) {
|
||||||
if (state.state === "unavailable") {
|
if (state.state === UNAVAILABLE) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
switch (domain) {
|
switch (domain) {
|
||||||
@ -166,7 +168,7 @@ export class HaStateLabelBadge extends LitElement {
|
|||||||
|
|
||||||
private _computeLabel(domain, state, _timerTimeRemaining) {
|
private _computeLabel(domain, state, _timerTimeRemaining) {
|
||||||
if (
|
if (
|
||||||
state.state === "unavailable" ||
|
state.state === UNAVAILABLE ||
|
||||||
["device_tracker", "alarm_control_panel", "person"].includes(domain)
|
["device_tracker", "alarm_control_panel", "person"].includes(domain)
|
||||||
) {
|
) {
|
||||||
// Localize the state with a special state_badge namespace, which has variations of
|
// Localize the state with a special state_badge namespace, which has variations of
|
||||||
|
@ -12,12 +12,13 @@ import {
|
|||||||
import "../../../components/ha-relative-time";
|
import "../../../components/ha-relative-time";
|
||||||
import { triggerAutomation } from "../../../data/automation";
|
import { triggerAutomation } from "../../../data/automation";
|
||||||
import { HomeAssistant } from "../../../types";
|
import { HomeAssistant } from "../../../types";
|
||||||
|
import { UNAVAILABLE_STATES } from "../../../data/entity";
|
||||||
|
|
||||||
@customElement("more-info-automation")
|
@customElement("more-info-automation")
|
||||||
class MoreInfoAutomation extends LitElement {
|
class MoreInfoAutomation extends LitElement {
|
||||||
@property({ attribute: false }) public hass!: HomeAssistant;
|
@property({ attribute: false }) public hass!: HomeAssistant;
|
||||||
|
|
||||||
@property() public stateObj?: HassEntity;
|
@property({ attribute: false }) public stateObj?: HassEntity;
|
||||||
|
|
||||||
protected render(): TemplateResult {
|
protected render(): TemplateResult {
|
||||||
if (!this.hass || !this.stateObj) {
|
if (!this.hass || !this.stateObj) {
|
||||||
@ -36,7 +37,7 @@ class MoreInfoAutomation extends LitElement {
|
|||||||
<div class="actions">
|
<div class="actions">
|
||||||
<mwc-button
|
<mwc-button
|
||||||
@click=${this.handleAction}
|
@click=${this.handleAction}
|
||||||
.disabled=${this.stateObj!.state === "unavailable"}
|
.disabled=${UNAVAILABLE_STATES.includes(this.stateObj!.state)}
|
||||||
>
|
>
|
||||||
${this.hass.localize("ui.card.automation.trigger")}
|
${this.hass.localize("ui.card.automation.trigger")}
|
||||||
</mwc-button>
|
</mwc-button>
|
||||||
|
@ -25,6 +25,7 @@ import {
|
|||||||
showAutomationEditor,
|
showAutomationEditor,
|
||||||
triggerAutomation,
|
triggerAutomation,
|
||||||
} from "../../../data/automation";
|
} from "../../../data/automation";
|
||||||
|
import { UNAVAILABLE_STATES } from "../../../data/entity";
|
||||||
import "../../../layouts/hass-tabs-subpage-data-table";
|
import "../../../layouts/hass-tabs-subpage-data-table";
|
||||||
import { haStyle } from "../../../resources/styles";
|
import { haStyle } from "../../../resources/styles";
|
||||||
import { HomeAssistant, Route } from "../../../types";
|
import { HomeAssistant, Route } from "../../../types";
|
||||||
@ -35,9 +36,9 @@ import { showThingtalkDialog } from "./show-dialog-thingtalk";
|
|||||||
class HaAutomationPicker extends LitElement {
|
class HaAutomationPicker extends LitElement {
|
||||||
@property({ attribute: false }) public hass!: HomeAssistant;
|
@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;
|
@property() public route!: Route;
|
||||||
|
|
||||||
@ -58,7 +59,7 @@ class HaAutomationPicker extends LitElement {
|
|||||||
toggle: {
|
toggle: {
|
||||||
title: "",
|
title: "",
|
||||||
type: "icon",
|
type: "icon",
|
||||||
template: (_toggle, automation) =>
|
template: (_toggle, automation: any) =>
|
||||||
html`
|
html`
|
||||||
<ha-entity-toggle
|
<ha-entity-toggle
|
||||||
.hass=${this.hass}
|
.hass=${this.hass}
|
||||||
@ -95,7 +96,7 @@ class HaAutomationPicker extends LitElement {
|
|||||||
<mwc-button
|
<mwc-button
|
||||||
.automation=${automation}
|
.automation=${automation}
|
||||||
@click=${(ev) => this._execute(ev)}
|
@click=${(ev) => this._execute(ev)}
|
||||||
.disabled=${automation.state === "unavailable"}
|
.disabled=${UNAVAILABLE_STATES.includes(automation.state)}
|
||||||
>
|
>
|
||||||
${this.hass.localize("ui.card.automation.trigger")}
|
${this.hass.localize("ui.card.automation.trigger")}
|
||||||
</mwc-button>
|
</mwc-button>
|
||||||
|
@ -59,6 +59,7 @@ import {
|
|||||||
showEntityEditorDialog,
|
showEntityEditorDialog,
|
||||||
} from "./show-dialog-entity-editor";
|
} from "./show-dialog-entity-editor";
|
||||||
import { haStyle } from "../../../resources/styles";
|
import { haStyle } from "../../../resources/styles";
|
||||||
|
import { UNAVAILABLE } from "../../../data/entity";
|
||||||
|
|
||||||
export interface StateEntity extends EntityRegistryEntry {
|
export interface StateEntity extends EntityRegistryEntry {
|
||||||
readonly?: boolean;
|
readonly?: boolean;
|
||||||
@ -281,7 +282,7 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) {
|
|||||||
|
|
||||||
for (const entry of entities) {
|
for (const entry of entities) {
|
||||||
const entity = this.hass.states[entry.entity_id];
|
const entity = this.hass.states[entry.entity_id];
|
||||||
const unavailable = entity?.state === "unavailable";
|
const unavailable = entity?.state === UNAVAILABLE;
|
||||||
const restored = entity?.attributes.restored;
|
const restored = entity?.attributes.restored;
|
||||||
|
|
||||||
if (!showUnavailable && unavailable) {
|
if (!showUnavailable && unavailable) {
|
||||||
|
@ -20,7 +20,7 @@ import { computeStateName } from "../../../common/entity/compute_state_name";
|
|||||||
import { stateIcon } from "../../../common/entity/state_icon";
|
import { stateIcon } from "../../../common/entity/state_icon";
|
||||||
import { supportsFeature } from "../../../common/entity/supports-feature";
|
import { supportsFeature } from "../../../common/entity/supports-feature";
|
||||||
import "../../../components/ha-card";
|
import "../../../components/ha-card";
|
||||||
import { UNAVAILABLE_STATES } from "../../../data/entity";
|
import { UNAVAILABLE_STATES, UNAVAILABLE } from "../../../data/entity";
|
||||||
import { SUPPORT_BRIGHTNESS } from "../../../data/light";
|
import { SUPPORT_BRIGHTNESS } from "../../../data/light";
|
||||||
import { ActionHandlerEvent } from "../../../data/lovelace";
|
import { ActionHandlerEvent } from "../../../data/lovelace";
|
||||||
import { HomeAssistant, LightEntity } from "../../../types";
|
import { HomeAssistant, LightEntity } from "../../../types";
|
||||||
@ -133,7 +133,7 @@ export class HuiLightCard extends LitElement implements LovelaceCard {
|
|||||||
SUPPORT_BRIGHTNESS
|
SUPPORT_BRIGHTNESS
|
||||||
),
|
),
|
||||||
"state-on": stateObj.state === "on",
|
"state-on": stateObj.state === "on",
|
||||||
"state-unavailable": stateObj.state === "unavailable",
|
"state-unavailable": stateObj.state === UNAVAILABLE,
|
||||||
})}"
|
})}"
|
||||||
.icon=${this._config.icon || stateIcon(stateObj)}
|
.icon=${this._config.icon || stateIcon(stateObj)}
|
||||||
.disabled=${UNAVAILABLE_STATES.includes(stateObj.state)}
|
.disabled=${UNAVAILABLE_STATES.includes(stateObj.state)}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { HomeAssistant } from "../../../types";
|
import { HomeAssistant } from "../../../types";
|
||||||
|
import { UNAVAILABLE } from "../../../data/entity";
|
||||||
|
|
||||||
export interface Condition {
|
export interface Condition {
|
||||||
entity: string;
|
entity: string;
|
||||||
@ -13,7 +14,7 @@ export function checkConditionsMet(
|
|||||||
return conditions.every((c) => {
|
return conditions.every((c) => {
|
||||||
const state = hass.states[c.entity]
|
const state = hass.states[c.entity]
|
||||||
? hass!.states[c.entity].state
|
? hass!.states[c.entity].state
|
||||||
: "unavailable";
|
: UNAVAILABLE;
|
||||||
|
|
||||||
return c.state ? state === c.state : state !== c.state_not;
|
return c.state ? state === c.state : state !== c.state_not;
|
||||||
});
|
});
|
||||||
|
@ -17,6 +17,7 @@ import parseAspectRatio from "../../../common/util/parse-aspect-ratio";
|
|||||||
import "../../../components/ha-camera-stream";
|
import "../../../components/ha-camera-stream";
|
||||||
import { fetchThumbnailUrlWithCache } from "../../../data/camera";
|
import { fetchThumbnailUrlWithCache } from "../../../data/camera";
|
||||||
import { CameraEntity, HomeAssistant } from "../../../types";
|
import { CameraEntity, HomeAssistant } from "../../../types";
|
||||||
|
import { UNAVAILABLE } from "../../../data/entity";
|
||||||
|
|
||||||
const UPDATE_INTERVAL = 10000;
|
const UPDATE_INTERVAL = 10000;
|
||||||
const DEFAULT_FILTER = "grayscale(100%)";
|
const DEFAULT_FILTER = "grayscale(100%)";
|
||||||
@ -73,7 +74,7 @@ export class HuiImage extends LitElement {
|
|||||||
}
|
}
|
||||||
const ratio = this.aspectRatio ? parseAspectRatio(this.aspectRatio) : null;
|
const ratio = this.aspectRatio ? parseAspectRatio(this.aspectRatio) : null;
|
||||||
const stateObj = this.entity ? this.hass.states[this.entity] : undefined;
|
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
|
// Figure out image source to use
|
||||||
let imageSrc: string | undefined;
|
let imageSrc: string | undefined;
|
||||||
|
@ -11,7 +11,7 @@ import "../../../components/ha-date-input";
|
|||||||
import type { HaDateInput } from "../../../components/ha-date-input";
|
import type { HaDateInput } from "../../../components/ha-date-input";
|
||||||
import "../../../components/paper-time-input";
|
import "../../../components/paper-time-input";
|
||||||
import type { PaperTimeInput } from "../../../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 { setInputDateTimeValue } from "../../../data/input_datetime";
|
||||||
import type { HomeAssistant } from "../../../types";
|
import type { HomeAssistant } from "../../../types";
|
||||||
import { hasConfigOrEntityChanged } from "../common/has-changed";
|
import { hasConfigOrEntityChanged } from "../common/has-changed";
|
||||||
@ -70,10 +70,10 @@ class HuiInputDatetimeEntityRow extends LitElement implements LovelaceRow {
|
|||||||
? html`
|
? html`
|
||||||
<paper-time-input
|
<paper-time-input
|
||||||
.disabled=${UNAVAILABLE_STATES.includes(stateObj.state)}
|
.disabled=${UNAVAILABLE_STATES.includes(stateObj.state)}
|
||||||
.hour=${stateObj.state === "unknown"
|
.hour=${stateObj.state === UNKNOWN
|
||||||
? ""
|
? ""
|
||||||
: ("0" + stateObj.attributes.hour).slice(-2)}
|
: ("0" + stateObj.attributes.hour).slice(-2)}
|
||||||
.min=${stateObj.state === "unknown"
|
.min=${stateObj.state === UNKNOWN
|
||||||
? ""
|
? ""
|
||||||
: ("0" + stateObj.attributes.minute).slice(-2)}
|
: ("0" + stateObj.attributes.minute).slice(-2)}
|
||||||
.amPm=${false}
|
.amPm=${false}
|
||||||
|
@ -22,6 +22,7 @@ import { actionHandler } from "../common/directives/action-handler-directive";
|
|||||||
import { hasAction } from "../common/has-action";
|
import { hasAction } from "../common/has-action";
|
||||||
import { ActionHandlerEvent } from "../../../data/lovelace";
|
import { ActionHandlerEvent } from "../../../data/lovelace";
|
||||||
import { handleAction } from "../common/handle-action";
|
import { handleAction } from "../common/handle-action";
|
||||||
|
import { UNAVAILABLE_STATES } from "../../../data/entity";
|
||||||
|
|
||||||
interface SensorEntityConfig extends EntitiesCardEntityConfig {
|
interface SensorEntityConfig extends EntitiesCardEntityConfig {
|
||||||
format?: "relative" | "date" | "time" | "datetime";
|
format?: "relative" | "date" | "time" | "datetime";
|
||||||
@ -71,8 +72,7 @@ class HuiSensorEntityRow extends LitElement implements LovelaceRow {
|
|||||||
>
|
>
|
||||||
${stateObj.attributes.device_class ===
|
${stateObj.attributes.device_class ===
|
||||||
SENSOR_DEVICE_CLASS_TIMESTAMP &&
|
SENSOR_DEVICE_CLASS_TIMESTAMP &&
|
||||||
stateObj.state !== "unavailable" &&
|
!UNAVAILABLE_STATES.includes(stateObj.state)
|
||||||
stateObj.state !== "unknown"
|
|
||||||
? html`
|
? html`
|
||||||
<hui-timestamp-display
|
<hui-timestamp-display
|
||||||
.hass=${this.hass}
|
.hass=${this.hass}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { assert } from "chai";
|
import { assert } from "chai";
|
||||||
import { computeStateDisplay } from "../../../src/common/entity/compute_state_display";
|
import { computeStateDisplay } from "../../../src/common/entity/compute_state_display";
|
||||||
|
import { UNKNOWN } from "../../../src/data/entity";
|
||||||
|
|
||||||
describe("computeStateDisplay", () => {
|
describe("computeStateDisplay", () => {
|
||||||
// Mock Localize function for testing
|
// Mock Localize function for testing
|
||||||
@ -72,7 +73,7 @@ describe("computeStateDisplay", () => {
|
|||||||
};
|
};
|
||||||
const stateObj: any = {
|
const stateObj: any = {
|
||||||
entity_id: "sensor.test",
|
entity_id: "sensor.test",
|
||||||
state: "unknown",
|
state: UNKNOWN,
|
||||||
attributes: {
|
attributes: {
|
||||||
unit_of_measurement: "m",
|
unit_of_measurement: "m",
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user