const LitElement = Object.getPrototypeOf(
customElements.get("ha-panel-lovelace")
);
const html = LitElement.prototype.html;
import {
mdiPower,
mdiPowerOn,
mdiPowerOff,
mdiArrowLeft,
mdiVideoInputHdmi,
mdiHome,
mdiArrowUp,
mdiTelevisionGuide,
mdiArrowDown,
mdiChevronUp,
mdiChevronLeft,
mdiCheckboxBlankCircle,
mdiChevronRight,
mdiChevronDown,
mdiRewind,
mdiPlayPause,
mdiFastForward,
mdiVolumeMute,
mdiVolumeMinus,
mdiVolumePlus,
} from "https://unpkg.com/@mdi/js@6.4.95/mdi.js?module"
class TVCardServices extends LitElement {
static get properties() {
return {
hass: {},
_config: {},
_apps: {}
};
}
// static async getConfigElement() {
// await import("./tv-card-editor.js");
// return document.createElement("tv-card-editor");
// }
static getStubConfig() {
return {};
}
getCardSize() {
return 7;
}
setConfig(config) {
if (!config.entity) {
console.log("Invalid configuration");
return;
}
this._config = { theme: "default", ...config };
}
renderApplicationsRow(applicationIds) {
return html`
${applicationIds.map(applicationId => {
const application = this._config.applications[applicationId];
return html`
`;
})}
`;
}
render() {
if (!this._config || !this.hass) {
return html``;
}
const stateObj = this.hass.states[this._config.entity];
const emptyButton = html`
`;
const chunkedApplicationIds = this._config.applications === undefined ? [] : this.sliceIntoChunks(Object.keys(this._config.applications), 3);
return html`
${this.renderStyle()}
${
this._config.tv && this._config.power
? html`
`
: ""
}
${
this._config.tv && !(this._config.power) && (this._config.power_on || this._config.power_off)
? html`
${emptyButton}
`
: ""
}
${
this._config.back || this._config.source || this._config.home
? html`
${this._config.back
? html`
`
: emptyButton}
${this._config.source
? html`
`
: emptyButton}
${this._config.home
? html`
`
: emptyButton}
`
: ""
}
${
this._config.channelup ||
this._config.info ||
this._config.channeldown
? html`
${this._config.channelup
? html`
`
: emptyButton}
${this._config.info
? html`
`
: emptyButton}
${this._config.channeldown
? html`
`
: emptyButton}
`
: ""
}
${
this._config.reverse || this._config.play || this._config.forward
? html`
${this._config.reverse
? html`
`
: emptyButton}
${this._config.play
? html`
`
: emptyButton}
${this._config.forward
? html`
`
: emptyButton}
`
: ""
}
${
this._config.tv && (
this._config.volume_up ||
this._config.volume_down ||
this._config.volume_mute )
? html`
`
: ""
}
${chunkedApplicationIds.map(applicationIds => this.renderApplicationsRow(applicationIds))}
`;
}
updated(changedProps) {
if (!this._config) {
return;
}
const oldHass = changedProps.get("hass");
if (!oldHass || oldHass.themes !== this.hass.themes) {
this.applyThemesOnElement(this, this.hass.themes, this._config.theme);
}
}
renderStyle() {
return html`
`;
}
launchApp(e) {
this.hass.callService("media_player", "select_source", {
entity_id: this._config.entity,
source: e.currentTarget.value
});
}
handleActionClick(e) {
const action = e.currentTarget.action;
const actionData = this._config[action] !== undefined ? this._config[action] : this._config.applications[action];
if (actionData === undefined) {
console.error(`The action ${action} is not defined in the configuration and is not a defined application.`);
return;
}
const [domain, service] = actionData.service.split(".", 2);
this.hass.callService(
domain,
service,
actionData.service_data !== undefined
? actionData.service_data
: null
);
}
applyThemesOnElement(element, themes, localTheme) {
if (!element._themes) {
element._themes = {};
}
let themeName = themes.default_theme;
if (localTheme === "default" || (localTheme && themes.themes[localTheme])) {
themeName = localTheme;
}
const styles = Object.assign({}, element._themes);
if (themeName !== "default") {
var theme = themes.themes[themeName];
Object.keys(theme).forEach(key => {
var prefixedKey = "--" + key;
element._themes[prefixedKey] = "";
styles[prefixedKey] = theme[key];
});
}
if (element.updateStyles) {
element.updateStyles(styles);
} else if (window.ShadyCSS) {
// implement updateStyles() method of Polemer elements
window.ShadyCSS.styleSubtree(
/** @type {!HTMLElement} */ (element),
styles
);
}
const meta = document.querySelector("meta[name=theme-color]");
if (meta) {
if (!meta.hasAttribute("default-content")) {
meta.setAttribute("default-content", meta.getAttribute("content"));
}
const themeColor =
styles["--primary-color"] || meta.getAttribute("default-content");
meta.setAttribute("content", themeColor);
}
}
sliceIntoChunks(arr, chunkSize) {
const res = [];
for (let i = 0; i < arr.length; i += chunkSize) {
const chunk = arr.slice(i, i + chunkSize);
res.push(chunk);
}
return res;
}
}
customElements.define("tv-card", TVCardServices);