Add support for external stats (#10411)

This commit is contained in:
Bram Kragten 2021-10-27 08:15:57 +02:00 committed by GitHub
parent 171eddd779
commit bd1a9f2cb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 28 additions and 16 deletions

View File

@ -102,7 +102,12 @@ export class HaStatisticPicker extends LitElement {
</style>
<ha-svg-icon .path=${mdiCheck}></ha-svg-icon>
<paper-icon-item>
<state-badge slot="item-icon" .stateObj=${item.state}></state-badge>
${item.state
? html`<state-badge
slot="item-icon"
.stateObj=${item.state}
></state-badge>`
: ""}
<paper-item-body two-line="">
${item.name}
<span secondary
@ -153,7 +158,10 @@ export class HaStatisticPicker extends LitElement {
const entityState = this.hass.states[meta.statistic_id];
if (!entityState) {
if (!entitiesOnly) {
output.push({ id: meta.statistic_id, name: meta.statistic_id });
output.push({
id: meta.statistic_id,
name: meta.name || meta.statistic_id,
});
}
return;
}

View File

@ -74,6 +74,8 @@ export interface StatisticValue {
export interface StatisticsMetaData {
unit_of_measurement: string;
statistic_id: string;
source: string;
name?: string | null;
}
export type StatisticsValidationResult =

View File

@ -73,7 +73,6 @@ export class DialogEnergyBatterySettings
.label=${this.hass.localize(
"ui.panel.config.energy.battery.dialog.energy_into_battery"
)}
entities-only
@value-changed=${this._statisticToChanged}
></ha-statistic-picker>
@ -85,7 +84,6 @@ export class DialogEnergyBatterySettings
.label=${this.hass.localize(
"ui.panel.config.energy.battery.dialog.energy_out_of_battery"
)}
entities-only
@value-changed=${this._statisticFromChanged}
></ha-statistic-picker>

View File

@ -74,7 +74,6 @@ export class DialogEnergyDeviceSettings
.label=${this.hass.localize(
"ui.panel.config.energy.device_consumption.dialog.device_consumption_energy"
)}
entities-only
@value-changed=${this._statisticChanged}
></ha-statistic-picker>

View File

@ -106,7 +106,6 @@ export class DialogEnergyGasSettings
? "kWh"
: "m³"
})`}
entities-only
@value-changed=${this._statisticChanged}
></ha-statistic-picker>

View File

@ -103,7 +103,6 @@ export class DialogEnergyGridFlowSettings
.label=${this.hass.localize(
`ui.panel.config.energy.grid.flow_dialog.${this._params.direction}.energy_stat`
)}
entities-only
@value-changed=${this._statisticChanged}
></ha-statistic-picker>

View File

@ -85,7 +85,6 @@ export class DialogEnergySolarSettings
.label=${this.hass.localize(
"ui.panel.config.energy.solar.dialog.solar_production_energy"
)}
entities-only
@value-changed=${this._statisticChanged}
></ha-statistic-picker>

View File

@ -50,21 +50,21 @@ class HaPanelDevStatistics extends LitElement {
private _columns = memoizeOne(
(localize): DataTableColumnContainer => ({
state: {
title: "Entity",
title: "Name",
sortable: true,
filterable: true,
grows: true,
template: (entityState, data: any) =>
html`${entityState
? computeStateName(entityState)
: data.statistic_id}`,
: data.name || data.statistic_id}`,
},
statistic_id: {
title: "Statistic id",
sortable: true,
filterable: true,
hidden: this.narrow,
width: "30%",
width: "20%",
},
unit_of_measurement: {
title: "Unit",
@ -72,6 +72,12 @@ class HaPanelDevStatistics extends LitElement {
filterable: true,
width: "10%",
},
source: {
title: "Source",
sortable: true,
filterable: true,
width: "10%",
},
issues: {
title: "Issue",
sortable: true,
@ -146,6 +152,7 @@ class HaPanelDevStatistics extends LitElement {
this._data.push({
statistic_id: statisticId,
unit_of_measurement: "",
source: "",
state: this.hass.states[statisticId],
issues: issues[statisticId],
});

View File

@ -78,7 +78,7 @@ export class HuiStatisticsGraphCard extends LitElement implements LovelaceCard {
}
const configEntities = config.entities
? processConfigEntities(config.entities)
? processConfigEntities(config.entities, false)
: [];
this._entities = [];

View File

@ -52,7 +52,7 @@ export function hasConfigOrEntitiesChanged(
const oldHass = changedProps.get("hass") as HomeAssistant;
const entities = processConfigEntities(element._config!.entities);
const entities = processConfigEntities(element._config!.entities, false);
return entities.some(
(entity) =>

View File

@ -5,7 +5,8 @@ import { EntityConfig, LovelaceRowConfig } from "../entity-rows/types";
export const processConfigEntities = <
T extends EntityConfig | LovelaceRowConfig
>(
entities: Array<T | string>
entities: Array<T | string>,
checkEntityId = true
): T[] => {
if (!entities || !Array.isArray(entities)) {
throw new Error("Entities need to be an array");
@ -35,7 +36,7 @@ export const processConfigEntities = <
throw new Error(`Invalid entity specified at position ${index}.`);
}
if (!isValidEntityId((config as EntityConfig).entity!)) {
if (checkEntityId && !isValidEntityId((config as EntityConfig).entity!)) {
throw new Error(
`Invalid entity ID at position ${index}: ${
(config as EntityConfig).entity

View File

@ -61,7 +61,7 @@ export class HuiStatisticsGraphCardEditor
assert(config, cardConfigStruct);
this._config = config;
this._configEntities = config.entities
? processConfigEntities(config.entities).map((cfg) => cfg.entity)
? processConfigEntities(config.entities, false).map((cfg) => cfg.entity)
: [];
}