@@ -2,6 +2,7 @@ import type { PropertyValues } from "lit";
22import { LitElement , css , html , nothing } from "lit" ;
33import { customElement , property , state } from "lit/decorators" ;
44import { classMap } from "lit/directives/class-map" ;
5+ import { theme2hex } from "../../../common/color/convert-color" ;
56import { isComponentLoaded } from "../../../common/config/is_component_loaded" ;
67import { createSearchParam } from "../../../common/url/search-params" ;
78import "../../../components/chart/state-history-charts" ;
@@ -21,9 +22,8 @@ import { getSensorNumericDeviceClasses } from "../../../data/sensor";
2122import type { HomeAssistant } from "../../../types" ;
2223import { hasConfigOrEntitiesChanged } from "../common/has-changed" ;
2324import { processConfigEntities } from "../common/process-config-entities" ;
24- import type { EntityConfig } from "../entity-rows/types" ;
2525import type { LovelaceCard , LovelaceGridOptions } from "../types" ;
26- import type { HistoryGraphCardConfig } from "./types" ;
26+ import type { GraphEntityConfig , HistoryGraphCardConfig } from "./types" ;
2727
2828export const DEFAULT_HOURS_TO_SHOW = 24 ;
2929
@@ -51,9 +51,11 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
5151
5252 private _names : Record < string , string > = { } ;
5353
54+ private _colors : Record < string , string | undefined > = { } ;
55+
5456 private _entityIds : string [ ] = [ ] ;
5557
56- private _entities : EntityConfig [ ] = [ ] ;
58+ private _entities : GraphEntityConfig [ ] = [ ] ;
5759
5860 private _historyLinkId = `history-${ Math . random ( ) . toString ( 36 ) . substring ( 2 , 9 ) } ` ;
5961
@@ -95,6 +97,7 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
9597
9698 this . _config = config ;
9799 this . _computeNames ( ) ;
100+ this . _computeColors ( ) ;
98101 }
99102
100103 private _computeNames ( ) {
@@ -110,6 +113,19 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
110113 } ) ;
111114 }
112115
116+ private _computeColors ( ) {
117+ if ( ! this . _config ) {
118+ return ;
119+ }
120+ this . _colors = { } ;
121+ this . _entities . forEach ( ( entity ) => {
122+ // if color = undefined, it is automatically defined inside a chart component
123+ this . _colors [ entity . entity ] = entity . color
124+ ? theme2hex ( entity . color )
125+ : undefined ;
126+ } ) ;
127+ }
128+
113129 public willUpdate ( changedProps : PropertyValues < this> ) {
114130 super . willUpdate ( changedProps ) ;
115131 if ( changedProps . has ( "hass" ) ) {
@@ -371,6 +387,7 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
371387 .minYAxis = ${ this . _config . min_y_axis }
372388 .maxYAxis = ${ this . _config . max_y_axis }
373389 .fitYData = ${ this . _config . fit_y_data || false }
390+ .colors = ${ this . _colors }
374391 .height = ${ hasFixedHeight ? "100%" : undefined }
375392 .narrow = ${ narrow }
376393 .expandLegend = ${ this . _config . expand_legend }
0 commit comments