Only allow selectable attributes in dropdown

This commit is contained in:
Zack Arnett 2020-11-15 16:18:58 -06:00
parent bc5cb46e7d
commit 62d471888f
2 changed files with 54 additions and 5 deletions

View File

@ -1,3 +1,4 @@
import "@material/mwc-icon-button/mwc-icon-button";
import { mdiClose, mdiMenuDown, mdiMenuUp } from "@mdi/js"; import { mdiClose, mdiMenuDown, mdiMenuUp } from "@mdi/js";
import "@polymer/paper-input/paper-input"; import "@polymer/paper-input/paper-input";
import "@polymer/paper-item/paper-item"; import "@polymer/paper-item/paper-item";
@ -14,13 +15,13 @@ import {
query, query,
TemplateResult, TemplateResult,
} from "lit-element"; } from "lit-element";
import memoizeOne from "memoize-one";
import { fireEvent } from "../../common/dom/fire_event"; import { fireEvent } from "../../common/dom/fire_event";
import { PolymerChangedEvent } from "../../polymer-types"; import { PolymerChangedEvent } from "../../polymer-types";
import { HomeAssistant } from "../../types"; import { HomeAssistant } from "../../types";
import { formatAttributeName } from "../../util/hass-attributes-util";
import "../ha-svg-icon"; import "../ha-svg-icon";
import "./state-badge"; import "./state-badge";
import { formatAttributeName } from "../../util/hass-attributes-util";
import "@material/mwc-icon-button/mwc-icon-button";
export type HaEntityPickerEntityFilterFunc = (entityId: HassEntity) => boolean; export type HaEntityPickerEntityFilterFunc = (entityId: HassEntity) => boolean;
@ -41,6 +42,41 @@ const rowRenderer = (root: HTMLElement, _owner, model: { item: string }) => {
); );
}; };
const SELECTABLE_ATTRIBUTES: { [key: string]: string[] } = {
light: ["brightness"],
climate: [
"current_temperature",
"fan_mode",
"preset_mode",
"swing_mode",
"temperature",
"current_hundity",
"humidity",
"hvac_action",
],
fan: ["speed"],
air_quality: [
"nitrogen_oxide",
"particulate_matter_10",
"particulate_matter_2_5",
],
cover: ["current_position", "current_tilt_position"],
device_tracker: ["battery"],
humidifier: ["humidty"],
media_player: ["media_title"],
vacuum: ["battery_level", "status"],
water_heater: ["current_temperature", "temperature", "operation_mode"],
weather: [
"temperature",
"humidity",
"ozone",
"pressure",
"wind_bearing",
"wind_speed",
"visibility",
],
};
@customElement("ha-entity-attribute-picker") @customElement("ha-entity-attribute-picker")
class HaEntityAttributePicker extends LitElement { class HaEntityAttributePicker extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant; @property({ attribute: false }) public hass!: HomeAssistant;
@ -68,9 +104,8 @@ class HaEntityAttributePicker extends LitElement {
protected updated(changedProps: PropertyValues) { protected updated(changedProps: PropertyValues) {
if (changedProps.has("_opened") && this._opened) { if (changedProps.has("_opened") && this._opened) {
const state = this.entityId ? this.hass.states[this.entityId] : undefined; (this._comboBox as any).items = this.entityId
(this._comboBox as any).items = state ? this._selectableAttributes(this.entityId)
? Object.keys(state.attributes)
: []; : [];
} }
} }
@ -137,6 +172,19 @@ class HaEntityAttributePicker extends LitElement {
`; `;
} }
private _selectableAttributes = memoizeOne((entity: string) => {
const stateObj = this.hass.states[entity];
if (!stateObj) {
return [];
}
return Object.keys(stateObj.attributes).filter((attr) =>
SELECTABLE_ATTRIBUTES[entity.substring(0, entity.indexOf("."))].includes(
attr
)
);
});
private _clearValue(ev: Event) { private _clearValue(ev: Event) {
ev.stopPropagation(); ev.stopPropagation();
this._setValue(""); this._setValue("");

View File

@ -115,6 +115,7 @@ export class HuiEntityCardEditor extends LitElement
</div> </div>
<div class="side-by-side"> <div class="side-by-side">
<ha-entity-attribute-picker <ha-entity-attribute-picker
allow-custom-value
.hass=${this.hass} .hass=${this.hass}
.entityId=${this._entity} .entityId=${this._entity}
.label="${this.hass.localize( .label="${this.hass.localize(