import {
html,
LitElement,
TemplateResult,
property,
customElement,
PropertyValues,
} from "lit-element";
import { PaperInputElement } from "@polymer/paper-input/paper-input";
import "../components/hui-generic-entity-row";
import "../components/hui-warning";
import { HomeAssistant } from "../../../types";
import { LovelaceRow, EntityConfig } from "./types";
import { setValue } from "../../../data/input_text";
import { hasConfigOrEntityChanged } from "../common/has-changed";
@customElement("hui-input-text-entity-row")
class HuiInputTextEntityRow extends LitElement implements LovelaceRow {
@property() public hass?: HomeAssistant;
@property() private _config?: EntityConfig;
public setConfig(config: EntityConfig): void {
if (!config) {
throw new Error("Configuration error");
}
this._config = config;
}
protected shouldUpdate(changedProps: PropertyValues): boolean {
return hasConfigOrEntityChanged(this, changedProps);
}
protected render(): TemplateResult {
if (!this._config || !this.hass) {
return html``;
}
const stateObj = this.hass.states[this._config.entity];
if (!stateObj) {
return html`
${this.hass.localize(
"ui.panel.lovelace.warning.entity_not_found",
"entity",
this._config.entity
)}
`;
}
return html`
`;
}
private get _inputEl(): PaperInputElement {
return this.shadowRoot!.querySelector("paper-input") as PaperInputElement;
}
private _selectedValueChanged(ev): void {
const element = this._inputEl;
const stateObj = this.hass!.states[this._config!.entity];
if (element.value !== stateObj.state) {
setValue(this.hass!, stateObj.entity_id, element.value!);
}
ev.target.blur();
}
}
declare global {
interface HTMLElementTagNameMap {
"hui-input-text-entity-row": HuiInputTextEntityRow;
}
}