1- import { LitElement , nothing } from "lit" ;
1+ import { html , LitElement } from "lit" ;
22import { customElement , property } from "lit/decorators" ;
3+ import {
4+ literal ,
5+ array ,
6+ object ,
7+ optional ,
8+ string ,
9+ assert ,
10+ enums ,
11+ } from "superstruct" ;
312import type { HomeAssistant } from "../../../../../types" ;
413import type { TimeCondition } from "../../../common/validate-condition" ;
14+ import { WEEKDAYS_SHORT } from "../../../../../common/datetime/weekday" ;
15+ import type { HaFormSchema } from "../../../../../components/ha-form/types" ;
16+ import { fireEvent } from "../../../../../common/dom/fire_event" ;
17+
18+ const timeConditionStruct = object ( {
19+ condition : literal ( "time" ) ,
20+ after : optional ( string ( ) ) ,
21+ before : optional ( string ( ) ) ,
22+ weekdays : optional ( array ( enums ( WEEKDAYS_SHORT ) ) ) ,
23+ } ) ;
24+
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 [ ] ;
539
640@customElement ( "ha-card-condition-time" )
741export class HaCardConditionTime extends LitElement {
@@ -15,8 +49,26 @@ export class HaCardConditionTime extends LitElement {
1549 return { condition : "time" } ;
1650 }
1751
52+ protected static validateUIConfig ( condition : TimeCondition ) {
53+ return assert ( condition , timeConditionStruct ) ;
54+ }
55+
1856 protected render ( ) {
19- return nothing ;
57+ return html `
58+ < ha-form
59+ .hass =${ this . hass }
60+ .data =${ this . condition }
61+ .schema=${ SCHEMA }
62+ .disabled=${ this . disabled }
63+ @value-changed=${ this . _valueChanged }
64+ > </ ha-form >
65+ ` ;
66+ }
67+
68+ private _valueChanged ( ev : CustomEvent ) {
69+ ev . stopPropagation ( ) ;
70+ const data = ev . detail . value as TimeCondition ;
71+ fireEvent ( this , "value-changed" , { value : data } ) ;
2072 }
2173}
2274
0 commit comments