diff --git a/src/common/dom/dynamic-content-directive.ts b/src/common/dom/dynamic-content-directive.ts new file mode 100644 index 0000000000..31a8a1ead7 --- /dev/null +++ b/src/common/dom/dynamic-content-directive.ts @@ -0,0 +1,29 @@ +import { directive, Part, NodePart } from "lit-html"; + +export const dynamicContentDirective = directive( + (tag: string, properties: { [key: string]: any }) => (part: Part): void => { + if (!(part instanceof NodePart)) { + throw new Error( + "dynamicContentDirective can only be used in content bindings" + ); + } + + let element = part.value as HTMLElement | undefined; + + if ( + element !== undefined && + tag.toUpperCase() === (element as HTMLElement).tagName + ) { + Object.entries(properties).forEach(([key, value]) => { + element![key] = value; + }); + return; + } + + element = document.createElement(tag); + Object.entries(properties).forEach(([key, value]) => { + element![key] = value; + }); + part.setValue(element); + } +); diff --git a/src/components/device/ha-device-automation-picker.ts b/src/components/device/ha-device-automation-picker.ts index 98c5ce492a..34eacfaa25 100644 --- a/src/components/device/ha-device-automation-picker.ts +++ b/src/components/device/ha-device-automation-picker.ts @@ -176,6 +176,7 @@ export abstract class HaDeviceAutomationPicker< this.value = automation; setTimeout(() => { fireEvent(this, "change"); + fireEvent(this, "value-changed", { value: automation }); }, 0); } diff --git a/src/components/ha-yaml-editor.ts b/src/components/ha-yaml-editor.ts new file mode 100644 index 0000000000..dc3a0cb08e --- /dev/null +++ b/src/components/ha-yaml-editor.ts @@ -0,0 +1,81 @@ +import { safeDump, safeLoad } from "js-yaml"; +import "./ha-code-editor"; +import { LitElement, property, customElement, html } from "lit-element"; +import { fireEvent } from "../common/dom/fire_event"; + +const isEmpty = (obj: object) => { + for (const key in obj) { + if (obj.hasOwnProperty(key)) { + return false; + } + } + return true; +}; + +@customElement("ha-yaml-editor") +export class HaYamlEditor extends LitElement { + @property() public value?: any; + @property() public isValid = true; + @property() public label?: string; + @property() private _yaml?: string; + + protected firstUpdated() { + try { + this._yaml = + this.value && !isEmpty(this.value) ? safeDump(this.value) : ""; + } catch (err) { + alert(`There was an error converting to YAML: ${err}`); + } + } + + protected render() { + if (this._yaml === undefined) { + return; + } + return html` + ${this.label + ? html` +
${this.label}
+ ` + : ""} +