UI Editor for media-control card (#2336)

*  UI Editor for `media-control` card

* Address review comments

* Note on LL interface
This commit is contained in:
Ian Richardson 2018-12-17 02:31:58 -06:00 committed by Paulus Schoutsen
parent cac7f8d1ab
commit d1a56d6acc
2 changed files with 97 additions and 0 deletions

View File

@ -2,7 +2,21 @@ import "../../../cards/ha-media_player-card";
import LegacyWrapperCard from "./hui-legacy-wrapper-card";
// should be interface when converted to TS
export const Config = {
entity: "",
};
class HuiMediaControlCard extends LegacyWrapperCard {
static async getConfigElement() {
await import("../editor/config-elements/hui-media-control-card-editor");
return document.createElement("hui-media-control-card-editor");
}
static getStubConfig() {
return {};
}
constructor() {
super("ha-media_player-card", "media_player");
}

View File

@ -0,0 +1,83 @@
import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element";
import { TemplateResult } from "lit-html";
import { struct } from "../../common/structs/struct";
import { EntitiesEditorEvent, EditorTarget } from "../types";
import { hassLocalizeLitMixin } from "../../../../mixins/lit-localize-mixin";
import { HomeAssistant } from "../../../../types";
import { LovelaceCardEditor } from "../../types";
import { fireEvent } from "../../../../common/dom/fire_event";
import { Config } from "../../cards/hui-media-control-card";
import "../../../../components/entity/ha-entity-picker";
const cardConfigStruct = struct({
type: "string",
entity: "string?",
});
export class HuiMediaControlCardEditor extends hassLocalizeLitMixin(LitElement)
implements LovelaceCardEditor {
public hass?: HomeAssistant;
private _config?: Config;
public setConfig(config: Config): void {
config = cardConfigStruct(config);
this._config = config;
}
static get properties(): PropertyDeclarations {
return { hass: {}, _config: {} };
}
get _entity(): string {
return this._config!.entity || "";
}
protected render(): TemplateResult {
if (!this.hass) {
return html``;
}
return html`
<div class="card-config">
<ha-entity-picker
.hass="${this.hass}"
.value="${this._entity}"
.configValue=${"entity"}
domain-filter="media_player"
@change="${this._valueChanged}"
allow-custom-entity
></ha-entity-picker>
</div>
`;
}
private _valueChanged(ev: EntitiesEditorEvent): void {
if (!this._config || !this.hass) {
return;
}
const target = ev.target! as EditorTarget;
if (this[`_${target.configValue}`] === target.value) {
return;
}
if (target.configValue) {
this._config = {
...this._config,
[target.configValue!]: target.value,
};
}
fireEvent(this, "config-changed", { config: this._config });
}
}
declare global {
interface HTMLElementTagNameMap {
"hui-media-control-card-editor": HuiMediaControlCardEditor;
}
}
customElements.define(
"hui-media-control-card-editor",
HuiMediaControlCardEditor
);