Compare commits

...

3 Commits

Author SHA1 Message Date
Zack Arnett
39200b62d5 Fix custom vlaue 2020-11-17 13:22:28 -06:00
Zack Arnett
0eb28ea733 comment 2020-11-17 11:58:32 -06:00
Zack Arnett
62d471888f Only allow selectable attributes in dropdown 2020-11-15 16:18:58 -06:00
2 changed files with 53 additions and 6 deletions

View File

@@ -1,3 +1,4 @@
import "@material/mwc-icon-button/mwc-icon-button";
import { mdiClose, mdiMenuDown, mdiMenuUp } from "@mdi/js";
import "@polymer/paper-input/paper-input";
import "@polymer/paper-item/paper-item";
@@ -14,13 +15,14 @@ import {
query,
TemplateResult,
} from "lit-element";
import memoizeOne from "memoize-one";
import { fireEvent } from "../../common/dom/fire_event";
import { computeDomain } from "../../common/entity/compute_domain";
import { PolymerChangedEvent } from "../../polymer-types";
import { HomeAssistant } from "../../types";
import { formatAttributeName } from "../../util/hass-attributes-util";
import "../ha-svg-icon";
import "./state-badge";
import { formatAttributeName } from "../../util/hass-attributes-util";
import "@material/mwc-icon-button/mwc-icon-button";
export type HaEntityPickerEntityFilterFunc = (entityId: HassEntity) => boolean;
@@ -41,6 +43,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")
class HaEntityAttributePicker extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;
@@ -68,9 +105,8 @@ class HaEntityAttributePicker extends LitElement {
protected updated(changedProps: PropertyValues) {
if (changedProps.has("_opened") && this._opened) {
const state = this.entityId ? this.hass.states[this.entityId] : undefined;
(this._comboBox as any).items = state
? Object.keys(state.attributes)
(this._comboBox as any).items = this.entityId
? this._selectableAttributes(this.entityId)
: [];
}
}
@@ -85,7 +121,6 @@ class HaEntityAttributePicker extends LitElement {
.value=${this._value}
.allowCustomValue=${this.allowCustomValue}
.renderer=${rowRenderer}
attr-for-value="bind-value"
@opened-changed=${this._openedChanged}
@value-changed=${this._valueChanged}
>
@@ -137,6 +172,17 @@ 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[computeDomain(entity)].includes(attr)
);
});
private _clearValue(ev: Event) {
ev.stopPropagation();
this._setValue("");

View File

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