diff --git a/src/action-handler-directive.ts b/src/action-handler-directive.ts index 5c0bae8..69778b0 100644 --- a/src/action-handler-directive.ts +++ b/src/action-handler-directive.ts @@ -161,16 +161,15 @@ class ActionHandler extends HTMLElement implements ActionHandler { } } -// TODO You need to replace all instances of "action-handler-boilerplate" with "action-handler-" -customElements.define('action-handler-boilerplate', ActionHandler); +customElements.define('action-handler-my-footer', ActionHandler); const getActionHandler = (): ActionHandler => { const body = document.body; - if (body.querySelector('action-handler-boilerplate')) { - return body.querySelector('action-handler-boilerplate') as ActionHandler; + if (body.querySelector('action-handler-my-footer')) { + return body.querySelector('action-handler-my-footer') as ActionHandler; } - const actionhandler = document.createElement('action-handler-boilerplate'); + const actionhandler = document.createElement('action-handler-my-footer'); body.appendChild(actionhandler); return actionhandler as ActionHandler; diff --git a/src/editor.ts b/src/editor.ts index a1bfc0b..ae0c6ff 100644 --- a/src/editor.ts +++ b/src/editor.ts @@ -12,7 +12,7 @@ import { } from 'lit-element'; import { HomeAssistant, fireEvent, LovelaceCardEditor, ActionConfig } from 'custom-card-helpers'; -import { BoilerplateCardConfig } from './types'; +import { MyFooterCardConfig } from './types'; const options = { required: { @@ -55,15 +55,15 @@ const options = { }, }; -@customElement('boilerplate-card-editor') -export class BoilerplateCardEditor extends LitElement implements LovelaceCardEditor { +@customElement('my-footer-card-editor') +export class MyFooterCardEditor extends LitElement implements LovelaceCardEditor { @property({ attribute: false }) public hass?: HomeAssistant; - @internalProperty() private _config?: BoilerplateCardConfig; + @internalProperty() private _config?: MyFooterCardConfig; @internalProperty() private _toggle?: boolean; @internalProperty() private _helpers?: any; private _initialized = false; - public setConfig(config: BoilerplateCardConfig): void { + public setConfig(config: MyFooterCardConfig): void { this._config = config; this.loadCardHelpers(); @@ -114,7 +114,9 @@ export class BoilerplateCardEditor extends LitElement implements LovelaceCardEdi this._helpers.importMoreInfoControl('climate'); // You can restrict on domain type - const entities = Object.keys(this.hass.states).filter(eid => eid.substr(0, eid.indexOf('.')) === 'sun'); + const entities = Object.keys(this.hass.states).filter( + (eid) => eid.substr(0, eid.indexOf('.')) === 'sun' + ); return html`
@@ -133,8 +135,11 @@ export class BoilerplateCardEditor extends LitElement implements LovelaceCardEdi @value-changed=${this._valueChanged} .configValue=${'entity'} > - - ${entities.map(entity => { + + ${entities.map((entity) => { return html` ${entity} `; @@ -275,7 +280,7 @@ export class BoilerplateCardEditor extends LitElement implements LovelaceCardEdi } if (target.configValue) { if (target.value === '') { - const tmpConfig = {...this._config}; + const tmpConfig = { ...this._config }; delete tmpConfig[target.configValue]; this._config = tmpConfig; } else { diff --git a/src/my-footer.ts b/src/my-footer.ts index 932886f..2858215 100644 --- a/src/my-footer.ts +++ b/src/my-footer.ts @@ -17,15 +17,11 @@ import { hasAction, ActionHandlerEvent, handleAction, - LovelaceCardEditor, - getLovelace, } from 'custom-card-helpers'; // This is a community maintained npm module with common helper functions/types import { subscribeRenderTemplate } from 'card-tools/src/templates' import { disableBodyScroll, enableBodyScroll } from 'body-scroll-lock' -import './editor'; - -import type { BoilerplateCardConfig } from './types'; +import type { MyFooterCardConfig } from './types'; import { actionHandler } from './action-handler-directive'; import { FOOTER_VERSION } from './const'; import { localize } from './localize/localize'; @@ -49,10 +45,6 @@ console.info( @customElement('my-footer') export class MyFooter extends LitElement { - public static async getConfigElement(): Promise { - return document.createElement('boilerplate-card-editor'); - } - public static getStubConfig(): object { return {}; } @@ -66,10 +58,10 @@ export class MyFooter extends LitElement { } @property({ attribute: false }) public hass!: HomeAssistant; - @internalProperty() private config!: BoilerplateCardConfig; + @internalProperty() private config!: MyFooterCardConfig; // https://lit-element.polymer-project.org/guide/properties#accessors-custom - public setConfig(config: BoilerplateCardConfig): void { + public setConfig(config: MyFooterCardConfig): void { // Run the content through a Jinja2 parser function and create new config field 'parsed_content' this.applyTemplate(config.content ? config.content : '') @@ -84,9 +76,9 @@ export class MyFooter extends LitElement { ...config, } }) - + // // Old code to set config, use if not parsing jinja2 - // this.config = { + // this.config = { // name: 'MyFooter', // ...config, // }; @@ -117,7 +109,7 @@ export class MyFooter extends LitElement { // console.log('Entity Name:', entityName) // console.log('Entity:', entity) // conf.buttons[0] - + const styles = conf.styles ? conf.styles : {} const buttons = conf.buttons ? conf.buttons : [] @@ -139,7 +131,7 @@ export class MyFooter extends LitElement { return Object.keys(style).reduce((acc, key) => ( acc + key.split(/(?=[A-Z])/).join('-').toLowerCase() + ':' + style[key] + ';' ), ''); - } + } const toggleScroll = () => { this.config.disabled_scroll = !this.config.disabled_scroll @@ -161,7 +153,7 @@ export class MyFooter extends LitElement {
- { this._handleDynamicButton(e, {toggle: this.config.scroll_disabled}) // console.log('Testing!!!') @@ -175,7 +167,7 @@ export class MyFooter extends LitElement { buttons.map((item, index) => { return html`
- this._handleDynamicButton(e, item.button)} .actionHandler=${actionHandler({ hasHold: hasAction(item.hold_action), @@ -206,7 +198,7 @@ export class MyFooter extends LitElement { } } - + // Promisified wrapper around subscribeRenderTemplate to allow this to be called as an async function private parseTemplate(template, variables): Promise { return new Promise((resolve, reject) => { @@ -214,10 +206,10 @@ export class MyFooter extends LitElement { async (result) => { resolve(result); }, - {template: template, variables: variables, + {template: template, variables: variables, entity_ids: []}, false); let unsub = console.log; - + // Catch any errors and unsubscribe (async () => { try { @@ -229,18 +221,18 @@ export class MyFooter extends LitElement { })(); }) } - + private async applyTemplate(template): Promise{ try{ - const _templateVariables = { + const _templateVariables = { user: { - name: 'Anthon', + name: 'Anthon', is_admin: true, is_owner: true }, page: { ...location, - path: location.pathname + path: location.pathname }, // theme: this.hass.selectedTheme ? this.hass.selectedTheme : this.hass.themes.default_theme theme: 'Dark - Wooden' @@ -263,16 +255,16 @@ export class MyFooter extends LitElement { .footer-container { display: flex; } - + .text-container { - + } .text-container p { border: inherit; margin: 0; padding: 0; } - + .button-container { flex-grow: 1; padding-right: 20px; diff --git a/src/my-slider.ts b/src/my-slider.ts index e77905a..102dd92 100644 --- a/src/my-slider.ts +++ b/src/my-slider.ts @@ -13,18 +13,10 @@ import { import { HomeAssistant, hasConfigOrEntityChanged, - hasAction, - ActionHandlerEvent, - handleAction, - LovelaceCardEditor, - getLovelace, } from 'custom-card-helpers'; // This is a community maintained npm module with common helper functions/types import { disableBodyScroll, enableBodyScroll } from 'body-scroll-lock' -import './editor'; - -import type { BoilerplateCardConfig } from './types'; -import { actionHandler } from './action-handler-directive'; +import type { MySliderCardConfig } from './types'; import { SLIDER_VERSION } from './const'; import { localize } from './localize/localize'; @@ -47,10 +39,6 @@ console.info( @customElement('my-slider') export class MySlider extends LitElement { - public static async getConfigElement(): Promise { - return document.createElement('boilerplate-card-editor'); - } - public static getStubConfig(): object { return {}; } @@ -64,15 +52,15 @@ export class MySlider extends LitElement { } @property({ attribute: false }) public hass!: HomeAssistant; - @internalProperty() private config!: BoilerplateCardConfig; + @internalProperty() private config!: MySliderCardConfig; // https://lit-element.polymer-project.org/guide/properties#accessors-custom - public setConfig(config: BoilerplateCardConfig): void { - + public setConfig(config: MySliderCardConfig): void { + if (!config.entity) { throw new Error("You need to define entity"); } - + if (!config.entity.includes("input_number.") && !config.entity.includes("light.") && !config.entity.includes("media_player.") && !config.entity.includes("cover.") && !config.entity.includes("fan.") && !config.entity.includes("switch.") && !config.entity.includes("lock.") ) { throw new Error("Entity has to be a light, input_number, media_player, cover or a fan."); } @@ -158,7 +146,7 @@ export class MySlider extends LitElement { // var entityClass = this.hass.states[entity] - + var styleStr = ` --slider-width: ${width}; --slider-width-inverse: -${width}; @@ -171,7 +159,7 @@ export class MySlider extends LitElement { --slider-secondary-color-off: ${secondarySliderColorOff}; --slider-radius: ${radius}; --border: ${border}; - + --thumb-width: ${thumbWidth}; --thumb-height: ${thumbHeight}; --thumb-color: ${(entity.state === "off" || entity.state == undefined) ? "var(--thumb-color-off)" : "var(--thumb-color-on)"}; @@ -226,12 +214,12 @@ export class MySlider extends LitElement { } }; - + const toggleScroll = () => { this.config.disabled_scroll = !this.config.disabled_scroll if (this.config.disabled_scroll) { disableBodyScroll(window) - } else { + } else { enableBodyScroll(window) } } @@ -241,9 +229,9 @@ export class MySlider extends LitElement { return html`
- @@ -255,8 +243,8 @@ export class MySlider extends LitElement { return html`
- @@ -270,9 +258,9 @@ export class MySlider extends LitElement { return html`
- @@ -290,8 +278,8 @@ export class MySlider extends LitElement { return html`
- @@ -304,9 +292,9 @@ export class MySlider extends LitElement { return html`
- @@ -319,9 +307,9 @@ export class MySlider extends LitElement { return html`
- @@ -334,8 +322,8 @@ export class MySlider extends LitElement { return html`
- @@ -348,8 +336,8 @@ export class MySlider extends LitElement { return html`
- @@ -365,7 +353,7 @@ export class MySlider extends LitElement { // handleAction(this, this.hass, this.config, ev.detail.action); // } // } - + private _setBrightness(_entity, _target, _minSet, _maxSet): void { var value = _target.value; if (value > _maxSet) { @@ -373,16 +361,16 @@ export class MySlider extends LitElement { } else if (value < _minSet) { value = _minSet; } - + this.hass.callService("homeassistant", "turn_on", { entity_id: _entity.entity_id, brightness: value * 2.56 }); - + _target.value = value; - + } - + private _setWarmth(_entity, _target, _minSet, _maxSet): void { var value = _target.value; if (value > _maxSet) { @@ -390,15 +378,15 @@ export class MySlider extends LitElement { } else if (value < _minSet) { value = _minSet; } - + this.hass.callService("homeassistant", "turn_on", { entity_id: _entity.entity_id, color_temp: value }); - + _target.value = value; } - + private _setInputNumber(_entity, _target, _minSet, _maxSet): void { var value = _target.value; if (value > _maxSet) { @@ -406,16 +394,16 @@ export class MySlider extends LitElement { } else if (value < _minSet) { value = _minSet; } - + this.hass.callService("input_number", "set_value", { entity_id: _entity.entity_id, value: value }); - + _target.value = value; } - + private _setFan(_entity, _target, _minSet, _maxSet): void { var value = _target.value; if (value > _maxSet) { @@ -423,15 +411,15 @@ export class MySlider extends LitElement { } else if (value < _minSet) { value = _minSet; } - + this.hass.callService("fan", "set_percentage", { entity_id: _entity.entity_id, percentage: value }); - + _target.value = value; } - + private _setCover(_entity, _target, _minSet, _maxSet): void { var value = _target.value; if (value > _maxSet) { @@ -439,15 +427,15 @@ export class MySlider extends LitElement { } else if (value < _minSet) { value = _minSet; } - + this.hass.callService("cover", "set_cover_position", { entity_id: _entity.entity_id, position: value }); - + _target.value = value; } - + private _setMediaVolume(_entity, _target, _minSet, _maxSet): void { var value = _target.value; if (value > _maxSet) { @@ -455,15 +443,15 @@ export class MySlider extends LitElement { } else if (value < _minSet) { value = _minSet; } - + this.hass.callService("media_player", "volume_set", { entity_id: _entity.entity_id, volume_level: value / 100 }); - + _target.value = value; } - + private _setSwitch(_entity, _target, _minSet, _maxSet, _minBar, _maxBar): void { var value = _target.value; var threshold = Math.min(_maxSet, _maxBar) //pick lesser of the two @@ -472,10 +460,10 @@ export class MySlider extends LitElement { entity_id: _entity.entity_id }); } - + _target.value = Number(Math.max(_minSet, _minBar)); } - + private _setLock(_entity, _target, _minSet, _maxSet, _minBar, _maxBar): void { var value = _target.value; var threshold = Math.min(_maxSet, _maxBar) //pick lesser of the two @@ -485,10 +473,10 @@ export class MySlider extends LitElement { entity_id: _entity.entity_id }); } - + _target.value = Number(Math.max(_minSet, _minBar)); } - + // https://lit-element.polymer-project.org/guide/styles static get styles(): CSSResult { @@ -499,7 +487,7 @@ export class MySlider extends LitElement { overflow: hidden; border-radius: var(--slider-radius); } - + .slider-container input[type="range"] { outline: 0; border: var(--border); @@ -522,14 +510,14 @@ export class MySlider extends LitElement { -ms-transform: rotate(var(--rotate)); transform: rotate(var(--rotate)); } - + .slider-container input[type="range"]::-webkit-slider-runnable-track { height: var(--slider-height); -webkit-appearance: none; color: var(--slider-main-color); transition: box-shadow 0.2s ease-in-out; } - + .slider-container input[type="range"]::-webkit-slider-thumb { width: var(--thumb-width); height: var(--thumb-height); @@ -538,19 +526,19 @@ export class MySlider extends LitElement { border-radius: 0; transition: box-shadow 0.2s ease-in-out; position: relative; - + box-shadow: -3500px 0 0 3500px var(--slider-main-color), inset 0 0 0 25px var(--thumb-color); - + top: var(--thumb-top); border-right: var(--thumb-border-right); border-left: var(--thumb-border-left); border-top: var(--thumb-border-top); border-bottom: var(--thumb-border-bottom); } - + .slider-container input[type=range]::-moz-range-thumb { width: calc(var(--thumb-width) / 4); - height: calc(var(--thumb-height) / 2); + height: calc(var(--thumb-height) / 2); box-shadow: -3500px 10px 0 3500px var(--slider-main-color), inset 0 0 0 25px var(--thumb-color); top: var(--thumb-top); cursor: ew-resize; @@ -563,11 +551,11 @@ export class MySlider extends LitElement { border-top: var(--thumb-border-top); border-bottom: var(--thumb-border-bottom); } - + .slider-container input[type="range"]::-webkit-slider-thumb:hover { cursor: default; } - + .slider-container input[type="range"]:hover { cursor: default; } diff --git a/src/types.ts b/src/types.ts index 9a5e863..4abdf4b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2,13 +2,18 @@ import { ActionConfig, LovelaceCard, LovelaceCardConfig, LovelaceCardEditor } fr declare global { interface HTMLElementTagNameMap { - 'boilerplate-card-editor': LovelaceCardEditor; + 'my-footer-card-editor': LovelaceCardEditor; 'hui-error-card': LovelaceCard; } } +export interface MySliderCardConfig extends LovelaceCardConfig { + type: string; + entity: string; +} + // TODO Add your configuration elements here for type-checking -export interface BoilerplateCardConfig extends LovelaceCardConfig { +export interface MyFooterCardConfig extends LovelaceCardConfig { type: string; name?: string; show_warning?: boolean;