1+ import SankeyChart from '../src/ha-sankey-chart' ;
2+ import { HomeAssistantReal } from '../src/hass' ;
3+
4+ // Mock dependencies
5+ jest . mock ( '../src/energy' , ( ) => ( {
6+ ...jest . requireActual ( '../src/energy' ) ,
7+ getEnergyPreferences : jest . fn ( ) ,
8+ getEnergySourceColor : ( type : string ) => `color-for-${ type } ` ,
9+ } ) ) ;
10+ jest . mock ( '../src/hass' , ( ) => ( {
11+ ...jest . requireActual ( '../src/hass' ) ,
12+ getEntitiesByArea : jest . fn ( ) ,
13+ fetchFloorRegistry : jest . fn ( ) ,
14+ } ) ) ;
15+
16+ import { getEnergyPreferences } from '../src/energy' ;
17+ import { getEntitiesByArea , fetchFloorRegistry } from '../src/hass' ;
18+ import { DEFAULT_CONFIG } from '../src/types' ;
19+
20+ describe ( 'SankeyChart autoconfig' , ( ) => {
21+ let sankeyChart : SankeyChart ;
22+ let hass : HomeAssistantReal ;
23+
24+ beforeEach ( ( ) => {
25+ sankeyChart = new SankeyChart ( ) ;
26+ hass = ( {
27+ states : {
28+ 'sensor.grid_in' : { entity_id : 'sensor.grid_in' , state : '10' } ,
29+ 'sensor.solar' : { entity_id : 'sensor.solar' , state : '5' } ,
30+ 'sensor.battery_in' : { entity_id : 'sensor.battery_in' , state : '2' } ,
31+ 'sensor.device1' : { entity_id : 'sensor.device1' , state : '3' } ,
32+ } ,
33+ areas : {
34+ 'area1' : { area_id : 'area1' , name : 'Area 1' } ,
35+ } ,
36+ } as unknown ) as HomeAssistantReal ;
37+ sankeyChart . hass = hass ;
38+ sankeyChart . setConfig ( { ...DEFAULT_CONFIG , autoconfig : { } } , true ) ;
39+ } ) ;
40+
41+ it ( 'creates sections from energy preferences' , async ( ) => {
42+ ( getEnergyPreferences as jest . Mock ) . mockResolvedValue ( {
43+ energy_sources : [
44+ { type : 'grid' , stat_energy_from : 'sensor.grid_in' } ,
45+ { type : 'solar' , stat_energy_from : 'sensor.solar' } ,
46+ { type : 'battery' , stat_energy_from : 'sensor.battery_in' , stat_energy_to : 'sensor.battery_out' } ,
47+ ] ,
48+ device_consumption : [
49+ { stat_consumption : 'sensor.device1' , name : 'Device 1' } ,
50+ ] ,
51+ } ) ;
52+ ( getEntitiesByArea as jest . Mock ) . mockResolvedValue ( {
53+ area1 : { area : { area_id : 'area1' , name : 'Area 1' } , entities : [ 'sensor.device1' ] } ,
54+ } ) ;
55+ ( fetchFloorRegistry as jest . Mock ) . mockResolvedValue ( [ ] ) ;
56+
57+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
58+ await ( sankeyChart as any ) [ 'autoconfig' ] ( ) ;
59+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
60+ const config = ( sankeyChart as any ) . config ;
61+ expect ( Array . isArray ( config . sections ) ) . toBe ( true ) ;
62+ expect ( config . sections . length ) . toBeGreaterThan ( 0 ) ;
63+ const allEntities = config . sections . flatMap ( ( s : { entities : { entity_id : string } [ ] } ) => s . entities . map ( ( e : { entity_id : string } ) => e . entity_id ) ) ;
64+ expect ( allEntities ) . toContain ( 'sensor.grid_in' ) ;
65+ expect ( allEntities ) . toContain ( 'sensor.solar' ) ;
66+ expect ( allEntities ) . toContain ( 'sensor.battery_in' ) ;
67+ expect ( allEntities ) . toContain ( 'sensor.device1' ) ;
68+ } ) ;
69+ } ) ;
0 commit comments