From 95a83f2e2e90af9ae45aad7bc0fd5a2cf36740a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Nyi=CC=81ri?= Date: Sat, 8 May 2021 17:58:57 +0200 Subject: [PATCH] Add: Better check for config format of entities --- dist/plex-meets-homeassistant.js | 43 +++++++++++++++++++++----------- src/const.ts | 8 +++++- src/modules/PlayController.ts | 6 ++--- src/plex-meets-homeassistant.ts | 17 +++++++++++-- 4 files changed, 53 insertions(+), 21 deletions(-) diff --git a/dist/plex-meets-homeassistant.js b/dist/plex-meets-homeassistant.js index 1ad15af..0679f65 100644 --- a/dist/plex-meets-homeassistant.js +++ b/dist/plex-meets-homeassistant.js @@ -17194,6 +17194,20 @@ var lodash = createCommonjsModule(function (module, exports) { }.call(commonjsGlobal)); }); +/* eslint-disable @typescript-eslint/no-explicit-any */ +const CSS_STYLE = { + width: 138, + height: 203, + expandedWidth: 220, + expandedHeight: 324, + episodeWidth: 300, + episodeHeight: 169 +}; +const supported = { + kodi: ['movie', 'episode'], + androidtv: ['movie', 'show', 'season', 'episode'] +}; + var bind = function bind(fn, thisArg) { return function wrap() { var args = new Array(arguments.length); @@ -18698,10 +18712,7 @@ class Plex { class PlayController { constructor(hass, plex, entity) { - this.supported = { - kodi: ['movie', 'episode'], - androidtv: ['movie', 'show', 'season', 'episode'] - }; + this.supported = supported; this.getState = async (entityID) => { return this.hass.callApi('GET', `states/${entityID}`); }; @@ -18894,15 +18905,6 @@ const getOffset = (el) => { return { top: y, left: x }; }; -const CSS_STYLE = { - width: 138, - height: 203, - expandedWidth: 220, - expandedHeight: 324, - episodeWidth: 300, - episodeHeight: 169 -}; - /** * @license * Copyright (c) 2017 The Polymer Project Authors. All rights reserved. @@ -20094,9 +20096,22 @@ class PlexMeetsHomeAssistant extends HTMLElement { }; this.setConfig = (config) => { this.plexProtocol = 'http'; - if (!config.entity || config.entity.length === 0) { + if (!config.entity || config.entity.length === 0 || !lodash.isObject(config.entity)) { throw new Error('You need to define at least one entity'); } + if (lodash.isObject(config.entity)) { + let entityDefined = false; + // eslint-disable-next-line consistent-return + lodash.forEach(config.entity, (value, key) => { + if (supported[key]) { + entityDefined = true; + return false; + } + }); + if (!entityDefined) { + throw new Error('You need to define at least one supported entity'); + } + } if (!config.token) { throw new Error('You need to define a token'); } diff --git a/src/const.ts b/src/const.ts index e74ce5f..6f7313d 100644 --- a/src/const.ts +++ b/src/const.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ const CSS_STYLE = { width: 138, height: 203, @@ -7,6 +8,11 @@ const CSS_STYLE = { episodeHeight: 169 }; +const supported: any = { + kodi: ['movie', 'episode'], + androidtv: ['movie', 'show', 'season', 'episode'] +}; + const LOREM_IPSUM = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec semper risus vitae aliquet interdum. Nulla facilisi. Pellentesque viverra sagittis lorem eget aliquet. Cras vehicula, purus vel consectetur mattis, ipsum arcu ullamcorper mi, id viverra purus ex eu dolor. Integer vehicula lacinia sem convallis iaculis. Nulla fermentum erat interdum, efficitur felis in, mollis neque. Vivamus luctus metus eget nisl pellentesque, placerat elementum magna eleifend. Curabitur tempor imperdiet felis, a vehicula augue tincidunt quis. Proin a libero vel ex ultrices commodo. Nam mattis, neque ac auctor pulvinar, augue ipsum ullamcorper magna, vel ultrices dolor purus in nisi. Ut et turpis varius, rhoncus sem quis, finibus quam. Nulla interdum lacus finibus, rhoncus magna non, semper erat. Nunc sit amet imperdiet ipsum. Vivamus purus velit, lobortis vitae consequat id, lobortis et nunc. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed sagittis tellus et dictum ultricies. Duis commodo tellus sed augue cursus dapibus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In blandit, tellus id tincidunt commodo, ante mauris sagittis mi, sit amet elementum nunc nulla eget diam. Aenean sed faucibus mauris. Aliquam eu ultricies orci, in consequat dui. @@ -17,4 +23,4 @@ Nam molestie auctor erat. Sed tempor porttitor nisi vel hendrerit. Vestibulum eg Vivamus at enim hendrerit, lacinia mauris sit amet, facilisis dolor. Sed egestas urna ut ante iaculis consequat. Ut ac libero commodo, egestas ligula in, pretium nisi. Nullam dolor sapien, auctor vitae enim non, dapibus laoreet enim. Nullam rhoncus efficitur nibh faucibus ultricies. In vitae libero sed turpis posuere tincidunt. Vivamus dapibus erat placerat lorem fringilla, a varius elit placerat. Aliquam sodales condimentum porta. Etiam ac mattis dui, quis sodales urna. Praesent at laoreet lectus. Ut tellus nisl, ullamcorper vitae metus eget, porttitor sagittis metus. Aliquam purus dolor, porttitor eget magna a, ullamcorper aliquam dui. Donec a neque vel justo fringilla tincidunt maximus eget nisl. Aliquam consequat id turpis nec laoreet. In dignissim enim quis neque scelerisque finibus.`; -export { CSS_STYLE, LOREM_IPSUM }; +export { CSS_STYLE, LOREM_IPSUM, supported }; diff --git a/src/modules/PlayController.ts b/src/modules/PlayController.ts index 8fa02e7..8e93d01 100644 --- a/src/modules/PlayController.ts +++ b/src/modules/PlayController.ts @@ -3,6 +3,7 @@ import { HomeAssistant } from 'custom-card-helpers'; import _ from 'lodash'; import Plex from './Plex'; +import { supported } from '../const'; class PlayController { entity: Record; @@ -11,10 +12,7 @@ class PlayController { plex: Plex; - supported: any = { - kodi: ['movie', 'episode'], - androidtv: ['movie', 'show', 'season', 'episode'] - }; + supported: any = supported; constructor(hass: HomeAssistant, plex: Plex, entity: Record) { this.hass = hass; diff --git a/src/plex-meets-homeassistant.ts b/src/plex-meets-homeassistant.ts index 3e2d135..1c222ca 100644 --- a/src/plex-meets-homeassistant.ts +++ b/src/plex-meets-homeassistant.ts @@ -2,10 +2,10 @@ /* eslint-env browser */ import { HomeAssistant } from 'custom-card-helpers'; import _ from 'lodash'; +import { supported, CSS_STYLE } from './const'; import Plex from './modules/Plex'; import PlayController from './modules/PlayController'; import { escapeHtml, getOffset } from './modules/utils'; -import { CSS_STYLE } from './const'; import style from './modules/style'; class PlexMeetsHomeAssistant extends HTMLElement { @@ -780,9 +780,22 @@ class PlexMeetsHomeAssistant extends HTMLElement { setConfig = (config: any): void => { this.plexProtocol = 'http'; - if (!config.entity || config.entity.length === 0) { + if (!config.entity || config.entity.length === 0 || !_.isObject(config.entity)) { throw new Error('You need to define at least one entity'); } + if (_.isObject(config.entity)) { + let entityDefined = false; + // eslint-disable-next-line consistent-return + _.forEach(config.entity, (value, key) => { + if (supported[key]) { + entityDefined = true; + return false; + } + }); + if (!entityDefined) { + throw new Error('You need to define at least one supported entity'); + } + } if (!config.token) { throw new Error('You need to define a token'); }