mirror of
https://github.com/home-assistant/frontend.git
synced 2025-11-05 17:09:48 +00:00
102 lines
2.9 KiB
TypeScript
102 lines
2.9 KiB
TypeScript
import type { PropertyValues } from "lit";
|
|
import { css, html, LitElement, nothing } from "lit";
|
|
import { customElement, property, state } from "lit/decorators";
|
|
import { computeStateName } from "../../../common/entity/compute_state_name";
|
|
import "../../../components/ha-textfield";
|
|
import { isUnavailableState, UNAVAILABLE } from "../../../data/entity";
|
|
import { setValue } from "../../../data/input_text";
|
|
import type { HomeAssistant } from "../../../types";
|
|
import { hasConfigOrEntityChanged } from "../common/has-changed";
|
|
import "../components/hui-generic-entity-row";
|
|
import { createEntityNotFoundWarning } from "../components/hui-warning";
|
|
import type { EntityConfig, LovelaceRow } from "./types";
|
|
|
|
@customElement("hui-input-text-entity-row")
|
|
class HuiInputTextEntityRow extends LitElement implements LovelaceRow {
|
|
@property({ attribute: false }) public hass?: HomeAssistant;
|
|
|
|
@state() private _config?: EntityConfig;
|
|
|
|
public setConfig(config: EntityConfig): void {
|
|
if (!config) {
|
|
throw new Error("Invalid configuration");
|
|
}
|
|
this._config = config;
|
|
}
|
|
|
|
protected shouldUpdate(changedProps: PropertyValues): boolean {
|
|
return hasConfigOrEntityChanged(this, changedProps);
|
|
}
|
|
|
|
protected render() {
|
|
if (!this._config || !this.hass) {
|
|
return nothing;
|
|
}
|
|
|
|
const stateObj = this.hass.states[this._config.entity];
|
|
|
|
if (!stateObj) {
|
|
return html`
|
|
<hui-warning .hass=${this.hass}>
|
|
${createEntityNotFoundWarning(this.hass, this._config.entity)}
|
|
</hui-warning>
|
|
`;
|
|
}
|
|
|
|
return html`
|
|
<hui-generic-entity-row
|
|
.hass=${this.hass}
|
|
.config=${this._config}
|
|
hide-name
|
|
>
|
|
<ha-textfield
|
|
.label=${this._config.name || computeStateName(stateObj)}
|
|
.disabled=${stateObj.state === UNAVAILABLE}
|
|
.value=${stateObj.state}
|
|
.minlength=${stateObj.attributes.min}
|
|
.maxlength=${stateObj.attributes.max}
|
|
.autoValidate=${stateObj.attributes.pattern}
|
|
.pattern=${stateObj.attributes.pattern}
|
|
.type=${stateObj.attributes.mode}
|
|
@change=${this._selectedValueChanged}
|
|
placeholder="(empty value)"
|
|
></ha-textfield>
|
|
</hui-generic-entity-row>
|
|
`;
|
|
}
|
|
|
|
private _selectedValueChanged(ev): void {
|
|
const stateObj = this.hass!.states[this._config!.entity];
|
|
|
|
const newValue = ev.target.value;
|
|
|
|
// Filter out invalid text states
|
|
if (newValue && isUnavailableState(newValue)) {
|
|
ev.target.value = stateObj.state;
|
|
return;
|
|
}
|
|
|
|
if (newValue !== stateObj.state) {
|
|
setValue(this.hass!, stateObj.entity_id, newValue);
|
|
}
|
|
|
|
ev.target.blur();
|
|
}
|
|
|
|
static styles = css`
|
|
hui-generic-entity-row {
|
|
display: flex;
|
|
align-items: center;
|
|
}
|
|
ha-textfield {
|
|
width: 100%;
|
|
}
|
|
`;
|
|
}
|
|
|
|
declare global {
|
|
interface HTMLElementTagNameMap {
|
|
"hui-input-text-entity-row": HuiInputTextEntityRow;
|
|
}
|
|
}
|