mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 10:16:46 +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 { 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";
|
||||
}
|
||||
|
||||
|
@ -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)}"
|
||||
></ha-label-badge>
|
||||
@ -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
|
||||
|
@ -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 {
|
||||
<div class="actions">
|
||||
<mwc-button
|
||||
@click=${this.handleAction}
|
||||
.disabled=${this.stateObj!.state === "unavailable"}
|
||||
.disabled=${UNAVAILABLE_STATES.includes(this.stateObj!.state)}
|
||||
>
|
||||
${this.hass.localize("ui.card.automation.trigger")}
|
||||
</mwc-button>
|
||||
|
@ -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`
|
||||
<ha-entity-toggle
|
||||
.hass=${this.hass}
|
||||
@ -95,7 +96,7 @@ class HaAutomationPicker extends LitElement {
|
||||
<mwc-button
|
||||
.automation=${automation}
|
||||
@click=${(ev) => this._execute(ev)}
|
||||
.disabled=${automation.state === "unavailable"}
|
||||
.disabled=${UNAVAILABLE_STATES.includes(automation.state)}
|
||||
>
|
||||
${this.hass.localize("ui.card.automation.trigger")}
|
||||
</mwc-button>
|
||||
|
@ -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) {
|
||||
|
@ -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)}
|
||||
|
@ -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;
|
||||
});
|
||||
|
@ -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;
|
||||
|
@ -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`
|
||||
<paper-time-input
|
||||
.disabled=${UNAVAILABLE_STATES.includes(stateObj.state)}
|
||||
.hour=${stateObj.state === "unknown"
|
||||
.hour=${stateObj.state === UNKNOWN
|
||||
? ""
|
||||
: ("0" + stateObj.attributes.hour).slice(-2)}
|
||||
.min=${stateObj.state === "unknown"
|
||||
.min=${stateObj.state === UNKNOWN
|
||||
? ""
|
||||
: ("0" + stateObj.attributes.minute).slice(-2)}
|
||||
.amPm=${false}
|
||||
|
@ -22,6 +22,7 @@ import { actionHandler } from "../common/directives/action-handler-directive";
|
||||
import { hasAction } from "../common/has-action";
|
||||
import { ActionHandlerEvent } from "../../../data/lovelace";
|
||||
import { handleAction } from "../common/handle-action";
|
||||
import { UNAVAILABLE_STATES } from "../../../data/entity";
|
||||
|
||||
interface SensorEntityConfig extends EntitiesCardEntityConfig {
|
||||
format?: "relative" | "date" | "time" | "datetime";
|
||||
@ -71,8 +72,7 @@ class HuiSensorEntityRow extends LitElement implements LovelaceRow {
|
||||
>
|
||||
${stateObj.attributes.device_class ===
|
||||
SENSOR_DEVICE_CLASS_TIMESTAMP &&
|
||||
stateObj.state !== "unavailable" &&
|
||||
stateObj.state !== "unknown"
|
||||
!UNAVAILABLE_STATES.includes(stateObj.state)
|
||||
? html`
|
||||
<hui-timestamp-display
|
||||
.hass=${this.hass}
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { assert } from "chai";
|
||||
import { computeStateDisplay } from "../../../src/common/entity/compute_state_display";
|
||||
import { UNKNOWN } from "../../../src/data/entity";
|
||||
|
||||
describe("computeStateDisplay", () => {
|
||||
// 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",
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user