mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-26 18:56:39 +00:00
Add weekday to time contidion editor (#6848)
This commit is contained in:
parent
e9141d82f3
commit
dfb2a7153b
@ -168,8 +168,9 @@ export interface ZoneCondition {
|
|||||||
|
|
||||||
export interface TimeCondition {
|
export interface TimeCondition {
|
||||||
condition: "time";
|
condition: "time";
|
||||||
after: string;
|
after?: string;
|
||||||
before: string;
|
before?: string;
|
||||||
|
weekday?: string | string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface TemplateCondition {
|
export interface TemplateCondition {
|
||||||
|
@ -6,6 +6,8 @@ import {
|
|||||||
internalProperty,
|
internalProperty,
|
||||||
LitElement,
|
LitElement,
|
||||||
property,
|
property,
|
||||||
|
CSSResult,
|
||||||
|
css,
|
||||||
} from "lit-element";
|
} from "lit-element";
|
||||||
import "../../../../../components/ha-formfield";
|
import "../../../../../components/ha-formfield";
|
||||||
import "../../../../../components/ha-radio";
|
import "../../../../../components/ha-radio";
|
||||||
@ -15,14 +17,31 @@ import {
|
|||||||
ConditionElement,
|
ConditionElement,
|
||||||
handleChangeEvent,
|
handleChangeEvent,
|
||||||
} from "../ha-automation-condition-row";
|
} 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 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")
|
@customElement("ha-automation-condition-time")
|
||||||
export class HaTimeCondition extends LitElement implements ConditionElement {
|
export class HaTimeCondition extends LitElement implements ConditionElement {
|
||||||
@property({ attribute: false }) public hass!: HomeAssistant;
|
@property({ attribute: false }) public hass!: HomeAssistant;
|
||||||
|
|
||||||
@property() public condition!: TimeCondition;
|
@property({ attribute: false }) public condition!: TimeCondition;
|
||||||
|
|
||||||
@internalProperty() private _inputModeBefore?: boolean;
|
@internalProperty() private _inputModeBefore?: boolean;
|
||||||
|
|
||||||
@ -33,7 +52,7 @@ export class HaTimeCondition extends LitElement implements ConditionElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected render() {
|
protected render() {
|
||||||
const { after, before } = this.condition;
|
const { after, before, weekday } = this.condition;
|
||||||
|
|
||||||
const inputModeBefore =
|
const inputModeBefore =
|
||||||
this._inputModeBefore ?? before?.startsWith("input_datetime.");
|
this._inputModeBefore ?? before?.startsWith("input_datetime.");
|
||||||
@ -128,6 +147,26 @@ export class HaTimeCondition extends LitElement implements ConditionElement {
|
|||||||
.value=${before?.startsWith("input_datetime.") ? "" : before}
|
.value=${before?.startsWith("input_datetime.") ? "" : before}
|
||||||
@value-changed=${this._valueChanged}
|
@value-changed=${this._valueChanged}
|
||||||
></paper-input>`}
|
></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>
|
||||||
|
`
|
||||||
|
)}
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,4 +182,45 @@ export class HaTimeCondition extends LitElement implements ConditionElement {
|
|||||||
private _valueChanged(ev: CustomEvent): void {
|
private _valueChanged(ev: CustomEvent): void {
|
||||||
handleChangeEvent(this, ev);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1139,7 +1139,16 @@
|
|||||||
"type_input": "[%key:ui::panel::config::automation::editor::triggers::type::time::type_input%]",
|
"type_input": "[%key:ui::panel::config::automation::editor::triggers::type::time::type_input%]",
|
||||||
"label": "[%key:ui::panel::config::automation::editor::triggers::type::time::label%]",
|
"label": "[%key:ui::panel::config::automation::editor::triggers::type::time::label%]",
|
||||||
"after": "After",
|
"after": "After",
|
||||||
"before": "Before"
|
"before": "Before",
|
||||||
|
"weekdays": {
|
||||||
|
"mon": "Monday",
|
||||||
|
"tue": "Tuesday",
|
||||||
|
"wed": "Wednesday",
|
||||||
|
"thu": "Thursday",
|
||||||
|
"fri": "Friday",
|
||||||
|
"sat": "Saturday",
|
||||||
|
"sun": "Sunday"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"zone": {
|
"zone": {
|
||||||
"label": "[%key:ui::panel::config::automation::editor::triggers::type::zone::label%]",
|
"label": "[%key:ui::panel::config::automation::editor::triggers::type::zone::label%]",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user