@@ -9,11 +9,19 @@ import {
99 assert ,
1010 enums ,
1111} from "superstruct" ;
12+ import memoizeOne from "memoize-one" ;
1213import type { HomeAssistant } from "../../../../../types" ;
14+ import type { LocalizeFunc } from "../../../../../common/translations/localize" ;
15+ import {
16+ WEEKDAY_SHORT_TO_LONG ,
17+ WEEKDAYS_SHORT ,
18+ } from "../../../../../common/datetime/weekday" ;
1319import type { TimeCondition } from "../../../common/validate-condition" ;
14- import { WEEKDAYS_SHORT } from "../../../../../common/datetime/weekday" ;
15- import type { HaFormSchema } from "../../../../../components/ha-form/types" ;
1620import { fireEvent } from "../../../../../common/dom/fire_event" ;
21+ import type {
22+ HaFormSchema ,
23+ SchemaUnion ,
24+ } from "../../../../../components/ha-form/types" ;
1725
1826const timeConditionStruct = object ( {
1927 condition : literal ( "time" ) ,
@@ -22,21 +30,6 @@ const timeConditionStruct = object({
2230 weekdays : optional ( array ( enums ( WEEKDAYS_SHORT ) ) ) ,
2331} ) ;
2432
25- const SCHEMA = [
26- { name : "after" , selector : { time : { } } } ,
27- { name : "before" , selector : { time : { } } } ,
28- {
29- name : "weekdays" ,
30- selector : {
31- select : {
32- mode : "list" ,
33- multiple : true ,
34- options : WEEKDAYS_SHORT ,
35- } ,
36- } ,
37- } ,
38- ] as const satisfies HaFormSchema [ ] ;
39-
4033@customElement ( "ha-card-condition-time" )
4134export class HaCardConditionTime extends LitElement {
4235 @property ( { attribute : false } ) public hass ! : HomeAssistant ;
@@ -53,12 +46,34 @@ export class HaCardConditionTime extends LitElement {
5346 return assert ( condition , timeConditionStruct ) ;
5447 }
5548
49+ private _schema = memoizeOne (
50+ ( localize : LocalizeFunc ) =>
51+ [
52+ { name : "after" , selector : { time : { } } } ,
53+ { name : "before" , selector : { time : { } } } ,
54+ {
55+ name : "weekdays" ,
56+ selector : {
57+ select : {
58+ mode : "list" ,
59+ multiple : true ,
60+ options : WEEKDAYS_SHORT . map ( ( day ) => ( {
61+ value : day ,
62+ label : localize ( `ui.weekdays.${ WEEKDAY_SHORT_TO_LONG [ day ] } ` ) ,
63+ } ) ) ,
64+ } ,
65+ } ,
66+ } ,
67+ ] as const satisfies HaFormSchema [ ]
68+ ) ;
69+
5670 protected render ( ) {
5771 return html `
5872 < ha-form
5973 .hass =${ this . hass }
6074 .data =${ this . condition }
61- .schema=${ SCHEMA }
75+ .computeLabel=${ this . _computeLabelCallback }
76+ .schema=${ this . _schema ( this . hass . localize ) }
6277 .disabled=${ this . disabled }
6378 @value-changed=${ this . _valueChanged }
6479 > </ ha-form >
@@ -70,6 +85,13 @@ export class HaCardConditionTime extends LitElement {
7085 const data = ev . detail . value as TimeCondition ;
7186 fireEvent ( this , "value-changed" , { value : data } ) ;
7287 }
88+
89+ private _computeLabelCallback = (
90+ schema : SchemaUnion < ReturnType < typeof this . _schema > >
91+ ) : string =>
92+ this . hass . localize (
93+ `ui.panel.lovelace.editor.condition-editor.condition.time.${ schema . name } `
94+ ) ;
7395}
7496
7597declare global {
0 commit comments