From dfb2a7153baf654496f49972e86650e1eece15ae Mon Sep 17 00:00:00 2001 From: Tomasz Date: Mon, 28 Sep 2020 12:28:43 +0200 Subject: [PATCH] Add weekday to time contidion editor (#6848) --- src/data/automation.ts | 5 +- .../types/ha-automation-condition-time.ts | 84 ++++++++++++++++++- src/translations/en.json | 11 ++- 3 files changed, 95 insertions(+), 5 deletions(-) diff --git a/src/data/automation.ts b/src/data/automation.ts index 7ef285e1e7..4b06d4af63 100644 --- a/src/data/automation.ts +++ b/src/data/automation.ts @@ -168,8 +168,9 @@ export interface ZoneCondition { export interface TimeCondition { condition: "time"; - after: string; - before: 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 baac37cf35..f9398b197d 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,45 @@ export class HaTimeCondition extends LitElement implements ConditionElement { private _valueChanged(ev: CustomEvent): void { handleChangeEvent(this, ev); } + + private _dayValueChanged(ev: CustomEvent): void { + const daySwitch = ev.currentTarget as WeekdayHaSwitch; + + 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); + } else { + days = days.filter((d) => d !== daySwitch.day); + } + + days.sort((a: string, b: string) => DAYS[a] - DAYS[b]); + + fireEvent(this, "value-changed", { + value: { ...this.condition, weekday: days }, + }); + } + + 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 a253639cf8..6cc4b4cfff 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%]",