Skip to content

Commit

Permalink
Add Card visual editor
Browse files Browse the repository at this point in the history
  • Loading branch information
danimart1991 committed Apr 28, 2020
1 parent 208ece7 commit 417e42e
Show file tree
Hide file tree
Showing 2 changed files with 262 additions and 23 deletions.
226 changes: 226 additions & 0 deletions dist/pvpc-hourly-pricing-card-editor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
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 locale = {
da: {
optionName: 'Navn (valgfrit)',
optionEntity: 'Enhed (påkrævet)',
optionShowCurrent: 'Vis nuværende status',
optionShowDetails: 'Vis detaljer',
optionShowGraph: 'Vis graf',
optionShowInfo: 'Vis information'
},
de: {
optionName: 'Name (optional)',
optionEntity: 'Entity (Erforderlich)',
optionShowCurrent: 'Aktuellen Status anzeigen',
optionShowDetails: 'Details anzeigen',
optionShowGraph: 'Grafik anzeigen',
optionShowInfo: 'Informationen anzeigen'
},
en: {
optionName: 'Name (Optional)',
optionEntity: 'Entity (Required)',
optionShowCurrent: 'Show Current State',
optionShowDetails: 'Show Details',
optionShowGraph: 'Show Graph',
optionShowInfo: 'Show Info'
},
es: {
optionName: 'Nombre (Opcional)',
optionEntity: 'Entidad (Necesario)',
optionShowCurrent: 'Mostrar Estado Actual',
optionShowDetails: 'Mostrar Detalles',
optionShowGraph: 'Mostrar Gráfico',
optionShowInfo: 'Mostrar Información'
},
fr: {
optionName: 'Nom (Facultatif)',
optionEntity: 'Entity (Required)',
optionShowCurrent: "Afficher l'état actuel",
optionShowDetails: 'Afficher les détails',
optionShowGraph: 'Afficher le graphique',
optionShowInfo: 'Afficher les informations'
},
nl: {
optionName: 'Naam (optioneel)',
optionEntity: 'Entiteit (vereist)',
optionShowCurrent: 'Toon huidige status',
optionShowDetails: 'Details weergeven',
optionShowGraph: 'Show Graph',
optionShowInfo: 'Informatie weergeven'
},
ru: {
optionName: 'Имя (необязательно)',
optionEntity: 'Entity (обязательно)',
optionShowCurrent: 'Показать текущий статус',
optionShowDetails: 'Показать детали',
optionShowGraph: 'Показать график',
optionShowInfo: 'Показать информацию'
},
sv: {
optionName: 'Namn (valfritt)',
optionEntity: 'Enhet (obligatoriskt)',
optionShowCurrent: 'Visa aktuell status',
optionShowDetails: 'Visa detaljer',
optionShowGraph: 'Visa graf',
optionShowInfo: 'Visa information'
}
};

const LitElement = Object.getPrototypeOf(customElements.get('hui-view'));
const html = LitElement.prototype.html;
const css = LitElement.prototype.css;

export class PVPCHourlyPricingCardEditor extends LitElement {
setConfig(config) {
this._config = { ...config };
}

static get properties() {
return { hass: {}, _config: {} };
}

get _entity() {
return this._config.entity || '';
}

get _name() {
return this._config.name || '';
}

get _current() {
return this._config.current !== false;
}

get _details() {
return this._config.details !== false;
}

get _graph() {
return this._config.graph !== false;
}

get _info() {
return this._config.info !== false;
}

render() {
if (!this.hass) {
return html``;
}

this.lang = this.hass.selectedLanguage || this.hass.language;

const entities = Object.keys(this.hass.states).filter((eid) =>
Object.keys(this.hass.states[eid].attributes).some((aid) => aid == 'min_price_at')
);

return html`
<div class="card-config">
<div class="side-by-side">
<paper-input
label="${this.ll('optionName')}"
.value="${this._name}"
.configValue="${'name'}"
@value-changed="${this._valueChanged}"
>
</paper-input>
</div>
<div class="side-by-side">
<paper-dropdown-menu
label="${this.ll('optionEntity')}"
@value-changed="${this._valueChanged}"
.configValue="${'entity'}"
>
<paper-listbox slot="dropdown-content" .selected="${entities.indexOf(this._entity)}">
${entities.map((entity) => {
return html` <paper-item>${entity}</paper-item> `;
})}
</paper-listbox>
</paper-dropdown-menu>
</div>
<div class="side-by-side">
<ha-switch .checked=${this._current} .configValue="${'current'}" @change="${this._valueChanged}"
>${this.ll('optionShowCurrent')}</ha-switch
>
<ha-switch .checked=${this._details} .configValue="${'details'}" @change="${this._valueChanged}"
>${this.ll('optionShowDetails')}</ha-switch
>
</div>
<div class="side-by-side">
<ha-switch .checked=${this._graph} .configValue="${'graph'}" @change="${this._valueChanged}"
>${this.ll('optionShowGraph')}</ha-switch
>
<ha-switch .checked=${this._info} .configValue="${'info'}" @change="${this._valueChanged}"
>${this.ll('optionShowInfo')}</ha-switch
>
</div>
</div>
`;
}

_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 });
}

ll(str) {
if (locale[this.lang] === undefined) return locale.en[str];
return locale[this.lang][str];
}

static get styles() {
return css`
ha-switch {
padding-top: 16px;
}
.side-by-side {
display: flex;
}
.side-by-side > * {
flex: 1;
padding-right: 4px;
}
`;
}
}

customElements.define('pvpc-hourly-pricing-card-editor', PVPCHourlyPricingCardEditor);

window.customCards = window.customCards || [];
window.customCards.push({
type: 'pvpc-hourly-pricing-card',
name: 'PVPC Hourly Pricing',
preview: true,
description: 'The PVPC Hourly Pricing card allows you to display propertly the PVPC Hourly Pricing entity.'
});
59 changes: 36 additions & 23 deletions dist/pvpc-hourly-pricing-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const locale = {
};

const tariffPeriodIcons = {
error: 'mdi:alert-circle',
peak: 'mdi:weather-sunny',
valley: 'mdi:weather-night',
'super-valley': 'mdi:car-electric'
Expand Down Expand Up @@ -114,8 +115,16 @@ class PVPCHourlyPricingCard extends LitElement {
return { _config: {}, hass: {} };
}

static getStubConfig() {
return {};
static async getConfigElement() {
await import('./pvpc-hourly-pricing-card-editor.js');
return document.createElement('pvpc-hourly-pricing-card-editor');
}

static getStubConfig(hass, entities, entitiesFallback) {
const entity = Object.keys(hass.states).find((eid) =>
Object.keys(hass.states[eid].attributes).some((aid) => aid == 'min_price_at')
);
return { entity: entity };
}

setConfig(config) {
Expand Down Expand Up @@ -154,10 +163,7 @@ class PVPCHourlyPricingCard extends LitElement {

this.setPVPCHourlyPricingObj();
this.numberElements = 0;
this.lang =
this._config.language === undefined || this._config.language === 'hacs'
? this.hass.selectedLanguage || this.hass.language
: this._config.language;
this.lang = this.hass.selectedLanguage || this.hass.language;

if (!this.pvpcHourlyPricingObj) {
return html`
Expand Down Expand Up @@ -539,25 +545,32 @@ class PVPCHourlyPricingCard extends LitElement {
getTariffPeriodIcon(tariff) {
let icon;

if (tariff == 'discrimination') {
const utcHours = new Date().getUTCHours();
if (utcHours >= 21 || utcHours < 11) {
icon = 'valley';
} else {
icon = 'peak';
}
} else if (tariff == 'electric_car') {
const hours = new Date().getHours();
if (hours >= 13 && hours < 23) {
icon = 'peak';
} else if (hours >= 1 && hours < 3) {
icon = 'valley';
} else {
icon = 'super-valley';
}
switch (tariff) {
case 'normal':
break;
case 'discrimination':
const utcHours = new Date().getUTCHours();
if (utcHours >= 21 || utcHours < 11) {
icon = 'valley';
} else {
icon = 'peak';
}
break;
case 'electric_car':
const hours = new Date().getHours();
if (hours >= 13 && hours < 23) {
icon = 'peak';
} else if (hours >= 1 && hours < 3) {
icon = 'valley';
} else {
icon = 'super-valley';
}
break;
default:
icon = 'error';
}

return tariffPeriodIcons[icon];
return icon ? tariffPeriodIcons[icon] : '';
}

getDateString(datetime) {
Expand Down

0 comments on commit 417e42e

Please sign in to comment.