mirror of
https://github.com/home-assistant/frontend.git
synced 2025-12-20 02:38:53 +00:00
Append current state to power-sources-graph (#28330)
This commit is contained in:
@@ -11,7 +11,7 @@ import {
|
|||||||
isLastDayOfMonth,
|
isLastDayOfMonth,
|
||||||
addYears,
|
addYears,
|
||||||
} from "date-fns";
|
} from "date-fns";
|
||||||
import type { Collection } from "home-assistant-js-websocket";
|
import type { Collection, HassEntity } from "home-assistant-js-websocket";
|
||||||
import { getCollection } from "home-assistant-js-websocket";
|
import { getCollection } from "home-assistant-js-websocket";
|
||||||
import memoizeOne from "memoize-one";
|
import memoizeOne from "memoize-one";
|
||||||
import {
|
import {
|
||||||
@@ -1361,3 +1361,37 @@ export const calculateSolarConsumedGauge = (
|
|||||||
}
|
}
|
||||||
return undefined;
|
return undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get current power value from entity state, normalized to kW
|
||||||
|
* @param stateObj - The entity state object to get power value from
|
||||||
|
* @returns Power value in kW, or 0 if entity not found or invalid
|
||||||
|
*/
|
||||||
|
export const getPowerFromState = (stateObj: HassEntity): number | undefined => {
|
||||||
|
if (!stateObj) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
const value = parseFloat(stateObj.state);
|
||||||
|
if (isNaN(value)) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normalize to kW based on unit of measurement (case-sensitive)
|
||||||
|
// Supported units: GW, kW, MW, mW, TW, W
|
||||||
|
const unit = stateObj.attributes.unit_of_measurement;
|
||||||
|
switch (unit) {
|
||||||
|
case "W":
|
||||||
|
return value / 1000;
|
||||||
|
case "mW":
|
||||||
|
return value / 1000000;
|
||||||
|
case "MW":
|
||||||
|
return value * 1000;
|
||||||
|
case "GW":
|
||||||
|
return value * 1000000;
|
||||||
|
case "TW":
|
||||||
|
return value * 1000000000;
|
||||||
|
default:
|
||||||
|
// Assume kW if no unit or unit is kW
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
@@ -6,7 +6,10 @@ import { classMap } from "lit/directives/class-map";
|
|||||||
import "../../../../components/ha-card";
|
import "../../../../components/ha-card";
|
||||||
import "../../../../components/ha-svg-icon";
|
import "../../../../components/ha-svg-icon";
|
||||||
import type { EnergyData, EnergyPreferences } from "../../../../data/energy";
|
import type { EnergyData, EnergyPreferences } from "../../../../data/energy";
|
||||||
import { getEnergyDataCollection } from "../../../../data/energy";
|
import {
|
||||||
|
getEnergyDataCollection,
|
||||||
|
getPowerFromState,
|
||||||
|
} from "../../../../data/energy";
|
||||||
import { SubscribeMixin } from "../../../../mixins/subscribe-mixin";
|
import { SubscribeMixin } from "../../../../mixins/subscribe-mixin";
|
||||||
import type { HomeAssistant } from "../../../../types";
|
import type { HomeAssistant } from "../../../../types";
|
||||||
import type { LovelaceCard, LovelaceGridOptions } from "../../types";
|
import type { LovelaceCard, LovelaceGridOptions } from "../../types";
|
||||||
@@ -724,33 +727,7 @@ class HuiPowerSankeyCard
|
|||||||
// Track this entity for state change detection
|
// Track this entity for state change detection
|
||||||
this._entities.add(entityId);
|
this._entities.add(entityId);
|
||||||
|
|
||||||
const stateObj = this.hass.states[entityId];
|
return getPowerFromState(this.hass.states[entityId]) ?? 0;
|
||||||
if (!stateObj) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
const value = parseFloat(stateObj.state);
|
|
||||||
if (isNaN(value)) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Normalize to kW based on unit of measurement (case-sensitive)
|
|
||||||
// Supported units: GW, kW, MW, mW, TW, W
|
|
||||||
const unit = stateObj.attributes.unit_of_measurement;
|
|
||||||
switch (unit) {
|
|
||||||
case "W":
|
|
||||||
return value / 1000;
|
|
||||||
case "mW":
|
|
||||||
return value / 1000000;
|
|
||||||
case "MW":
|
|
||||||
return value * 1000;
|
|
||||||
case "GW":
|
|
||||||
return value * 1000000;
|
|
||||||
case "TW":
|
|
||||||
return value * 1000000000;
|
|
||||||
default:
|
|
||||||
// Assume kW if no unit or unit is kW
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -10,7 +10,10 @@ import { LinearGradient } from "../../../../resources/echarts/echarts";
|
|||||||
import "../../../../components/chart/ha-chart-base";
|
import "../../../../components/chart/ha-chart-base";
|
||||||
import "../../../../components/ha-card";
|
import "../../../../components/ha-card";
|
||||||
import type { EnergyData } from "../../../../data/energy";
|
import type { EnergyData } from "../../../../data/energy";
|
||||||
import { getEnergyDataCollection } from "../../../../data/energy";
|
import {
|
||||||
|
getEnergyDataCollection,
|
||||||
|
getPowerFromState,
|
||||||
|
} from "../../../../data/energy";
|
||||||
import type { StatisticValue } from "../../../../data/recorder";
|
import type { StatisticValue } from "../../../../data/recorder";
|
||||||
import type { FrontendLocaleData } from "../../../../data/translation";
|
import type { FrontendLocaleData } from "../../../../data/translation";
|
||||||
import { SubscribeMixin } from "../../../../mixins/subscribe-mixin";
|
import { SubscribeMixin } from "../../../../mixins/subscribe-mixin";
|
||||||
@@ -197,6 +200,7 @@ export class HuiPowerSourcesGraphCard
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const now = Date.now();
|
||||||
Object.keys(statIds).forEach((key, keyIndex) => {
|
Object.keys(statIds).forEach((key, keyIndex) => {
|
||||||
if (statIds[key].stats.length) {
|
if (statIds[key].stats.length) {
|
||||||
const colorHex = computedStyles.getPropertyValue(statIds[key].color);
|
const colorHex = computedStyles.getPropertyValue(statIds[key].color);
|
||||||
@@ -204,7 +208,14 @@ export class HuiPowerSourcesGraphCard
|
|||||||
// Echarts is supposed to handle that but it is bugged when you use it together with stacking.
|
// Echarts is supposed to handle that but it is bugged when you use it together with stacking.
|
||||||
// The interpolation breaks the stacking, so this positive/negative is a workaround
|
// The interpolation breaks the stacking, so this positive/negative is a workaround
|
||||||
const { positive, negative } = this._processData(
|
const { positive, negative } = this._processData(
|
||||||
statIds[key].stats.map((id: string) => energyData.stats[id] ?? [])
|
statIds[key].stats.map((id: string) => {
|
||||||
|
const stats = energyData.stats[id] ?? [];
|
||||||
|
const currentState = getPowerFromState(this.hass.states[id]);
|
||||||
|
if (currentState !== undefined) {
|
||||||
|
stats.push({ start: now, end: now, mean: currentState });
|
||||||
|
}
|
||||||
|
return stats;
|
||||||
|
})
|
||||||
);
|
);
|
||||||
datasets.push({
|
datasets.push({
|
||||||
...commonSeriesOptions,
|
...commonSeriesOptions,
|
||||||
|
|||||||
Reference in New Issue
Block a user