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 date and datetime conditions #22298

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions src/data/automation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,27 @@ export interface ZoneCondition extends BaseCondition {

type Weekday = "sun" | "mon" | "tue" | "wed" | "thu" | "fri" | "sat";

export interface DateCondition extends BaseCondition {
condition: "date";
after?: string;
before?: string;
weekday?: Weekday | Weekday[];
}

export interface TimeCondition extends BaseCondition {
condition: "time";
after?: string;
before?: string;
weekday?: Weekday | Weekday[];
}

export interface DatetimeCondition extends BaseCondition {
condition: "datetime";
after?: string;
before?: string;
weekday?: Weekday | Weekday[];
}

export interface TemplateCondition extends BaseCondition {
condition: "template";
value_template: string;
Expand Down Expand Up @@ -300,7 +314,9 @@ export type Condition =
| NumericStateCondition
| SunCondition
| ZoneCondition
| DateCondition
| TimeCondition
| DatetimeCondition
| TemplateCondition
| DeviceCondition
| LogicalCondition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { expandConditionWithShorthand } from "../../../../data/automation";
import { haStyle } from "../../../../resources/styles";
import type { HomeAssistant } from "../../../../types";
import "./types/ha-automation-condition-and";
import "./types/ha-automation-condition-date";
import "./types/ha-automation-condition-datetime";
import "./types/ha-automation-condition-device";
import "./types/ha-automation-condition-not";
import "./types/ha-automation-condition-numeric_state";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
import { html, LitElement } from "lit";
import { customElement, property, state } from "lit/decorators";
import memoizeOne from "memoize-one";
import { firstWeekdayIndex } from "../../../../../common/datetime/first_weekday";
import { fireEvent } from "../../../../../common/dom/fire_event";
import type { LocalizeFunc } from "../../../../../common/translations/localize";
import "../../../../../components/ha-form/ha-form";
import type { SchemaUnion } from "../../../../../components/ha-form/types";
import type { DateCondition } from "../../../../../data/automation";
import type { FrontendLocaleData } from "../../../../../data/translation";
import type { HomeAssistant } from "../../../../../types";
import type { ConditionElement } from "../ha-automation-condition-row";

const DAYS = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"] as const;

@customElement("ha-automation-condition-date")
export class HaDateCondition extends LitElement implements ConditionElement {
@property({ attribute: false }) public hass!: HomeAssistant;

@property({ attribute: false }) public condition!: DateCondition;

@state() private _inputModeBefore?: boolean;

@state() private _inputModeAfter?: boolean;

@property({ type: Boolean }) public disabled = false;

public static get defaultConfig(): DateCondition {
return { condition: "date" };
}

private _schema = memoizeOne(
(
localize: LocalizeFunc,
locale: FrontendLocaleData,
inputModeAfter?: boolean,
inputModeBefore?: boolean
) => {
const dayIndex = firstWeekdayIndex(locale);
const sortedDays = DAYS.slice(dayIndex, DAYS.length).concat(
DAYS.slice(0, dayIndex)
);
return [
{
name: "mode_after",
type: "select",
required: true,
options: [
[
"value",
localize(
"ui.panel.config.automation.editor.conditions.type.date.type_value"
),
],
[
"input",
localize(
"ui.panel.config.automation.editor.conditions.type.date.type_input"
),
],
],
},
{
name: "after",
selector: inputModeAfter
? {
entity: {
filter: [
{ domain: "input_datetime" },
{ domain: "sensor", device_class: "timestamp" },
],
},
}
: { date: {} },
},
{
name: "mode_before",
type: "select",
required: true,
options: [
[
"value",
localize(
"ui.panel.config.automation.editor.conditions.type.date.type_value"
),
],
[
"input",
localize(
"ui.panel.config.automation.editor.conditions.type.date.type_input"
),
],
],
},
{
name: "before",
selector: inputModeBefore
? {
entity: {
filter: [
{ domain: "input_datetime" },
{ domain: "sensor", device_class: "timestamp" },
],
},
}
: { date: {} },
},
{
type: "multi_select",
name: "weekday",
options: sortedDays.map(
(day) =>
[
day,
localize(
`ui.panel.config.automation.editor.conditions.type.date.weekdays.${day}`
),
] as const
),
},
] as const;
}
);

protected render() {
const inputModeBefore =
this._inputModeBefore ??
(this.condition.before?.startsWith("input_datetime.") ||
this.condition.before?.startsWith("sensor."));
const inputModeAfter =
this._inputModeAfter ??
(this.condition.after?.startsWith("input_datetime.") ||
this.condition.after?.startsWith("sensor."));

const schema = this._schema(
this.hass.localize,
this.hass.locale,
inputModeAfter,
inputModeBefore
);

const data = {
mode_before: inputModeBefore ? "input" : "value",
mode_after: inputModeAfter ? "input" : "value",
...this.condition,
};

return html`
<ha-form
.hass=${this.hass}
.data=${data}
.schema=${schema}
.disabled=${this.disabled}
@value-changed=${this._valueChanged}
.computeLabel=${this._computeLabelCallback}
></ha-form>
`;
}

private _valueChanged(ev: CustomEvent): void {
ev.stopPropagation();
const newValue = ev.detail.value;

this._inputModeAfter = newValue.mode_after === "input";
this._inputModeBefore = newValue.mode_before === "input";

delete newValue.mode_after;
delete newValue.mode_before;

Object.keys(newValue).forEach((key) =>
newValue[key] === undefined ||
newValue[key] === "" ||
(Array.isArray(newValue[key]) && newValue[key].length === 0)
? delete newValue[key]
: {}
);

fireEvent(this, "value-changed", { value: newValue });
}

private _computeLabelCallback = (
schema: SchemaUnion<ReturnType<typeof this._schema>>
): string =>
this.hass.localize(
`ui.panel.config.automation.editor.conditions.type.date.${schema.name}`
);
}

declare global {
interface HTMLElementTagNameMap {
"ha-automation-condition-date": HaDateCondition;
}
}
Loading