From c9e6264c105ec7f32d8e2d80bb656c664abc6690 Mon Sep 17 00:00:00 2001 From: jkocon <39369166+jkocon@users.noreply.github.com> Date: Mon, 8 Jul 2019 15:35:19 +0200 Subject: [PATCH] Create tv-card-editor.js --- tv-card-editor.js | 190 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 tv-card-editor.js diff --git a/tv-card-editor.js b/tv-card-editor.js new file mode 100644 index 0000000..ea21a80 --- /dev/null +++ b/tv-card-editor.js @@ -0,0 +1,190 @@ +if (!customElements.get("paper-input")) { + console.log("imported", "paper-input"); + import("https://unpkg.com/@polymer/paper-input/paper-input.js?module"); +} + +const fireEvent = (node, type, detail, options) => { + options = options || {}; + detail = detail === null || detail === undefined ? {} : detail; + const event = new Event(type, { + bubbles: options.bubbles === undefined ? true : options.bubbles, + cancelable: Boolean(options.cancelable), + composed: options.composed === undefined ? true : options.composed + }); + event.detail = detail; + node.dispatchEvent(event); + return event; +}; + +const LitElement = Object.getPrototypeOf( + customElements.get("ha-panel-lovelace") +); +const html = LitElement.prototype.html; + +export class TVCardEditor extends LitElement { + setConfig(config) { + this._config = config; + } + + static get properties() { + return { hass: {}, _config: {} }; + } + + get _name() { + return this._config.name || ""; + } + + get _entity() { + return this._config.entity || ""; + } + + get _remote() { + return this._config.remote || ""; + } + + get _theme() { + return this._config.theme; + } + + get _tv() { + return this._config.tv || false; + } + + render() { + if (!this.hass) { + return html``; + } + + const themes = ["Backend-selected", "default"].concat( + Object.keys(this.hass.themes.themes).sort() + ); + + const entities = Object.keys(this.hass.states).filter( + eid => eid.substr(0, eid.indexOf(".")) === "media_player" + ); + const remotes = [""].concat( + Object.keys(this.hass.states).filter( + eid => eid.substr(0, eid.indexOf(".")) === "remote" + ) + ); + + return html` + ${this.renderStyle()} +
+
+ + + + ${ + entities.map(entity => { + return html` + ${entity} + `; + }) + } + + +
+
+ + + ${ + remotes.map(remote => { + return html` + ${remote} + `; + }) + } + + + + + + ${ + themes.map(theme => { + return html` + ${theme} + `; + }) + } + + + Roku TV? +
+
Use the YAML editor if you need to specify custom services
+
+ `; + } + + renderStyle() { + return html` + + `; + } + + _valueChanged(ev) { + if (!this._config || !this.hass) { + return; + } + const target = ev.target; + if (this[`_${target.configValue}`] === target.value) { + return; + } + if (target.configValue) { + if (target.value === "") { + delete this._config[target.configValue]; + } else { + this._config = { + ...this._config, + [target.configValue]: + target.checked !== undefined ? target.checked : target.value + }; + } + } + fireEvent(this, "config-changed", { config: this._config }); + } +} + +customElements.define("tv-card-editor", TVCardEditor);