From d5161176892c9643f0062602e2cfa0e31d5449cb Mon Sep 17 00:00:00 2001 From: niels Date: Fri, 1 Jan 2021 14:53:43 +0100 Subject: [PATCH] several small improvments --- src/custom-elements/my-relative-time.ts | 2 +- src/custom-elements/scheduler-entity-row.ts | 48 ++++++++++++++------- src/custom-elements/timeslot-editor.ts | 14 +++--- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/src/custom-elements/my-relative-time.ts b/src/custom-elements/my-relative-time.ts index ce35bcd7..71749282 100755 --- a/src/custom-elements/my-relative-time.ts +++ b/src/custom-elements/my-relative-time.ts @@ -51,7 +51,7 @@ export class MyRelativeTime extends LitElement { (dateObj.valueOf() - startOfToday.valueOf()) / (hoursPerDay * secondsPerHour * 1000) ); let day = ''; - if (daysFromNow == 1 && dateObj.getHours() == 0 && dateObj.getMinutes() == 0) + if (daysFromNow == 1) day = localize('ui.components.date.tomorrow', this._hass.language); else if (daysFromNow > 0) day = formatWeekday(dateObj, this._hass.language); diff --git a/src/custom-elements/scheduler-entity-row.ts b/src/custom-elements/scheduler-entity-row.ts index 2a63d49f..c86d6e79 100755 --- a/src/custom-elements/scheduler-entity-row.ts +++ b/src/custom-elements/scheduler-entity-row.ts @@ -1,4 +1,4 @@ -import { LitElement, html, customElement, css, property } from 'lit-element'; +import { LitElement, html, customElement, css, property, PropertyValues } from 'lit-element'; import { unsafeHTML } from 'lit-html/directives/unsafe-html.js'; import { CardConfig, ETimeEvent, EDayType, Schedule, Timeslot, WeekdayType } from '../types'; import { PrettyPrintName, capitalize, PrettyPrintIcon, unique } from '../helpers'; @@ -16,22 +16,40 @@ import { weekdayToList } from '../data/date-time/weekday_to_list'; import { stringToTime, parseRelativeTime } from '../data/date-time/time'; import { stringToDate } from '../data/date-time/string_to_date'; import { standardIcon } from '../standard-configuration/standardIcon'; +import { STATE_NOT_RUNNING } from 'home-assistant-js-websocket'; @customElement('scheduler-entity-row') export class ScheduleEntityRow extends LitElement { @property() hass!: HomeAssistant; - @property() schedule!: Schedule; @property() config!: CardConfig; + @property() _schedule!: Schedule; + + set schedule(schedule: Schedule) { + this._schedule = schedule; + } + + shouldUpdate(changedProps: PropertyValues) { + if(changedProps.size > 1) return true; + const oldSchedule = changedProps.get("_schedule") as Schedule | undefined; + const newSchedule = this._schedule; + return JSON.stringify(oldSchedule) !== JSON.stringify(newSchedule); + } render() { - if (!this.schedule.next_entries.length) { - return html` + if (!this._schedule.next_entries.length) { + return this.hass.config.state !== STATE_NOT_RUNNING + ? html` + + Defective schedule entity: ${this._schedule.entity_id} + + ` + : html` - Defective schedule entity: ${this.schedule.entity_id} + ${this.hass.localize("ui.panel.lovelace.warning.starting")} `; } - const nextEntry = this.schedule.timeslots[this.schedule.next_entries[0]]; + const nextEntry = this._schedule.timeslots[this._schedule.next_entries[0]]; const entities = unique(nextEntry.actions.map(e => e.entity_id)).map(e => parseEntity(e, this.hass, this.config)); const entityIcon = unique(entities.map(e => e.icon)).length == 1 ? entities[0].icon @@ -56,23 +74,23 @@ export class ScheduleEntityRow extends LitElement { const computeDisplayItem = (displayItem: string): string => { switch (displayItem) { case 'name': - return this.schedule.name || ''; + return this._schedule.name || ''; case 'relative-time': return ``; case 'additional-tasks': - return this.schedule.timeslots.length > 1 + return this._schedule.timeslots.length > 1 ? '+' + localize( 'ui.panel.overview.additional_tasks', this.hass!.language, '{number}', - String(this.schedule.timeslots.length - 1) + String(this._schedule.timeslots.length - 1) ) : ''; case 'time': - return capitalize(this.computeTime(this.schedule.timeslots[this.schedule.next_entries[0]])); + return capitalize(this.computeTime(this._schedule.timeslots[this._schedule.next_entries[0]])); case 'days': - return capitalize(this.computeDays(this.schedule.weekdays)); + return capitalize(this.computeDays(this._schedule.weekdays)); case 'entity': return entityName.length ? capitalize(PrettyPrintName(entityName)) : ''; case 'action': @@ -93,7 +111,7 @@ export class ScheduleEntityRow extends LitElement { if (parts.length == 1) return unsafeHTML(input); return html` ${parts[0] ? unsafeHTML(parts[0]) : ''} - + ${parts[1] ? unsafeHTML(parts[1]) : ''} `; }; @@ -122,7 +140,7 @@ export class ScheduleEntityRow extends LitElement { : renderDisplayItems(this.config.display_options.secondary_info)} - + `; } @@ -227,10 +245,10 @@ export class ScheduleEntityRow extends LitElement { } toggleDisabled(ev: Event) { - if (!this.hass || !this.schedule) return; + if (!this.hass || !this._schedule) return; ev.stopPropagation(); const checked = !(ev.target as HTMLInputElement).checked; - this.hass!.callService('switch', checked ? 'turn_on' : 'turn_off', { entity_id: this.schedule.entity_id }); + this.hass!.callService('switch', checked ? 'turn_on' : 'turn_off', { entity_id: this._schedule.entity_id }); } static styles = css` diff --git a/src/custom-elements/timeslot-editor.ts b/src/custom-elements/timeslot-editor.ts index 193ddceb..d0586746 100755 --- a/src/custom-elements/timeslot-editor.ts +++ b/src/custom-elements/timeslot-editor.ts @@ -1,4 +1,4 @@ -import { LitElement, html, customElement, css, property, TemplateResult } from 'lit-element'; +import { LitElement, html, customElement, css, property, TemplateResult, eventOptions } from 'lit-element'; import { localize } from '../localize/localize'; import { ActionElement, EVariableType, LevelVariableConfig, ListVariableConfig, Timeslot } from '../types'; import { PrettyPrintName, unique } from '../helpers'; @@ -105,8 +105,9 @@ export class TimeslotEditor extends LitElement { >