From 48c2bee113b69dc2a4add50731a32c043cfa2ea9 Mon Sep 17 00:00:00 2001 From: Misiu Date: Tue, 8 Sep 2020 11:15:00 +0200 Subject: [PATCH 1/6] add weekday to time condition editor --- src/data/automation.ts | 1 + .../types/ha-automation-condition-time.ts | 80 ++++++++++++++++++- src/translations/en.json | 11 ++- 3 files changed, 89 insertions(+), 3 deletions(-) diff --git a/src/data/automation.ts b/src/data/automation.ts index 7ef285e1e7b5..fe29dd8b35a1 100644 --- a/src/data/automation.ts +++ b/src/data/automation.ts @@ -170,6 +170,7 @@ export interface TimeCondition { condition: "time"; after: string; before: string; + weekday: string[]; } export interface TemplateCondition { diff --git a/src/panels/config/automation/condition/types/ha-automation-condition-time.ts b/src/panels/config/automation/condition/types/ha-automation-condition-time.ts index baac37cf3500..75cbbcf14b19 100644 --- a/src/panels/config/automation/condition/types/ha-automation-condition-time.ts +++ b/src/panels/config/automation/condition/types/ha-automation-condition-time.ts @@ -6,6 +6,8 @@ import { internalProperty, LitElement, property, + CSSResult, + css, } from "lit-element"; import "../../../../../components/ha-formfield"; import "../../../../../components/ha-radio"; @@ -15,14 +17,31 @@ import { ConditionElement, handleChangeEvent, } from "../ha-automation-condition-row"; +import { HaSwitch } from "../../../../../components/ha-switch"; +import { computeRTLDirection } from "../../../../../common/util/compute_rtl"; +import { fireEvent } from "../../../../../common/dom/fire_event"; const includeDomains = ["input_datetime"]; +const DAYS = { + mon: 1, + tue: 2, + wed: 3, + thu: 4, + fri: 5, + sat: 6, + sun: 7, +}; + +interface WeekdayHaSwitch extends HaSwitch { + day: string; +} + @customElement("ha-automation-condition-time") export class HaTimeCondition extends LitElement implements ConditionElement { @property({ attribute: false }) public hass!: HomeAssistant; - @property() public condition!: TimeCondition; + @property({ attribute: false }) public condition!: TimeCondition; @internalProperty() private _inputModeBefore?: boolean; @@ -33,7 +52,7 @@ export class HaTimeCondition extends LitElement implements ConditionElement { } protected render() { - const { after, before } = this.condition; + const { after, before, weekday } = this.condition; const inputModeBefore = this._inputModeBefore ?? before?.startsWith("input_datetime."); @@ -128,6 +147,26 @@ export class HaTimeCondition extends LitElement implements ConditionElement { .value=${before?.startsWith("input_datetime.") ? "" : before} @value-changed=${this._valueChanged} >`} + ${Object.keys(DAYS).map( + (day) => html` + + + + + ` + )} `; } @@ -143,4 +182,41 @@ export class HaTimeCondition extends LitElement implements ConditionElement { private _valueChanged(ev: CustomEvent): void { handleChangeEvent(this, ev); } + + private _dayValueChanged(ev: CustomEvent): void { + ev.stopPropagation(); + + const daySwitch = ev.currentTarget as WeekdayHaSwitch; + + let days = this.condition.weekday || []; + + if (daySwitch.checked) { + days.push(daySwitch.day); + } else { + days = days.filter((d) => d !== daySwitch.day); + } + + days.sort((a: string, b: string) => DAYS[a] - DAYS[b]); + + this.condition.weekday = days; + + fireEvent(this, "value-changed", { + value: this.condition, + }); + } + + static get styles(): CSSResult { + return css` + .weekday-toggle { + display: flex; + height: 40px; + } + `; + } +} + +declare global { + interface HTMLElementTagNameMap { + "ha-automation-condition-time": HaTimeCondition; + } } diff --git a/src/translations/en.json b/src/translations/en.json index 85fa6823adbe..45035b41a086 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -1139,7 +1139,16 @@ "type_input": "[%key:ui::panel::config::automation::editor::triggers::type::time::type_input%]", "label": "[%key:ui::panel::config::automation::editor::triggers::type::time::label%]", "after": "After", - "before": "Before" + "before": "Before", + "weekdays": { + "mon": "Monday", + "tue": "Tuesday", + "wed": "Wednesday", + "thu": "Thursday", + "fri": "Friday", + "sat": "Saturday", + "sun": "Sunday" + } }, "zone": { "label": "[%key:ui::panel::config::automation::editor::triggers::type::zone::label%]", From ba62dce5098fa016416abfa9a97be6a6de223dec Mon Sep 17 00:00:00 2001 From: Misiu Date: Fri, 25 Sep 2020 09:00:57 +0200 Subject: [PATCH 2/6] address comments --- .../condition/types/ha-automation-condition-time.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/panels/config/automation/condition/types/ha-automation-condition-time.ts b/src/panels/config/automation/condition/types/ha-automation-condition-time.ts index 75cbbcf14b19..af627be9a2b2 100644 --- a/src/panels/config/automation/condition/types/ha-automation-condition-time.ts +++ b/src/panels/config/automation/condition/types/ha-automation-condition-time.ts @@ -198,10 +198,8 @@ export class HaTimeCondition extends LitElement implements ConditionElement { days.sort((a: string, b: string) => DAYS[a] - DAYS[b]); - this.condition.weekday = days; - fireEvent(this, "value-changed", { - value: this.condition, + value: { ...this.condition, weekday: days }, }); } From b3997f871b3a01b5f3e194fc24326256330e6e21 Mon Sep 17 00:00:00 2001 From: Misiu Date: Fri, 25 Sep 2020 12:57:57 +0200 Subject: [PATCH 3/6] fix logic --- .../condition/types/ha-automation-condition-time.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/panels/config/automation/condition/types/ha-automation-condition-time.ts b/src/panels/config/automation/condition/types/ha-automation-condition-time.ts index af627be9a2b2..ec70e3fe276a 100644 --- a/src/panels/config/automation/condition/types/ha-automation-condition-time.ts +++ b/src/panels/config/automation/condition/types/ha-automation-condition-time.ts @@ -160,7 +160,7 @@ export class HaTimeCondition extends LitElement implements ConditionElement { > @@ -188,7 +188,7 @@ export class HaTimeCondition extends LitElement implements ConditionElement { const daySwitch = ev.currentTarget as WeekdayHaSwitch; - let days = this.condition.weekday || []; + let days = this.condition.weekday || Object.keys(DAYS); if (daySwitch.checked) { days.push(daySwitch.day); From 22fb048f3d288235f18052cc68ee9aa1c06f13fb Mon Sep 17 00:00:00 2001 From: Misiu Date: Fri, 25 Sep 2020 13:04:42 +0200 Subject: [PATCH 4/6] remove ev.stopPropagation() --- .../automation/condition/types/ha-automation-condition-time.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/panels/config/automation/condition/types/ha-automation-condition-time.ts b/src/panels/config/automation/condition/types/ha-automation-condition-time.ts index ec70e3fe276a..ef70b14ed91e 100644 --- a/src/panels/config/automation/condition/types/ha-automation-condition-time.ts +++ b/src/panels/config/automation/condition/types/ha-automation-condition-time.ts @@ -184,8 +184,6 @@ export class HaTimeCondition extends LitElement implements ConditionElement { } private _dayValueChanged(ev: CustomEvent): void { - ev.stopPropagation(); - const daySwitch = ev.currentTarget as WeekdayHaSwitch; let days = this.condition.weekday || Object.keys(DAYS); From 6f65ef242d9d4a714e543fdc50d7ded7ae7d6716 Mon Sep 17 00:00:00 2001 From: Misiu Date: Fri, 25 Sep 2020 13:12:35 +0200 Subject: [PATCH 5/6] oops --- .../automation/condition/types/ha-automation-condition-time.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/panels/config/automation/condition/types/ha-automation-condition-time.ts b/src/panels/config/automation/condition/types/ha-automation-condition-time.ts index ef70b14ed91e..b33c2bbd72bb 100644 --- a/src/panels/config/automation/condition/types/ha-automation-condition-time.ts +++ b/src/panels/config/automation/condition/types/ha-automation-condition-time.ts @@ -160,7 +160,7 @@ export class HaTimeCondition extends LitElement implements ConditionElement { > From 514b57b6b30868551b6c7ab4cf718d9e3fd6dd45 Mon Sep 17 00:00:00 2001 From: Misiu Date: Fri, 25 Sep 2020 13:31:59 +0200 Subject: [PATCH 6/6] fix types --- src/data/automation.ts | 6 +++--- .../condition/types/ha-automation-condition-time.ts | 12 ++++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/data/automation.ts b/src/data/automation.ts index fe29dd8b35a1..4b06d4af6384 100644 --- a/src/data/automation.ts +++ b/src/data/automation.ts @@ -168,9 +168,9 @@ export interface ZoneCondition { export interface TimeCondition { condition: "time"; - after: string; - before: string; - weekday: string[]; + after?: string; + before?: string; + weekday?: string | string[]; } export interface TemplateCondition { diff --git a/src/panels/config/automation/condition/types/ha-automation-condition-time.ts b/src/panels/config/automation/condition/types/ha-automation-condition-time.ts index b33c2bbd72bb..f9398b197de3 100644 --- a/src/panels/config/automation/condition/types/ha-automation-condition-time.ts +++ b/src/panels/config/automation/condition/types/ha-automation-condition-time.ts @@ -160,7 +160,7 @@ export class HaTimeCondition extends LitElement implements ConditionElement { > @@ -186,7 +186,15 @@ export class HaTimeCondition extends LitElement implements ConditionElement { private _dayValueChanged(ev: CustomEvent): void { const daySwitch = ev.currentTarget as WeekdayHaSwitch; - let days = this.condition.weekday || Object.keys(DAYS); + let days: string[]; + + if (!this.condition.weekday) { + days = Object.keys(DAYS); + } else { + days = !Array.isArray(this.condition.weekday) + ? [this.condition.weekday] + : this.condition.weekday; + } if (daySwitch.checked) { days.push(daySwitch.day);