1
1
import { HassEntity } from "home-assistant-js-websocket" ;
2
2
import { css , CSSResultGroup , html , nothing , PropertyValues , TemplateResult } from "lit" ;
3
- import { customElement , query } from "lit/decorators.js" ;
3
+ import { customElement } from "lit/decorators.js" ;
4
4
import { classMap } from "lit/directives/class-map.js" ;
5
5
import { styleMap } from "lit/directives/style-map.js" ;
6
6
import {
@@ -12,8 +12,8 @@ import {
12
12
HomeAssistant ,
13
13
LovelaceCard ,
14
14
LovelaceCardEditor ,
15
- LovelaceLayoutOptions ,
16
15
} from "../../ha" ;
16
+ import { ALARM_MODES , AlarmMode , setProtectedAlarmControlPanelMode } from "../../ha/data/alarm_control_panel" ;
17
17
import "../../shared/badge-icon" ;
18
18
import "../../shared/button" ;
19
19
import "../../shared/button-group" ;
@@ -25,22 +25,14 @@ import { computeAppearance } from "../../utils/appearance";
25
25
import { MushroomBaseCard } from "../../utils/base-card" ;
26
26
import { cardStyle } from "../../utils/card-styles" ;
27
27
import { registerCustomCard } from "../../utils/custom-cards" ;
28
- import { alarmPanelIconAction } from "../../utils/icons/alarm-panel-icon" ;
29
28
import { computeEntityPicture } from "../../utils/info" ;
30
29
import { AlarmControlPanelCardConfig } from "./alarm-control-panel-card-config" ;
31
30
import {
32
31
ALARM_CONTROl_PANEL_CARD_EDITOR_NAME ,
33
32
ALARM_CONTROl_PANEL_CARD_NAME ,
34
33
ALARM_CONTROl_PANEL_ENTITY_DOMAINS ,
35
34
} from "./const" ;
36
- import {
37
- getStateColor ,
38
- getStateService ,
39
- hasCode ,
40
- isActionsAvailable ,
41
- isDisarmed ,
42
- shouldPulse ,
43
- } from "./utils" ;
35
+ import { getStateColor , hasCode , isActionsAvailable , isDisarmed , shouldPulse } from "./utils" ;
44
36
45
37
registerCustomCard ( {
46
38
type : ALARM_CONTROl_PANEL_CARD_NAME ,
@@ -49,14 +41,10 @@ registerCustomCard({
49
41
} ) ;
50
42
51
43
type ActionButtonType = {
52
- state : string ;
44
+ mode : AlarmMode ;
53
45
disabled ?: boolean ;
54
46
} ;
55
47
56
- type HaTextField = any ;
57
-
58
- const BUTTONS = [ "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "" , "0" , "clear" ] ;
59
-
60
48
/*
61
49
* Ref: https://github.com/home-assistant/frontend/blob/dev/src/panels/lovelace/cards/hui-alarm-panel-card.ts
62
50
* TODO: customize icon for modes (advanced YAML configuration)
@@ -88,68 +76,15 @@ export class AlarmControlPanelCard
88
76
return Boolean ( this . _config ?. states ?. length ) ;
89
77
}
90
78
91
- public getLayoutOptions ( ) : LovelaceLayoutOptions {
92
- const options = super . getLayoutOptions ( ) ;
93
- if ( this . _config ?. show_keypad ) {
94
- delete options . grid_columns ;
95
- delete options . grid_rows ;
96
- }
97
- return options ;
98
- }
99
-
100
- @query ( "#alarmCode" ) private _input ?: HaTextField ;
101
-
102
- setConfig ( config : AlarmControlPanelCardConfig ) : void {
103
- super . setConfig ( config ) ;
104
- this . loadComponents ( ) ;
105
- }
106
-
107
- protected updated ( changedProperties : PropertyValues ) {
108
- super . updated ( changedProperties ) ;
109
- if ( this . hass && changedProperties . has ( "hass" ) ) {
110
- this . loadComponents ( ) ;
111
- }
112
- }
113
-
114
- async loadComponents ( ) {
115
- const stateObj = this . _stateObj ;
116
-
117
- if ( stateObj && hasCode ( stateObj ) ) {
118
- void import ( "../../shared/form/mushroom-textfield" ) ;
119
- }
120
- }
121
-
122
- private _onTap ( e : MouseEvent , state : string ) : void {
123
- const service = getStateService ( state ) ;
124
- if ( ! service ) return ;
79
+ private _onTap ( e : MouseEvent , mode : AlarmMode ) : void {
125
80
e . stopPropagation ( ) ;
126
- const code = this . _input ?. value || undefined ;
127
- this . hass . callService ( "alarm_control_panel" , service , {
128
- entity_id : this . _config ?. entity ,
129
- code,
130
- } ) ;
131
- if ( this . _input ) {
132
- this . _input . value = "" ;
133
- }
134
- }
135
-
136
- private _handlePadClick ( e : MouseEvent ) : void {
137
- const val = ( e . currentTarget ! as any ) . value ;
138
- if ( this . _input ) {
139
- this . _input . value = val === "clear" ? "" : this . _input ! . value + val ;
140
- }
81
+ setProtectedAlarmControlPanelMode ( this , this . hass ! , this . _stateObj ! , mode ) ;
141
82
}
142
83
143
84
private _handleAction ( ev : ActionHandlerEvent ) {
144
85
handleAction ( this , this . hass ! , this . _config ! , ev . detail . action ! ) ;
145
86
}
146
87
147
- private get _hasCode ( ) : boolean {
148
- const stateObj = this . _stateObj ;
149
- if ( ! stateObj ) return false ;
150
- return hasCode ( stateObj ) && Boolean ( this . _config ?. show_keypad ) ;
151
- }
152
-
153
88
protected render ( ) {
154
89
if ( ! this . hass || ! this . _config || ! this . _config . entity ) {
155
90
return nothing ;
@@ -169,8 +104,8 @@ export class AlarmControlPanelCard
169
104
const actions : ActionButtonType [ ] =
170
105
this . _config . states && this . _config . states . length > 0
171
106
? isDisarmed ( stateObj )
172
- ? this . _config . states . map ( ( state ) => ( { state } ) )
173
- : [ { state : "disarmed" } ]
107
+ ? this . _config . states . map ( ( state ) => ( { mode : state } ) )
108
+ : [ { mode : "disarmed" } ]
174
109
: [ ] ;
175
110
176
111
const isActionEnabled = isActionsAvailable ( stateObj ) ;
@@ -202,10 +137,10 @@ export class AlarmControlPanelCard
202
137
${ actions . map (
203
138
( action ) => html `
204
139
<mushroom- butto n
205
- @click = ${ ( e ) => this . _onTap ( e , action . state ) }
140
+ @click = ${ ( e ) => this . _onTap ( e , action . mode ) }
206
141
.disabled = ${ ! isActionEnabled }
207
142
>
208
- <ha- icon .icon = ${ alarmPanelIconAction ( action . state ) } >
143
+ <ha- icon .icon = ${ ALARM_MODES [ action . mode ] . icon } >
209
144
</ ha- icon>
210
145
</ mushroom- butto n>
211
146
`
@@ -214,44 +149,6 @@ export class AlarmControlPanelCard
214
149
`
215
150
: nothing }
216
151
</ mushroom- card>
217
- ${ ! this . _hasCode
218
- ? nothing
219
- : html `
220
- <mushroom- textfield
221
- id= "alarmCode"
222
- .label = ${ this . hass . localize ( "ui.card.alarm_control_panel.code" ) }
223
- type= "passwor d"
224
- .inputmode = ${ stateObj . attributes . code_format === "number"
225
- ? "numeric"
226
- : "text" }
227
- > </ mushroom- textfield>
228
- ` }
229
- ${ ! ( this . _hasCode && stateObj . attributes . code_format === "number" )
230
- ? nothing
231
- : html `
232
- <div id= "keypad" >
233
- ${ BUTTONS . map ( ( value ) =>
234
- value === ""
235
- ? html `<mwc- butto n dis abled> </ mwc- butto n> `
236
- : html `
237
- <mwc- butto n
238
- .value = ${ value }
239
- @click = ${ this . _handlePadClick }
240
- outlined
241
- class= ${ classMap ( {
242
- numberkey : value !== "clear" ,
243
- } ) }
244
- >
245
- ${ value === "clear"
246
- ? this . hass ! . localize (
247
- `ui.card.alarm_control_panel.clear_code`
248
- )
249
- : value }
250
- </ mwc- butto n>
251
- `
252
- ) }
253
- </ div>
254
- ` }
255
152
</ ha- card>
256
153
` ;
257
154
}
@@ -287,31 +184,9 @@ export class AlarmControlPanelCard
287
184
mushroom-state-item {
288
185
cursor : pointer;
289
186
}
290
- .alert {
291
- --main-color : var (--warning-color );
292
- }
293
187
mushroom-shape-icon .pulse {
294
188
--shape-animation : 1s ease 0s infinite normal none running pulse;
295
189
}
296
- mushroom-textfield {
297
- display : block;
298
- margin : 8px auto;
299
- max-width : 150px ;
300
- text-align : center;
301
- }
302
- # keypad {
303
- display : flex;
304
- justify-content : center;
305
- flex-wrap : wrap;
306
- margin : auto;
307
- width : 100% ;
308
- max-width : 300px ;
309
- }
310
- # keypad mwc-button {
311
- padding : 8px ;
312
- width : 30% ;
313
- box-sizing : border-box;
314
- }
315
190
` ,
316
191
] ;
317
192
}
0 commit comments