Skip to content

Commit 8a7c611

Browse files
committed
Fix #51
1 parent db423dd commit 8a7c611

File tree

6 files changed

+77
-92
lines changed

6 files changed

+77
-92
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "state-switch",
33
"private": true,
4-
"version": "1.9.0",
4+
"version": "1.9.1",
55
"description": "state-switch =================",
66
"scripts": {
77
"build": "rollup -c",

src/main.ts

Lines changed: 28 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import pjson from "../package.json";
88

99
class 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 {

state-switch.js

Lines changed: 9 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
HASS_USERNAME=dev
22
HASS_PASSWORD=dev
33
LOVELACE_LOCAL_FILES=state-switch.js
4-
LOVELACE_PLUGINS=thomasloven/lovelace-card-mod
4+
LOVELACE_PLUGINS=thomasloven/lovelace-card-mod thomasloven/lovelace-auto-entities

test/lovelace.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,32 @@ views:
77
- type: entities
88
entities:
99
- light.bed_light
10+
- light.kitchen_lights
11+
- type: horizontal-stack
12+
cards:
13+
- type: button
14+
icon: mdi:numeric-1
15+
tap_action:
16+
action: navigate
17+
navigation_path: "#1"
18+
- type: button
19+
icon: mdi:numeric-2
20+
tap_action:
21+
action: navigate
22+
navigation_path: "#2"
23+
- type: custom:state-switch
24+
entity: hash
25+
transition: flip
26+
states:
27+
"1":
28+
type: entities
29+
entities:
30+
- light.bed_light
31+
"2":
32+
type: entities
33+
entities:
34+
- light.kitchen_lights
35+
1036
- type: custom:state-switch
1137
entity: light.bed_light
1238
states:

test/views/1-standard.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,15 @@ cards:
104104
default:
105105
type: markdown
106106
content: "# DEFAULT"
107+
- type: custom:state-switch
108+
entity: mediaquery
109+
states:
110+
"(min-width: 1000px)":
111+
type: markdown
112+
content: more than 1000 px
113+
"(min-width: 500px)":
114+
type: markdown
115+
content: 500 to 1000 px
116+
all:
117+
type: markdown
118+
content: Really small

0 commit comments

Comments
 (0)