Skip to content

Commit

Permalink
Add theme watcher. Fix #66
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasloven committed Mar 2, 2021
1 parent eedcb74 commit 3343450
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
4 changes: 2 additions & 2 deletions card-mod.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "card-mod",
"private": true,
"version": "3.0.6",
"version": "3.0.7",
"description": "",
"scripts": {
"build": "rollup -c",
Expand Down
6 changes: 3 additions & 3 deletions src/card-mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export class CardMod extends LitElement {

constructor() {
super();
document
.querySelector("home-assistant")
.addEventListener("settheme", () => this.refresh());
document.addEventListener("cm_update", () => {
this.refresh();
});
}

connectedCallback() {
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ import "./patch/hui-card-element-editor";
import "./patch/hui-picture-elements-card";
import "./patch/ha-icon";
import "./mod-card";
import "./theme-watcher";
fireEvent("ll-rebuild", {});
24 changes: 24 additions & 0 deletions src/theme-watcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { hass } from "card-tools/src/hass";

function refresh_theme() {
document.dispatchEvent(new Event("cm_update"));
}

const bases = [
customElements.whenDefined("home-assistant"),
customElements.whenDefined("hc-main"),
];
Promise.race(bases).then(() => {
window.setTimeout(() => {
hass().connection.subscribeEvents(() => {
window.setTimeout(refresh_theme, 500);
});
document
.querySelector("home-assistant")
?.addEventListener("settheme", refresh_theme);

document
.querySelector("hc-main")
?.addEventListener("settheme", refresh_theme);
}, 1000);
});

0 comments on commit 3343450

Please sign in to comment.