@@ -8,11 +8,12 @@ import pjson from "../package.json";
88
99class StateSwitch extends LitElement {
1010 @property ( ) _config : StateSwitchConfig ;
11- @property ( ) hass : HassObject ;
11+ @property ( ) _hass : HassObject ;
1212 @property ( ) state ;
1313 @property ( ) _tmpl ;
1414
1515 cards : Record < string , LovelaceCard > ;
16+ _mqs : MediaQueryList [ ] ;
1617
1718 async setConfig ( config ) {
1819 ( window as any ) . deviceID = deviceID ;
@@ -31,8 +32,9 @@ class StateSwitch extends LitElement {
3132 window . addEventListener ( "hashchange" , ( ) => this . updated ( new Map ( ) ) ) ;
3233 }
3334 if ( config . entity === "mediaquery" ) {
34- for ( const q in this . cards ) {
35- window . matchMedia ( q ) . addListener ( this . update_state . bind ( this ) ) ;
35+ for ( const q in config . states ) {
36+ const mq = window . matchMedia ( q ) ;
37+ mq . addEventListener ( "change" , ( ) => this . update_state ( ) ) ;
3638 }
3739 }
3840 if ( config . entity === "template" || hasTemplate ( config . entity ) ) {
@@ -50,6 +52,7 @@ class StateSwitch extends LitElement {
5052 connectedCallback ( ) {
5153 super . connectedCallback ( ) ;
5254 if ( ! this . _config ) return ;
55+ for ( const k in this . cards ) this . cards [ k ] . hass = this . _hass ;
5356 if (
5457 this . _config . entity === "template" ||
5558 hasTemplate ( this . _config . entity )
@@ -72,7 +75,7 @@ class StateSwitch extends LitElement {
7275 const helpers = await ( window as any ) . loadCardHelpers ( ) ;
7376 for ( let k in this . _config . states ) {
7477 this . cards [ k ] = await helpers . createCardElement ( this . _config . states [ k ] ) ;
75- this . cards [ k ] . hass = this . hass ;
78+ this . cards [ k ] . hass = this . _hass ;
7679 }
7780 this . update_state ( ) ;
7881 }
@@ -84,10 +87,10 @@ class StateSwitch extends LitElement {
8487 newstate = this . _tmpl ;
8588 break ;
8689 case "user" :
87- newstate = this . hass ?. user ?. name ;
90+ newstate = this . _hass ?. user ?. name ;
8891 break ;
8992 case "group" :
90- newstate = this . hass ?. user ?. is_admin ? "admin" : "user" ;
93+ newstate = this . _hass ?. user ?. is_admin ? "admin" : "user" ;
9194 break ;
9295 case "deviceID" :
9396 case "browser" :
@@ -105,28 +108,32 @@ class StateSwitch extends LitElement {
105108 }
106109 break ;
107110 default :
108- newstate = this . hass ?. states [ this . _config . entity ] ?. state ;
111+ newstate = this . _hass ?. states [ this . _config . entity ] ?. state ;
109112 }
110113
111114 if ( newstate === undefined || ! this . cards . hasOwnProperty ( newstate ) )
112115 newstate = this . _config . default ;
113116 this . state = newstate ;
114117 }
115118
116- updated ( changedProperties ) {
117- if ( changedProperties . has ( "hass" ) )
118- for ( let k in this . cards ) this . cards [ k ] . hass = this . hass ;
119+ set hass ( hass ) {
120+ this . _hass = hass ;
121+ for ( const k in this . cards ) this . cards [ k ] . hass = hass ;
122+ }
119123
124+ updated ( changedProperties ) {
120125 if ( ! changedProperties . has ( "state" ) ) {
121126 this . update_state ( ) ;
122127 } else {
123128 const oldState = changedProperties . get ( "state" ) ;
124129 if ( this . cards [ oldState ] ) {
125130 this . cards [ oldState ] . classList . remove ( "visible" ) ;
126- this . cards [ oldState ] . classList . add ( "out" ) ;
127- window . setTimeout ( ( ) => {
128- this . cards [ oldState ] . classList . remove ( "out" ) ;
129- } , this . _config . transition_time || 500 ) ;
131+ if ( this . _config . transition ) {
132+ this . cards [ oldState ] . classList . add ( "out" ) ;
133+ window . setTimeout ( ( ) => {
134+ this . cards [ oldState ] . classList . remove ( "out" ) ;
135+ } , this . _config . transition_time || 500 ) ;
136+ }
130137 }
131138 if ( this . cards [ this . state ] ) {
132139 this . cards [ this . state ] . classList . add ( "visible" ) ;
@@ -156,7 +163,7 @@ class StateSwitch extends LitElement {
156163
157164 async getCardSize ( ) {
158165 let sz = 1 ;
159- for ( let k in this . cards ) {
166+ for ( const k in this . cards ) {
160167 if ( this . cards [ k ] ?. getCardSize )
161168 sz = Math . max ( sz , await this . cards [ k ] . getCardSize ( ) ) ;
162169 }
@@ -174,23 +181,21 @@ class StateSwitch extends LitElement {
174181 # root {
175182 overflow : hidden;
176183 display : grid;
184+ grid-template-rows : auto 0px ;
177185 }
178186 # root * {
179- display : none;
180187 grid-column : 1 ;
181- grid-row : 1 ;
188+ grid-row : 2 ;
182189 }
183- # root .visible {
184- display : block;
190+ # root * .visible ,
191+ # root * .out {
192+ grid-row : 1 ;
185193 }
186194
187195 # root .slide-down * ,
188196 # root .slide-up * ,
189197 # root .slide-left * ,
190198 # root .slide-right * {
191- display : block;
192- opacity : 0 ;
193- height : 0 ;
194199 transition-property : transform;
195200 transition-timing-function : linear;
196201 transition-duration : inherit;
@@ -209,16 +214,9 @@ class StateSwitch extends LitElement {
209214 # root .slide-up .visible ,
210215 # root .slide-left .visible ,
211216 # root .slide-right .visible {
212- opacity : 1 ;
213- height : auto;
214217 transform : translate (0% );
215218 }
216- # root .slide-down .out ,
217- # root .slide-up .out ,
218- # root .slide-left .out ,
219- # root .slide-right .out {
220- opacity : 1 ;
221- height : auto;
219+ # root .slide-down .out {
222220 transform : translate (0 , 110% );
223221 }
224222 # root .slide-up .out {
@@ -235,9 +233,6 @@ class StateSwitch extends LitElement {
235233 # root .swap-up * ,
236234 # root .swap-left * ,
237235 # root .swap-right * {
238- display : block;
239- opacity : 0 ;
240- height : 0 ;
241236 transition-property : transform;
242237 transition-timing-function : linear;
243238 transition-duration : inherit;
@@ -256,33 +251,19 @@ class StateSwitch extends LitElement {
256251 # root .swap-up .visible ,
257252 # root .swap-left .visible ,
258253 # root .swap-right .visible {
259- opacity : 1 ;
260- height : auto;
261254 transition-delay : inherit;
262255 transform : translate (0% );
263256 }
264- # root .swap-down .out ,
265- # root .swap-up .out ,
266- # root .swap-left .out ,
267- # root .swap-right .out {
268- opacity : 1 ;
269- height : auto;
270- }
271257
272258 # root .flip ,
273259 # root .flip-x ,
274260 # root .flip-y {
275- xwidth : 100% ;
276- xheight : 100% ;
277261 position : relative;
278262 perspective : 1000px ;
279263 }
280264 # root .flip * ,
281265 # root .flip-x * ,
282266 # root .flip-y * {
283- display : block;
284- opacity : 0 ;
285- height : 0 ;
286267 transform : rotate3d (0 , 1 , 0 , -180deg );
287268 transition-property : transform;
288269 transition-timing-function : linear;
@@ -297,17 +278,13 @@ class StateSwitch extends LitElement {
297278 # root .flip .visible ,
298279 # root .flip-x .visible ,
299280 # root .flip-y .visible {
300- opacity : 1 ;
301- height : auto;
302281 backface-visibility : hidden;
303282 transform : rotate3d (0 , 0 , 0 , 0deg );
304283 }
305284 # root .flip .out ,
306285 # root .flip-x .out ,
307286 # root .flip-y .out {
308287 pointer-events : none;
309- opacity : 1 ;
310- height : auto;
311288 transform : rotate3d (0 , 1 , 0 , 180deg );
312289 }
313290 # root .flip-y .out {
0 commit comments