@@ -40,7 +40,19 @@ import { calcDateRange } from "../common/datetime/calc_date_range";
4040import type { DateRange } from "../common/datetime/calc_date_range" ;
4141import { formatNumber } from "../common/number/format_number" ;
4242
43- const energyCollectionKeys : ( string | undefined ) [ ] = [ ] ;
43+ export const ENERGY_COLLECTION_KEY_PREFIX = "energy_" ;
44+
45+ // All collection keys created this session
46+ const energyCollectionKeys = new Set < string | undefined > ( ) ;
47+
48+ // Validate that a string is a valid energy collection key.
49+ export function validateEnergyCollectionKey ( key : string | undefined ) {
50+ if ( ! key ?. startsWith ( ENERGY_COLLECTION_KEY_PREFIX ) ) {
51+ throw new Error (
52+ `Collection keys must start with ${ ENERGY_COLLECTION_KEY_PREFIX } .`
53+ ) ;
54+ }
55+ }
4456
4557export const emptyGridSourceEnergyPreference =
4658 ( ) : GridSourceTypeEnergyPreference => ( {
@@ -693,17 +705,20 @@ export interface EnergyCollection extends Collection<EnergyData> {
693705 clearPrefs ( ) : void ;
694706 setPeriod ( newStart : Date , newEnd ?: Date ) : void ;
695707 setCompare ( compare : CompareMode ) : void ;
708+ isActive ( ) : boolean ;
696709 _refreshTimeout ?: number ;
697710 _updatePeriodTimeout ?: number ;
698711 _active : number ;
699712}
700713
701714const clearEnergyCollectionPreferences = ( hass : HomeAssistant ) => {
702715 energyCollectionKeys . forEach ( ( key ) => {
703- const energyCollection = getEnergyDataCollection ( hass , { key } ) ;
704- energyCollection . clearPrefs ( ) ;
705- if ( energyCollection . _active ) {
706- energyCollection . refresh ( ) ;
716+ const energyCollection = findEnergyDataCollection ( hass , key ) ;
717+ if ( energyCollection ) {
718+ energyCollection . clearPrefs ( ) ;
719+ if ( energyCollection . isActive ( ) ) {
720+ energyCollection . refresh ( ) ;
721+ }
707722 }
708723 } ) ;
709724} ;
@@ -730,23 +745,47 @@ const scheduleHourlyRefresh = (collection: EnergyCollection) => {
730745 }
731746} ;
732747
733- export const getEnergyDataCollection = (
748+ const convertCollectionKeyToConnection = (
734749 hass : HomeAssistant ,
735- options : { prefs ?: EnergyPreferences ; key ?: string } = { }
736- ) : EnergyCollection => {
750+ collectionKey : string | undefined
751+ ) : [ string , string | undefined ] => {
737752 let key = "_energy" ;
738- if ( options . key ) {
739- if ( ! options . key . startsWith ( "energy_" ) ) {
740- throw new Error ( "Key need to start with energy_" ) ;
741- }
742- key = `_${ options . key } ` ;
753+ if ( collectionKey ) {
754+ validateEnergyCollectionKey ( collectionKey ) ;
755+ key = `_${ collectionKey } ` ;
756+ } else if ( hass . panelUrl ) {
757+ const defaultKey = ENERGY_COLLECTION_KEY_PREFIX + hass . panelUrl ;
758+ key = `_${ defaultKey } ` ;
759+ collectionKey = defaultKey ;
743760 }
761+ return [ key , collectionKey ] ;
762+ } ;
763+
764+ const findEnergyDataCollection = (
765+ hass : HomeAssistant ,
766+ collectionKey : string | undefined
767+ ) : EnergyCollection | undefined => {
768+ // Lookup the connection key and default key name
769+ const [ key , _collectionKey ] = convertCollectionKeyToConnection (
770+ hass ,
771+ collectionKey
772+ ) ;
773+ return ( hass . connection as any ) [ key ] ;
774+ } ;
744775
776+ export const getEnergyDataCollection = (
777+ hass : HomeAssistant ,
778+ options : { prefs ?: EnergyPreferences ; key ?: string } = { }
779+ ) : EnergyCollection => {
780+ const [ key , collectionKey ] = convertCollectionKeyToConnection (
781+ hass ,
782+ options . key
783+ ) ;
745784 if ( ( hass . connection as any ) [ key ] ) {
746785 return ( hass . connection as any ) [ key ] ;
747786 }
748787
749- energyCollectionKeys . push ( options . key ) ;
788+ energyCollectionKeys . add ( collectionKey ) ;
750789
751790 const collection = getCollection < EnergyData > (
752791 hass . connection ,
@@ -832,6 +871,7 @@ export const getEnergyDataCollection = (
832871 } ;
833872 scheduleUpdatePeriod ( ) ;
834873
874+ collection . isActive = ( ) => ! ! collection . _active ;
835875 collection . clearPrefs = ( ) => {
836876 collection . prefs = undefined ;
837877 } ;
0 commit comments