Skip to content

Commit

Permalink
Support fire dom action tap action
Browse files Browse the repository at this point in the history
  • Loading branch information
yosilevy committed May 6, 2022
1 parent 24dffd9 commit 9368010
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
11 changes: 11 additions & 0 deletions src/hass/fireEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const fireEvent = (node, type, detail) => {
detail = detail === null || detail === undefined ? {} : detail;
const event = new Event(type, {
bubbles: true,
cancelable: false,
composed: true
});
event.detail = detail;
node.dispatchEvent(event);
return event;
};
4 changes: 4 additions & 0 deletions src/hass/handle-click.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { HassUtils } from "./hass-utils";
import { fireEvent } from "./fireEvent.js";

export const handleClick = (
node: HTMLElement,
Expand Down Expand Up @@ -50,5 +51,8 @@ export const handleClick = (
const [domain, service] = actionConfig.service.split(".", 2);
hass.callService(domain, service, actionConfig.service_data);
}
case "fire-dom-event": {
fireEvent(node, "ll-custom", actionConfig);
}
}
};
24 changes: 13 additions & 11 deletions src/hass/hass-utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { fireEvent } from "./fireEvent.js";

/*
Home Assistant utility class
Inspired by Thomas Loven's card-tools (no-license)
Expand All @@ -15,10 +17,10 @@ export class HassUtils {
return this.LitElement.prototype.css;
}

static callService(hass, domain, service, entity_id, inOptions) {
static callService(hass, domain, service, entityId, inOptions) {
hass.callService(domain, service, {
entity_id: entity_id,
...inOptions,
entity_id: entityId,
...inOptions
});
}

Expand Down Expand Up @@ -51,8 +53,8 @@ export class HassUtils {
return entityId.substr(0, entityId.indexOf("."));
}

static popUp(hass, title, content, large = false) {
let popup = document.createElement("div");
static popUp(hass, title, content) {
const popup = document.createElement("div");
popup.innerHTML = `
<style>
app-toolbar {
Expand All @@ -72,15 +74,15 @@ export class HassUtils {
`;
popup.appendChild(content);
this.moreInfo(Object.keys(hass.states)[0]);
let moreInfo = document.querySelector("home-assistant")._moreInfoEl;
const moreInfo = document.querySelector("home-assistant")._moreInfoEl;
moreInfo._page = "none";
moreInfo.shadowRoot.appendChild(popup);
// moreInfo.large = large;
moreInfo.style.width = "570px";
document.querySelector("home-assistant").provideHass(content);

setTimeout(() => {
let interval = setInterval(() => {
const interval = setInterval(() => {
if (moreInfo.getAttribute("aria-hidden")) {
popup.parentNode.removeChild(popup);
clearInterval(interval);
Expand All @@ -91,7 +93,7 @@ export class HassUtils {
}

static closePopUp() {
let moreInfo = document.querySelector("home-assistant")._moreInfoEl;
const moreInfo = document.querySelector("home-assistant")._moreInfoEl;
if (moreInfo) moreInfo.close();
}

Expand All @@ -103,7 +105,7 @@ export class HassUtils {
ev = new Event(ev, {
bubbles: true,
cancelable: false,
composed: true,
composed: true
});
ev.detail = detail || {};
if (entity) {
Expand Down Expand Up @@ -133,9 +135,9 @@ export class HassUtils {
history.pushState(null, "", path);
}
fireEvent(window, "location-changed", {
replace,
replace
});
}
}

export const STATES_OFF = ["closed", "locked", "off"];
export const STATES_OFF = ["closed", "locked", "off"];
4 changes: 2 additions & 2 deletions src/text-action-element.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HassUtils } from "./hass/hass-utils";
import { HassUtils } from "./hass/hass-utils.js";
import { handleClick } from "./hass/handle-click.ts";

class HuiTextActionElement extends HassUtils.LitElement {
Expand All @@ -10,7 +10,7 @@ class HuiTextActionElement extends HassUtils.LitElement {
static get properties() {
return {
hass: {},
config: {},
config: {}
};
}

Expand Down

0 comments on commit 9368010

Please sign in to comment.