@@ -4,30 +4,61 @@ import { addMarginForChips, entitiesThatShouldBeChips } from '@/helpers';
4
4
import { Task } from '@lit/task' ;
5
5
import type { Config } from '@type/config' ;
6
6
import type { HomeAssistant } from '@type/homeassistant' ;
7
- import { CSSResult , html , LitElement } from 'lit' ;
7
+ import { CSSResult , html , LitElement , type TemplateResult } from 'lit' ;
8
8
import { state } from 'lit/decorators.js' ;
9
9
import { version } from '../package.json' ;
10
10
import { chipStyles , styles } from './styles' ;
11
11
const equal = require ( 'fast-deep-equal' ) ;
12
12
13
+ /**
14
+ * Declaration for the loadCardHelpers function provided by Home Assistant
15
+ * Used to create and configure card elements
16
+ */
13
17
declare function loadCardHelpers ( ) : Promise < any > ;
14
18
19
+ /**
20
+ * Main component class for the Toolbar Status Chips
21
+ * Displays entity statuses as chips in the Home Assistant toolbar
22
+ */
15
23
export default class ToolbarStatusChips extends LitElement {
24
+ /**
25
+ * Card configuration object
26
+ * @state Marks this as a reactive property that will trigger updates
27
+ */
16
28
@state ( )
17
29
private _config ! : Config ;
18
30
31
+ /**
32
+ * Collection of entities to be displayed as chips
33
+ * @state Marks this as a reactive property that will trigger updates
34
+ */
19
35
@state ( )
20
36
private _entities ! : ChipEntity [ ] ;
21
37
38
+ /**
39
+ * Current URL slug extracted from the document URL
40
+ * Used for automatic area matching when no area is explicitly configured
41
+ * @state Marks this as a reactive property that will trigger updates
42
+ */
22
43
@state ( )
23
44
private _slug : string | undefined ;
24
45
25
- // not state
46
+ /**
47
+ * Reference to the current Home Assistant instance
48
+ * Not marked as @state as it's handled differently
49
+ */
26
50
private _hass ! : HomeAssistant ;
27
51
28
- // for editor
52
+ /**
53
+ * Flag indicating whether the card is in edit mode
54
+ * Used by the Home Assistant card editor
55
+ */
29
56
public editMode : boolean = false ;
30
57
58
+ /**
59
+ * Creates an instance of ToolbarStatusChips
60
+ * Extracts the current URL slug and logs version information
61
+ */
31
62
constructor ( ) {
32
63
super ( ) ;
33
64
this . _slug = document ?. URL ?. split ( '?' ) [ 0 ]
@@ -41,7 +72,12 @@ export default class ToolbarStatusChips extends LitElement {
41
72
) ;
42
73
}
43
74
44
- override render ( ) {
75
+ /**
76
+ * Renders the lit element card
77
+ * Displays chips based on the current entities if available
78
+ * @returns {TemplateResult } The rendered HTML template
79
+ */
80
+ override render ( ) : TemplateResult {
45
81
const styles = chipStyles ( this . isEditing ) ;
46
82
return this . _entities . length
47
83
? this . _createChipsTask . render ( {
@@ -54,35 +90,68 @@ export default class ToolbarStatusChips extends LitElement {
54
90
: html `` ;
55
91
}
56
92
57
- // styles to position the status chips at the top of on the toolbar
93
+ /**
94
+ * Defines the CSS styles for the component
95
+ * Positions the status chips at the top of the toolbar
96
+ * @returns {CSSResult } The component styles
97
+ */
58
98
static override get styles ( ) : CSSResult {
59
99
return styles ;
60
100
}
61
101
62
- // config property getters
102
+ /**
103
+ * Gets the additional label from configuration
104
+ * Used to filter entities by an additional label
105
+ * @returns {string | undefined } The configured additional label
106
+ */
63
107
get additionalLabel ( ) {
64
108
return this . _config . additional_label ;
65
109
}
66
110
111
+ /**
112
+ * Gets the area ID to filter entities by
113
+ * Falls back to the URL slug if no area is configured
114
+ * @returns {string | undefined } The area ID to use for filtering
115
+ */
67
116
get area ( ) {
68
117
return this . _config . area || this . _slug ;
69
118
}
70
119
120
+ /**
121
+ * Determines if optional entities should be shown
122
+ * True if explicitly configured or if viewing the status path
123
+ * @returns {boolean } Whether optional entities should be shown
124
+ */
71
125
get optional ( ) {
72
126
return (
73
127
this . _config . features ?. includes ( 'optional' ) ||
74
128
this . area === this . statusPath
75
129
) ;
76
130
}
77
131
132
+ /**
133
+ * Gets the solo label from configuration
134
+ * When set, only entities with this label are shown, ignoring area filtering
135
+ * @returns {string | undefined } The configured solo label
136
+ */
78
137
get soloLabel ( ) {
79
138
return this . _config . solo_label ;
80
139
}
81
140
141
+ /**
142
+ * Gets the status path from configuration
143
+ * Defaults to 'home' if not configured
144
+ * @returns {string } The path identifier for the status/home view
145
+ */
82
146
get statusPath ( ) {
83
147
return this . _config . status_path || 'home' ;
84
148
}
85
149
150
+ /**
151
+ * Determines if the card is currently in editing mode
152
+ * True if editMode is explicitly set or if parent has 'preview' class
153
+ * @returns {boolean } Whether the card is being edited
154
+ */
86
155
get isEditing ( ) : boolean {
87
156
return (
88
157
this . editMode ||
@@ -95,16 +164,22 @@ export default class ToolbarStatusChips extends LitElement {
95
164
* HASS setup
96
165
*/
97
166
98
- // The user supplied configuration. Throw an exception and Home Assistant
99
- // will render an error card.
167
+ /**
168
+ * Sets the card configuration
169
+ * Only updates if the new config is different from the current one
170
+ * @param {Config } config - The new card configuration
171
+ */
100
172
setConfig ( config : Config ) {
101
173
if ( ! equal ( config , this . _config ) ) {
102
174
this . _config = config ;
103
175
}
104
176
}
105
177
106
- // Whenever the state changes, a new `hass` object is set. Use this to
107
- // update your content.
178
+ /**
179
+ * Updates the card when Home Assistant state changes
180
+ * Filters entities based on labels and area, then creates chips for matching entities
181
+ * @param {HomeAssistant } hass - The new Home Assistant state
182
+ */
108
183
set hass ( hass : HomeAssistant ) {
109
184
// get entities with the status label
110
185
let entities = Object . values ( hass . entities ) . filter ( ( entity ) =>
@@ -132,7 +207,6 @@ export default class ToolbarStatusChips extends LitElement {
132
207
133
208
const chips = entitiesThatShouldBeChips ( entities , hass , this . optional ) ;
134
209
135
- // todo - don't set raw objects to properties of this...
136
210
// check if the entities have changed - update the card
137
211
if ( ! equal ( chips , this . _entities ) ) {
138
212
// no need to check states if entities have changed
@@ -142,11 +216,20 @@ export default class ToolbarStatusChips extends LitElement {
142
216
}
143
217
}
144
218
145
- // card configuration
219
+ /**
220
+ * Returns the configuration element for the card editor
221
+ * @returns {HTMLElement } The card editor element
222
+ */
146
223
static getConfigElement ( ) {
147
224
return document . createElement ( 'toolbar-status-chips-editor' ) ;
148
225
}
149
226
227
+ /**
228
+ * Generates a default configuration based on Home Assistant state
229
+ * Finds the area with the most status-labeled entities
230
+ * @param {HomeAssistant } hass - The Home Assistant state
231
+ * @returns {Promise<Config> } The generated configuration
232
+ */
150
233
public static async getStubConfig ( hass : HomeAssistant ) : Promise < Config > {
151
234
// Get all area IDs and their details
152
235
const areas = Object . entries ( hass . areas ) ;
@@ -191,7 +274,10 @@ export default class ToolbarStatusChips extends LitElement {
191
274
} ;
192
275
}
193
276
194
- // Task handles async work
277
+ /**
278
+ * Task that creates the chip cards asynchronously
279
+ * Uses the Home Assistant card helpers to create a horizontal stack of chips
280
+ */
195
281
_createChipsTask = new Task ( this , {
196
282
task : async ( ) => {
197
283
const helpers = await loadCardHelpers ( ) ;
0 commit comments