33// ====================================================================
44
55import type { HomeAssistant } from '../types/homeassistant' ;
6- import type { LovelaceViewConfig , LovelaceSectionConfig } from '../types/lovelace' ;
6+ import type { LovelaceViewConfig , LovelaceSectionConfig , LovelaceCardConfig } from '../types/lovelace' ;
77import { Registry } from '../Registry' ;
88import { type BatteryStatus , type BatteryStatusGroup , buildBatteryStatusGroups , getBatteryStatusDisplay } from '../utils/battery-utils' ;
99import { localize } from '../utils/localize' ;
1010
1111type BatteryStatusKeys = BatteryStatus [ ] ;
12+ type BatteryStatusKeyList = BatteryStatusKeys [ ] ;
1213
1314/**
1415 * Smart Grid Layout:
1516 * Groups battery status groups into 1-3 grid sections based on their entity counts, to optimize visual balance and minimize empty space.
1617 * - If all groups have very few entities, they are combined into a single section.
1718 * - If one group has significantly more entities than the others, it gets its own section, while smaller groups are combined.
1819 */
19- function smartGridGrouping ( batteryGroups : Record < BatteryStatus , BatteryStatusGroup > ) : BatteryStatusKeys [ ] {
20+ function smartGridGrouping ( batteryGroups : Record < BatteryStatus , BatteryStatusGroup > ) : BatteryStatusKeyList {
2021
2122 // Determine the maximum entity count among groups with entities (ignoring empty groups)
2223 const maxEntities = [ ...Object . values ( batteryGroups ) ]
2324 . filter ( ( group ) => group . entities . length > 0 )
2425 . reduce ( ( max , group ) => Math . max ( max , group . entities . length ) , 0 ) ;
2526
26- // Loop through each group and assign to grid groups based on count relative to maxEntities
27+ // Loop through each group and assign it to a grid group based on its count relative to maxEntities
2728 let numEntities = 0 ;
28- let groupKeys : BatteryStatusKeys [ ] = [ ] ;
29+ let statusKeyList : BatteryStatusKeyList = [ ] ;
2930 for ( const [ key , group ] of Object . entries ( batteryGroups ) as Array < [ BatteryStatus , BatteryStatusGroup ] > ) {
30- numEntities += group . entities . length ;
31- if ( numEntities === 0 ) continue ;
31+ if ( group . entities . length === 0 ) continue ;
3232
3333 // If adding this group would exceed 75% of maxEntities, start a new grid group
34- if ( groupKeys . length !== 0 && ( numEntities <= maxEntities * 0.75 ) ) {
35- groupKeys [ groupKeys . length - 1 ] . push ( key ) ;
34+ numEntities += group . entities . length ;
35+ if ( statusKeyList . length !== 0 && ( numEntities <= maxEntities * 0.75 ) ) {
36+ statusKeyList [ statusKeyList . length - 1 ] . push ( key ) ;
3637 } else {
37- groupKeys . push ( [ key ] ) ;
38+ statusKeyList . push ( [ key ] ) ;
3839 }
3940 }
4041
41- // If we still ended up still with 4 groups, merge the first and last two groups to a 2-column grid
42- if ( groupKeys . length === 4 ) {
43- return [ [ groupKeys [ 0 ] [ 0 ] , groupKeys [ 1 ] [ 0 ] ] , [ groupKeys [ 2 ] [ 0 ] , groupKeys [ 3 ] [ 0 ] ] ] ;
42+ // If we still end up with 4 groups, merge them into a 2-column grid
43+ if ( statusKeyList . length === 4 ) {
44+ return [ [ statusKeyList [ 0 ] [ 0 ] , statusKeyList [ 1 ] [ 0 ] ] , [ statusKeyList [ 2 ] [ 0 ] , statusKeyList [ 3 ] [ 0 ] ] ] ;
4445 }
45- return groupKeys ;
46+ return statusKeyList ;
4647}
4748
4849class Simon42ViewBatteriesStrategy extends HTMLElement {
@@ -52,19 +53,17 @@ class Simon42ViewBatteriesStrategy extends HTMLElement {
5253
5354 const strategyConfig = config . config || { } ;
5455 const batteryGroups = buildBatteryStatusGroups ( hass , strategyConfig ) ;
55- const batteryGroupStyles = getBatteryStatusDisplay ( strategyConfig ) ;
5656
5757 const sections : LovelaceSectionConfig [ ] = [ ] ;
5858
5959 // Build sections based on grid groups
60- for ( const gridGroupKeys of smartGridGrouping ( batteryGroups ) ) {
61- const cards : any [ ] = [ ] ;
60+ for ( const statusKeyList of smartGridGrouping ( batteryGroups ) ) {
61+ const cards : LovelaceCardConfig [ ] = [ ] ;
6262
6363 // Build sections based on grid groups
64- for ( const key of gridGroupKeys ) {
64+ for ( const status of statusKeyList ) {
6565
66- const entities = batteryGroups [ key ] . entities ;
67- if ( ! entities || entities . length === 0 ) continue ;
66+ const entities = batteryGroups [ status ] ! . entities ;
6867
6968 entities . sort ( ( a : string , b : string ) => {
7069 const valA = parseFloat ( hass . states [ a ] ?. state ) ;
@@ -74,11 +73,11 @@ class Simon42ViewBatteriesStrategy extends HTMLElement {
7473 return valA - valB ;
7574 } ) ;
7675
77- const style = batteryGroupStyles [ key ] ;
76+ const style = getBatteryStatusDisplay ( strategyConfig , status ) ;
7877 const oneOrMany = entities . length === 1 ? 'battery_one' : 'battery_many' ;
7978 cards . push ( {
8079 type : 'heading' ,
81- heading : `${ localize ( 'batteries.' + key ) } ` + ( style . info ? `(${ style . info } )` : '' ) +
80+ heading : `${ localize ( 'batteries.' + status ) } ` + ( style . info ? `(${ style . info } )` : '' ) +
8281 ` - ${ entities . length } ${ localize ( 'batteries.' + oneOrMany ) } ` ,
8382 heading_style : 'title' ,
8483 icon : style . icon ,
0 commit comments