import "@material/mwc-button/mwc-button"; import "@polymer/paper-dropdown-menu/paper-dropdown-menu-light"; import "@polymer/paper-input/paper-textarea"; import { HassEntity } from "home-assistant-js-websocket"; import { css, CSSResult, customElement, internalProperty, LitElement, property, } from "lit-element"; import { html } from "lit-html"; import { fireEvent } from "../../../common/dom/fire_event"; import "../../../components/entity/ha-entity-toggle"; import "../../../components/ha-blueprint-picker"; import "../../../components/ha-card"; import "../../../components/ha-circular-progress"; import "../../../components/ha-markdown"; import "../../../components/ha-selector/ha-selector"; import "../../../components/ha-settings-row"; import { BlueprintAutomationConfig, triggerAutomationActions, } from "../../../data/automation"; import { BlueprintOrError, Blueprints, fetchBlueprints, } from "../../../data/blueprint"; import { haStyle } from "../../../resources/styles"; import { HomeAssistant } from "../../../types"; import "../ha-config-section"; @customElement("blueprint-automation-editor") export class HaBlueprintAutomationEditor extends LitElement { @property({ attribute: false }) public hass!: HomeAssistant; @property() public isWide!: boolean; @property({ reflect: true, type: Boolean }) public narrow!: boolean; @property() public config!: BlueprintAutomationConfig; @property() public stateObj?: HassEntity; @internalProperty() private _blueprints?: Blueprints; protected firstUpdated(changedProps) { super.firstUpdated(changedProps); this._getBlueprints(); } private get _blueprint(): BlueprintOrError | undefined { if (!this._blueprints) { return undefined; } return this._blueprints[this.config.use_blueprint.path]; } protected render() { const blueprint = this._blueprint; return html` ${!this.narrow ? html` ${this.config.alias} ` : ""} ${this.hass.localize( "ui.panel.config.automation.editor.introduction" )}
${this.stateObj ? html`
${this.hass.localize( "ui.panel.config.automation.editor.enable_disable" )}
${this.hass.localize("ui.card.automation.trigger")}
` : ""}
${this.hass.localize( "ui.panel.config.automation.editor.blueprint.header" )}
${this._blueprints ? Object.keys(this._blueprints).length ? html` ` : this.hass.localize( "ui.panel.config.automation.editor.blueprint.no_blueprints" ) : html``}
${this.config.use_blueprint.path ? blueprint && "error" in blueprint ? html`

There is an error in this Blueprint: ${blueprint.error}

` : html`${blueprint?.metadata.description ? html`` : ""} ${blueprint?.metadata?.input && Object.keys(blueprint.metadata.input).length ? Object.entries(blueprint.metadata.input).map( ([key, value]) => html` ${value?.name || key} ${value?.description} ${value?.selector ? html`` : html``} ` ) : html`

${this.hass.localize( "ui.panel.config.automation.editor.blueprint.no_inputs" )}

`}` : ""}
`; } private async _getBlueprints() { this._blueprints = await fetchBlueprints(this.hass, "automation"); } private _runActions(ev: Event) { triggerAutomationActions(this.hass, (ev.target as any).stateObj.entity_id); } private _blueprintChanged(ev) { ev.stopPropagation(); if (this.config.use_blueprint.path === ev.detail.value) { return; } fireEvent(this, "value-changed", { value: { ...this.config!, use_blueprint: { path: ev.detail.value, }, }, }); } private _inputChanged(ev) { ev.stopPropagation(); const target = ev.target as any; const key = target.key; const value = ev.detail.value; if ( (this.config.use_blueprint.input && this.config.use_blueprint.input[key] === value) || (!this.config.use_blueprint.input && value === "") ) { return; } const input = { ...this.config.use_blueprint.input, [key]: value }; if (value === "" || value === undefined) { delete input[key]; } fireEvent(this, "value-changed", { value: { ...this.config!, use_blueprint: { ...this.config.use_blueprint, input, }, }, }); } private _valueChanged(ev: CustomEvent) { ev.stopPropagation(); const target = ev.target as any; const name = target.name; if (!name) { return; } const newVal = ev.detail.value; if ((this.config![name] || "") === newVal) { return; } fireEvent(this, "value-changed", { value: { ...this.config!, [name]: newVal }, }); } static get styles(): CSSResult[] { return [ haStyle, css` .padding { padding: 16px; } .blueprint-picker-container { padding: 16px; } h3 { margin: 16px; } span[slot="introduction"] a { color: var(--primary-color); } p { margin-bottom: 0; } ha-entity-toggle { margin-right: 8px; } ha-settings-row { --paper-time-input-justify-content: flex-end; border-top: 1px solid var(--divider-color); } :host(:not([narrow])) ha-settings-row paper-input { width: 60%; } :host(:not([narrow])) ha-settings-row ha-selector { width: 60%; } `, ]; } } declare global { interface HTMLElementTagNameMap { "blueprint-automation-editor": HaBlueprintAutomationEditor; } }