Skip to content

Commit

Permalink
add option to change entity/action for existing schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
niels committed Nov 12, 2020
1 parent 9a810b1 commit 3c06c0b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 46 deletions.
30 changes: 20 additions & 10 deletions src/custom-elements/scheduler-entitypicker-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ export class SchedulerEditorCard extends LitElement {
@property() selectedGroup?: string;
@property() selectedEntity?: string;
@property() selectedAction?: string;
@property() entity?: EntityElement;

firstUpdated() {
if (this.entity) {
const group = this.getGroups().find(group => group.entities.find(e => e == this.entity!.id));
if (!group) return;
this.selectedGroup = group.id;
this.selectedEntity = this.entity.id;
}
}

getGroups() {
if (!this.hass || !this.config) return [];
Expand Down Expand Up @@ -71,10 +81,10 @@ export class SchedulerEditorCard extends LitElement {
<div class="card-header">
<div class="name">
${this.config.title !== undefined
? typeof this.config.title == 'string'
? this.config.title
: ''
: localize('ui.panel.common.title', this.hass.language)}
? typeof this.config.title == 'string'
? this.config.title
: ''
: localize('ui.panel.common.title', this.hass.language)}
</div>
<ha-icon-button icon="hass:close" @click=${this.cancelClick}> </ha-icon-button>
</div>
Expand All @@ -87,15 +97,15 @@ export class SchedulerEditorCard extends LitElement {
<div class="header">${this.hass.localize('ui.components.entity.entity-picker.entity')}</div>
<button-group .items=${entities} value=${this.selectedEntity} @change=${this.selectEntity}>
${!this.selectedGroup
? localize('ui.panel.entity_picker.no_group_selected', this.hass.language)
: localize('ui.panel.entity_picker.no_entities_for_group', this.hass.language)}
? localize('ui.panel.entity_picker.no_group_selected', this.hass.language)
: localize('ui.panel.entity_picker.no_entities_for_group', this.hass.language)}
</button-group>
<div class="header">${this.hass.localize('ui.panel.config.automation.editor.actions.name')}</div>
<button-group .items=${actions} value=${this.selectedAction} @change=${this.selectAction}>
${!this.selectedEntity
? localize('ui.panel.entity_picker.no_entity_selected', this.hass.language)
: localize('ui.panel.entity_picker.no_actions_for_entity', this.hass.language)}
? localize('ui.panel.entity_picker.no_entity_selected', this.hass.language)
: localize('ui.panel.entity_picker.no_actions_for_entity', this.hass.language)}
</button-group>
${this.makeSchemeButton(actions)}
</div>
Expand All @@ -116,8 +126,8 @@ export class SchedulerEditorCard extends LitElement {
<mwc-button
class="${this.selectedAction == CreateTimeScheme ? ' active' : ''}"
@click=${() => {
this.selectedAction = CreateTimeScheme;
}}>
this.selectedAction = CreateTimeScheme;
}}>
<ha-icon icon="${PrettyPrintIcon('chart-timeline')}" class="padded-right"></ha-icon>
${localize('ui.panel.entity_picker.make_scheme', this.hass.language)}
</mwc-button>
Expand Down
25 changes: 21 additions & 4 deletions src/custom-elements/scheduler-timepicker-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ export class SchedulerTimepickerCard extends LitElement {
<div class="card-content">
<div class="header">${this.hass.localize('ui.panel.config.automation.editor.actions.name')}</div>
<div class="summary">
<div class="summary-entity">
<div
class="summary-entity"
@click=${this.editActionClick}
>
<ha-icon icon="${PrettyPrintIcon(this.entity.icon)}"> </ha-icon>
${capitalize(
PrettyPrintName(
Expand All @@ -93,7 +96,10 @@ export class SchedulerTimepickerCard extends LitElement {
<div class="summary-arrow">
<ha-icon icon="hass:arrow-right"> </ha-icon>
</div>
<div class="summary-action">
<div
class="summary-action"
@click=${this.editActionClick}
>
<ha-icon icon="${PrettyPrintIcon(this.actions[0].icon || DefaultActionIcon)}"> </ha-icon>
${capitalize(this.actions[0].name || computeEntity(this.actions[0].service))}
</div>
Expand Down Expand Up @@ -159,7 +165,10 @@ export class SchedulerTimepickerCard extends LitElement {
<div class="card-content">
<div class="header">${this.hass.localize('ui.panel.config.automation.editor.actions.name')}</div>
<div class="summary">
<div class="summary-entity">
<div
class="summary-entity"
@click=${this.editActionClick}
>
<ha-icon icon="${PrettyPrintIcon(this.entity.icon)}"> </ha-icon>
${capitalize(
PrettyPrintName(
Expand All @@ -172,7 +181,10 @@ export class SchedulerTimepickerCard extends LitElement {
<div class="summary-arrow">
<ha-icon icon="hass:arrow-right"> </ha-icon>
</div>
<div class="summary-action">
<div
class="summary-action"
@click=${this.editActionClick}
>
<ha-icon icon="${PrettyPrintIcon('chart-timeline')}"> </ha-icon>
${capitalize(localize('ui.panel.entity_picker.make_scheme', this.hass.language))}
</div>
Expand Down Expand Up @@ -368,6 +380,10 @@ export class SchedulerTimepickerCard extends LitElement {
this.dispatchEvent(myEvent);
}

editActionClick() {
const myEvent = new CustomEvent('editActionClick', { detail: this.entries });
this.dispatchEvent(myEvent);
}

async deleteClick(ev) {
const element = ev.target as HTMLElement;
Expand Down Expand Up @@ -431,6 +447,7 @@ export class SchedulerTimepickerCard extends LitElement {
grid-template-areas: 'icon text';
grid-auto-flow: column;
grid-gap: 10px;
cursor: pointer;
}
div.summary-entity:before,
Expand Down
91 changes: 59 additions & 32 deletions src/scheduler-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ export class SchedulerCard extends LitElement {
<scheduler-editor-card
.hass=${this._hass}
.config=${this._config}
.entity=${this.entity}
.selectedAction=${this.entries ? this.entries.every(e => e.endTime) ? CreateTimeScheme : this.actions[0].id : undefined}
@nextClick=${this._confirmItemClick}
@cancelClick=${this._cancelEditClick}
>
Expand All @@ -150,6 +152,7 @@ export class SchedulerCard extends LitElement {
@saveClick=${this._saveItemClick}
@optionsClick=${this._gotoOptionsClick}
@deleteClick=${this._deleteItemClick}
@editActionClick=${this._editActionClick}
>
</scheduler-timepicker-card>
`;
Expand All @@ -173,6 +176,9 @@ export class SchedulerCard extends LitElement {
this._view = EViews.NewSchedule;
this.editItem = null;
this.friendlyName = undefined;
this.entity = undefined;
this.actions = [];
this.entries = [];
}

private _cancelEditClick(): void {
Expand All @@ -187,47 +193,68 @@ export class SchedulerCard extends LitElement {
const action = String(ev.detail.action);

if (action != CreateTimeScheme) {
this.entries = [
{
entity: ev.detail.entity,
action: action,
time: { value: parseTimestamp('12:00') },
days: { type: EDayType.Daily },
},
];

if (this.entries && this.entries.length == 1) { //editing an existing entry
let entry = this.entries[0];
if (entity != entry.entity) entry = { ...entry, entity: entity };
if (action != entry.action) entry = { ...entry, action: action, variable: undefined };
this.entries = [entry];
} else {
this.entries = [
{
entity: entity,
action: action,
time: { value: parseTimestamp('12:00') },
days: { type: EDayType.Daily },
},
];
}
this._view = EViews.TimePicker;

this.actions = [computeEntityActions(entity, this._hass, this._config).find(e => e.id == action)!];
} else {
this.entries = [
{
entity: ev.detail.entity,
action: '',
time: { value: parseTimestamp('00:00') },
endTime: { value: parseTimestamp('08:00') },
days: { type: EDayType.Daily },
},
{
entity: ev.detail.entity,
action: '',
time: { value: parseTimestamp('08:00') },
endTime: { value: parseTimestamp('16:00') },
days: { type: EDayType.Daily },
},
{
entity: ev.detail.entity,
action: '',
time: { value: parseTimestamp('16:00') },
endTime: { value: MinutesPerDay },
days: { type: EDayType.Daily },
},
];
this.actions = computeEntityActions(entity, this._hass, this._config);
if (this.entries && this.entries.length > 1) { //editing an existing timeline
let entries = this.entries;
this.entries = entries.map(entry => {
if (entity != entry.entity) entry = { ...entry, entity: entity };
if (entry.action && !this.actions.find(e => entry.action == e.id)) entry = { ...entry, action: '', variable: undefined };
return entry;
});
}
else {
this.entries = [
{
entity: ev.detail.entity,
action: '',
time: { value: parseTimestamp('00:00') },
endTime: { value: parseTimestamp('08:00') },
days: { type: EDayType.Daily },
},
{
entity: ev.detail.entity,
action: '',
time: { value: parseTimestamp('08:00') },
endTime: { value: parseTimestamp('16:00') },
days: { type: EDayType.Daily },
},
{
entity: ev.detail.entity,
action: '',
time: { value: parseTimestamp('16:00') },
endTime: { value: MinutesPerDay },
days: { type: EDayType.Daily },
},
];
}
this._view = EViews.TimeScheme;
}
}

private _editActionClick(ev: CustomEvent): void {
this.entries = ev.detail as Entry[];
this._view = EViews.NewSchedule;
}

_saveItemClick(ev?: CustomEvent): void {
if (!this._hass || !this._config) return;

Expand Down

0 comments on commit 3c06c0b

Please sign in to comment.