1+ import { HomeAssistant , fireEvent , forwardHaptic , navigate , toggleEntity } from "custom-card-helpers" ;
2+ import { ActionConfigExtended } from "./types" ;
3+
4+ interface ToastActionParams {
5+ action : ( ) => void ;
6+ text : string ;
7+ }
8+ interface ShowToastParams {
9+ message : string ;
10+ action ?: ToastActionParams ;
11+ duration ?: number ;
12+ dismissable ?: boolean ;
13+ }
14+ const showToast = ( el : HTMLElement , params : ShowToastParams ) =>
15+ fireEvent ( el , "hass-notification" , params ) ;
16+
17+ export const handleAction = async (
18+ node : HTMLElement ,
19+ hass : HomeAssistant ,
20+ config : {
21+ entity_id : string ;
22+ hold_action ?: ActionConfigExtended ;
23+ tap_action ?: ActionConfigExtended ;
24+ double_tap_action ?: ActionConfigExtended ;
25+ } ,
26+ action : string
27+ ) : Promise < void > => {
28+ let actionConfig = config . tap_action ;
29+
30+ if ( action === "double_tap" && config . double_tap_action ) {
31+ actionConfig = config . double_tap_action ;
32+ } else if ( action === "hold" && config . hold_action ) {
33+ actionConfig = config . hold_action ;
34+ }
35+
36+ if ( ! actionConfig ) {
37+ actionConfig = {
38+ action : "more-info" ,
39+ } ;
40+ }
41+
42+ if (
43+ actionConfig . confirmation &&
44+ ( ! actionConfig . confirmation . exemptions ||
45+ ! actionConfig . confirmation . exemptions . some ( ( e ) => e . user === hass ! . user ! . id ) )
46+ ) {
47+ forwardHaptic ( "warning" ) ;
48+
49+ if (
50+ ! confirm (
51+ actionConfig . confirmation . text ||
52+ hass . localize (
53+ "ui.panel.lovelace.cards.actions.action_confirmation" ,
54+ "action" ,
55+ hass . localize (
56+ "ui.panel.lovelace.editor.action-editor.actions." +
57+ actionConfig . action
58+ ) ||
59+ actionConfig . action
60+ )
61+ )
62+ ) {
63+ return ;
64+ }
65+ }
66+
67+ switch ( actionConfig . action ) {
68+ case "more-info" : {
69+ fireEvent ( node , "hass-more-info" , {
70+ // @ts -ignore
71+ entityId : actionConfig . entity ?? actionConfig . data ?. entity_id ?? config . entity_id ,
72+ } ) ;
73+ break ;
74+ }
75+ case "navigate" :
76+ if ( actionConfig . navigation_path ) {
77+ navigate ( node , actionConfig . navigation_path ) ;
78+ } else {
79+ showToast ( node , {
80+ message : hass . localize ( "ui.panel.lovelace.cards.actions.no_navigation_path" ) ,
81+ } ) ;
82+ forwardHaptic ( "failure" ) ;
83+ }
84+ break ;
85+ case "url" : {
86+ if ( actionConfig . url_path ) {
87+ window . open ( actionConfig . url_path ) ;
88+ } else {
89+ showToast ( node , {
90+ message : hass . localize ( "ui.panel.lovelace.cards.actions.no_url" ) ,
91+ } ) ;
92+ forwardHaptic ( "failure" ) ;
93+ }
94+ break ;
95+ }
96+ case "toggle" : {
97+ toggleEntity ( hass , config . entity_id ) ;
98+ forwardHaptic ( "light" ) ;
99+ break ;
100+ }
101+ case "call-service" : {
102+ if ( ! actionConfig . service ) {
103+ showToast ( node , {
104+ message : hass . localize ( "ui.panel.lovelace.cards.actions.no_service" ) ,
105+ } ) ;
106+ forwardHaptic ( "failure" ) ;
107+ return ;
108+ }
109+ const [ domain , service ] = actionConfig . service . split ( "." , 2 ) ;
110+ hass . callService (
111+ domain ,
112+ service ,
113+ // @ts -ignore
114+ actionConfig . data ?? actionConfig . service_data ,
115+ actionConfig . target ,
116+ ) ;
117+ forwardHaptic ( "light" ) ;
118+ break ;
119+ }
120+ case "fire-dom-event" : {
121+ fireEvent ( node , "ll-custom" , actionConfig ) ;
122+ }
123+ }
124+ } ;
125+
126+ declare global {
127+ interface HASSDomEvents {
128+ "hass-notification" : ShowToastParams ;
129+ }
130+ }
0 commit comments