@@ -84,47 +84,17 @@ export class EntityTable extends LitElement implements RestAction {
84
84
"Diagnostic" ,
85
85
] ;
86
86
87
+ private _unknown_state_events : { [ key : string ] : number } = { } ;
88
+
87
89
connectedCallback ( ) {
88
90
super . connectedCallback ( ) ;
89
- window . source ?. addEventListener ( "state" , ( e : Event ) => {
91
+
92
+ window . source ?. addEventListener ( 'state' , ( e : Event ) => {
90
93
const messageEvent = e as MessageEvent ;
91
94
const data = JSON . parse ( messageEvent . data ) ;
92
95
let idx = this . entities . findIndex ( ( x ) => x . unique_id === data . id ) ;
93
- if ( idx === - 1 && data . id ) {
94
- // Dynamically add discovered..
95
- let parts = data . id . split ( "-" ) ;
96
- let entity = {
97
- ...data ,
98
- domain : parts [ 0 ] ,
99
- unique_id : data . id ,
100
- id : parts . slice ( 1 ) . join ( "-" ) ,
101
- entity_category : data . entity_category ,
102
- sorting_group : data . sorting_group ?? ( EntityTable . ENTITY_CATEGORIES [ parseInt ( data . entity_category ) ] || EntityTable . ENTITY_UNDEFINED ) ,
103
- value_numeric_history : [ data . value ] ,
104
- } as entityConfig ;
105
- entity . has_action = this . hasAction ( entity ) ;
106
- if ( entity . has_action ) {
107
- this . has_controls = true ;
108
- }
109
- this . entities . push ( entity ) ;
110
- this . entities . sort ( ( a , b ) => {
111
- const sortA = a . sorting_weight ?? a . name ;
112
- const sortB = b . sorting_weight ?? b . name ;
113
- return a . sorting_group < b . sorting_group
114
- ? - 1
115
- : a . sorting_group === b . sorting_group
116
- ? sortA === sortB
117
- ? a . name . toLowerCase ( ) < b . name . toLowerCase ( )
118
- ? - 1
119
- : 1
120
- : sortA < sortB
121
- ? - 1
122
- : 1
123
- : 1
124
- } ) ;
125
- this . requestUpdate ( ) ;
126
- } else {
127
- if ( typeof data . value === "number" ) {
96
+ if ( idx != - 1 && data . id ) {
97
+ if ( typeof data . value === 'number' ) {
128
98
let history = [ ...this . entities [ idx ] . value_numeric_history ] ;
129
99
history . push ( data . value ) ;
130
100
this . entities [ idx ] . value_numeric_history = history . splice ( - 50 ) ;
@@ -135,6 +105,44 @@ export class EntityTable extends LitElement implements RestAction {
135
105
delete data . unique_id ;
136
106
Object . assign ( this . entities [ idx ] , data ) ;
137
107
this . requestUpdate ( ) ;
108
+ } else {
109
+ // is it a `detail_all` event already?
110
+ if ( data ?. name ) {
111
+ this . addEntity ( data ) ;
112
+ } else {
113
+ if ( this . _unknown_state_events [ data . id ] ) {
114
+ this . _unknown_state_events [ data . id ] ++ ;
115
+ } else {
116
+ this . _unknown_state_events [ data . id ] = 1 ;
117
+ }
118
+ // ignore the first few events, maybe the esp will send a detail_all
119
+ // event soon
120
+ if ( this . _unknown_state_events [ data . id ] < 1 ) {
121
+ return ;
122
+ }
123
+
124
+ let parts = data . id . split ( '-' ) ;
125
+ let domain = parts [ 0 ] ;
126
+ let id = parts . slice ( 1 ) . join ( '-' ) ;
127
+
128
+ fetch ( `${ this . _basePath } /${ domain } /${ id } ?detail=all` , {
129
+ method : 'GET' ,
130
+ } )
131
+ . then ( ( r ) => {
132
+ console . log ( r ) ;
133
+ if ( ! r . ok ) {
134
+ throw new Error ( `HTTP error! Status: ${ r . status } ` ) ;
135
+ }
136
+ return r . json ( ) ;
137
+ } )
138
+ . then ( ( data ) => {
139
+ console . log ( data ) ;
140
+ this . addEntity ( data ) ;
141
+ } )
142
+ . catch ( ( error ) => {
143
+ console . error ( 'Fetch error:' , error ) ;
144
+ } ) ;
145
+ }
138
146
}
139
147
} ) ;
140
148
@@ -166,6 +174,45 @@ export class EntityTable extends LitElement implements RestAction {
166
174
} ) ;
167
175
}
168
176
177
+ addEntity ( data : any ) {
178
+ let idx = this . entities . findIndex ( ( x ) => x . unique_id === data . id ) ;
179
+ if ( idx === - 1 && data . id ) {
180
+ // Dynamically add discovered..
181
+ let parts = data . id . split ( "-" ) ;
182
+ let entity = {
183
+ ...data ,
184
+ domain : parts [ 0 ] ,
185
+ unique_id : data . id ,
186
+ id : parts . slice ( 1 ) . join ( "-" ) ,
187
+ entity_category : data . entity_category ,
188
+ sorting_group : data . sorting_group ?? ( EntityTable . ENTITY_CATEGORIES [ parseInt ( data . entity_category ) ] || EntityTable . ENTITY_UNDEFINED ) ,
189
+ value_numeric_history : [ data . value ] ,
190
+ } as entityConfig ;
191
+ entity . has_action = this . hasAction ( entity ) ;
192
+ if ( entity . has_action ) {
193
+ this . has_controls = true ;
194
+ }
195
+ this . entities . push ( entity ) ;
196
+ this . entities . sort ( ( a , b ) => {
197
+ const sortA = a . sorting_weight ?? a . name ;
198
+ const sortB = b . sorting_weight ?? b . name ;
199
+ return a . sorting_group < b . sorting_group
200
+ ? - 1
201
+ : a . sorting_group === b . sorting_group
202
+ ? sortA === sortB
203
+ ? a . name . toLowerCase ( ) < b . name . toLowerCase ( )
204
+ ? - 1
205
+ : 1
206
+ : sortA < sortB
207
+ ? - 1
208
+ : 1
209
+ : 1
210
+ } ) ;
211
+ this . requestUpdate ( ) ;
212
+ }
213
+
214
+ }
215
+
169
216
hasAction ( entity : entityConfig ) : boolean {
170
217
return `render_${ entity . domain } ` in this . _actionRenderer ;
171
218
}
@@ -181,7 +228,9 @@ export class EntityTable extends LitElement implements RestAction {
181
228
restAction ( entity : entityConfig , action : string ) {
182
229
fetch ( `${ this . _basePath } /${ entity . domain } /${ entity . id } /${ action } ` , {
183
230
method : "POST" ,
184
- body : "true" ,
231
+ headers :{
232
+ 'Content-Type' : 'application/x-www-form-urlencoded'
233
+ } ,
185
234
} ) . then ( ( r ) => {
186
235
console . log ( r ) ;
187
236
} ) ;
@@ -323,11 +372,12 @@ class ActionRenderer {
323
372
return this [ method ] ( ) ;
324
373
}
325
374
326
- private _actionButton ( entity : entityConfig , label : string , action : string ) {
375
+ private _actionButton ( entity : entityConfig , label : string , action : string , isCurrentState : boolean = false ) {
327
376
if ( ! entity ) return ;
328
377
let a = action || label . toLowerCase ( ) ;
329
378
return html `< button
330
- class ="abutton "
379
+ class ="${ isCurrentState ? 'abuttonIsState' : 'abutton' } "
380
+ ?disabled =${ isCurrentState }
331
381
@click =${ ( ) => this . actioner ?. restAction ( entity , a ) }
332
382
>
333
383
${ label }
@@ -607,16 +657,16 @@ class ActionRenderer {
607
657
608
658
render_lock ( ) {
609
659
if ( ! this . entity ) return ;
610
- return html `${ this . _actionButton ( this . entity , "🔐" , "lock" ) }
611
- ${ this . _actionButton ( this . entity , "🔓" , "unlock" ) }
660
+ return html `${ this . _actionButton ( this . entity , "🔐" , "lock" , this . entity . state === "LOCKED" ) }
661
+ ${ this . _actionButton ( this . entity , "🔓" , "unlock" , this . entity . state === "UNLOCKED" ) }
612
662
${ this . _actionButton ( this . entity , "↑" , "open" ) } ` ;
613
663
}
614
664
615
665
render_cover ( ) {
616
666
if ( ! this . entity ) return ;
617
- return html `${ this . _actionButton ( this . entity , "↑" , "open" ) }
667
+ return html `${ this . _actionButton ( this . entity , "↑" , "open" , this . entity . state === "OPEN" ) }
618
668
${ this . _actionButton ( this . entity , "☐" , "stop" ) }
619
- ${ this . _actionButton ( this . entity , "↓" , "close" ) } ` ;
669
+ ${ this . _actionButton ( this . entity , "↓" , "close" , this . entity . state === "CLOSED" ) } ` ;
620
670
}
621
671
622
672
render_button ( ) {
@@ -735,4 +785,10 @@ class ActionRenderer {
735
785
</ div >
736
786
` ;
737
787
}
788
+ render_valve ( ) {
789
+ if ( ! this . entity ) return ;
790
+ return html `${ this . _actionButton ( this . entity , "OPEN" , "open" , this . entity . state === "OPEN" ) }
791
+ ${ this . _actionButton ( this . entity , "☐" , "stop" ) }
792
+ ${ this . _actionButton ( this . entity , "CLOSE" , "close" , this . entity . state === "CLOSED" ) } ` ;
793
+ }
738
794
}
0 commit comments