Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add weekday to time contidion editor #6848

Merged
merged 6 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/data/automation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
internalProperty,
LitElement,
property,
CSSResult,
css,
} from "lit-element";
import "../../../../../components/ha-formfield";
import "../../../../../components/ha-radio";
Expand All @@ -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;

Expand All @@ -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.");
Expand Down Expand Up @@ -128,6 +147,26 @@ export class HaTimeCondition extends LitElement implements ConditionElement {
.value=${before?.startsWith("input_datetime.") ? "" : before}
@value-changed=${this._valueChanged}
></paper-input>`}
${Object.keys(DAYS).map(
(day) => html`
<ha-formfield
alignEnd
spaceBetween
class="weekday-toggle"
.label=${this.hass!.localize(
`ui.panel.config.automation.editor.conditions.type.time.weekdays.${day}`
)}
.dir=${computeRTLDirection(this.hass!)}
>
<ha-switch
.day=${day}
.checked=${!weekday || weekday === day || weekday.includes(day)}
@change=${this._dayValueChanged}
>
</ha-switch>
</ha-formfield>
`
)}
`;
}

Expand All @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we set the height to 40px? What is wrong with the default styling?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks really bad without those 40px:

1403c6bcebf56314e31d3a1dea595480

}
`;
}
}

declare global {
interface HTMLElementTagNameMap {
"ha-automation-condition-time": HaTimeCondition;
}
}
11 changes: 10 additions & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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%]",
Expand Down