Automatically strip area name from entities (#4597)

This commit is contained in:
Paulus Schoutsen 2020-01-25 05:43:11 -08:00 committed by Bram Kragten
parent f95ba4c04c
commit 8abe8d7615
2 changed files with 48 additions and 20 deletions

View File

@ -178,7 +178,7 @@ export interface PictureEntityCardConfig extends LovelaceCardConfig {
camera_image?: string; camera_image?: string;
camera_view?: HuiImage["cameraView"]; camera_view?: HuiImage["cameraView"];
state_image?: {}; state_image?: {};
state_filter: string[]; state_filter?: string[];
aspect_ratio?: string; aspect_ratio?: string;
tap_action?: ActionConfig; tap_action?: ActionConfig;
hold_action?: ActionConfig; hold_action?: ActionConfig;

View File

@ -20,7 +20,13 @@ import { computeDomain } from "../../../common/entity/compute_domain";
import { LovelaceRowConfig, WeblinkConfig } from "../entity-rows/types"; import { LovelaceRowConfig, WeblinkConfig } from "../entity-rows/types";
import { LocalizeFunc } from "../../../common/translations/localize"; import { LocalizeFunc } from "../../../common/translations/localize";
import { EntitiesCardConfig } from "../cards/types"; import {
EntitiesCardConfig,
AlarmPanelCardConfig,
PictureEntityCardConfig,
ThermostatCardConfig,
LightCardConfig,
} from "../cards/types";
import { import {
subscribeAreaRegistry, subscribeAreaRegistry,
AreaRegistryEntry, AreaRegistryEntry,
@ -107,64 +113,86 @@ export const computeCards = (
// For entity card // For entity card
const entities: Array<string | LovelaceRowConfig> = []; const entities: Array<string | LovelaceRowConfig> = [];
const titlePrefix = entityCardOptions.title
? `${entityCardOptions.title} `
: undefined;
for (const [entityId, stateObj] of states) { for (const [entityId, stateObj] of states) {
const domain = computeDomain(entityId); const domain = computeDomain(entityId);
if (domain === "alarm_control_panel") { if (domain === "alarm_control_panel") {
cards.push({ const cardConfig: AlarmPanelCardConfig = {
type: "alarm-panel", type: "alarm-panel",
entity: entityId, entity: entityId,
}); };
cards.push(cardConfig);
} else if (domain === "camera") { } else if (domain === "camera") {
cards.push({ const cardConfig: PictureEntityCardConfig = {
type: "picture-entity", type: "picture-entity",
entity: entityId, entity: entityId,
}); };
cards.push(cardConfig);
} else if (domain === "climate") { } else if (domain === "climate") {
cards.push({ const cardConfig: ThermostatCardConfig = {
type: "thermostat", type: "thermostat",
entity: entityId, entity: entityId,
}); };
cards.push(cardConfig);
} else if (domain === "history_graph" && stateObj) { } else if (domain === "history_graph" && stateObj) {
cards.push({ const cardConfig = {
type: "history-graph", type: "history-graph",
entities: stateObj.attributes.entity_id, entities: stateObj.attributes.entity_id,
hours_to_show: stateObj.attributes.hours_to_show, hours_to_show: stateObj.attributes.hours_to_show,
title: stateObj.attributes.friendly_name, title: stateObj.attributes.friendly_name,
refresh_interval: stateObj.attributes.refresh, refresh_interval: stateObj.attributes.refresh,
}); };
cards.push(cardConfig);
} else if (domain === "light" && single) { } else if (domain === "light" && single) {
cards.push({ const cardConfig: LightCardConfig = {
type: "light", type: "light",
entity: entityId, entity: entityId,
}); };
cards.push(cardConfig);
} else if (domain === "media_player") { } else if (domain === "media_player") {
cards.push({ const cardConfig = {
type: "media-control", type: "media-control",
entity: entityId, entity: entityId,
}); };
cards.push(cardConfig);
} else if (domain === "plant") { } else if (domain === "plant") {
cards.push({ const cardConfig = {
type: "plant-status", type: "plant-status",
entity: entityId, entity: entityId,
}); };
cards.push(cardConfig);
} else if (domain === "weather") { } else if (domain === "weather") {
cards.push({ const cardConfig = {
type: "weather-forecast", type: "weather-forecast",
entity: entityId, entity: entityId,
}); };
cards.push(cardConfig);
} else if (domain === "weblink" && stateObj) { } else if (domain === "weblink" && stateObj) {
const conf: WeblinkConfig = { const conf: WeblinkConfig = {
type: "weblink", type: "weblink",
url: stateObj.state, url: stateObj.state,
name: computeStateName(stateObj),
}; };
if ("icon" in stateObj.attributes) { if ("icon" in stateObj.attributes) {
conf.icon = stateObj.attributes.icon; conf.icon = stateObj.attributes.icon;
} }
entities.push(conf); entities.push(conf);
} else { } else {
entities.push(entityId); let name: string;
const entityConf =
titlePrefix &&
stateObj &&
(name = computeStateName(stateObj)).startsWith(titlePrefix)
? {
entity: entityId,
name: name.substr(titlePrefix.length),
}
: entityId;
entities.push(entityConf);
} }
} }