-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathhandle-action.ts
More file actions
161 lines (153 loc) · 4.53 KB
/
Copy pathhandle-action.ts
File metadata and controls
161 lines (153 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { fireEvent } from "../../../common/dom/fire_event";
import { navigate } from "../../../common/navigate";
import { forwardHaptic } from "../../../data/haptics";
import type { ActionConfig } from "../../../data/lovelace/config/action";
import { showConfirmationDialog } from "../../../dialogs/generic/show-dialog-box";
import { showVoiceCommandDialog } from "../../../dialogs/voice-command-dialog/show-ha-voice-command-dialog";
import type { HomeAssistant } from "../../../types";
import { showToast } from "../../../util/toast";
import { getConfirmationDefaultText } from "./confirmation-default-text";
import { toggleEntity } from "./entity/toggle-entity";
declare global {
interface HASSDomEvents {
"ll-custom": ActionConfig;
}
}
export interface ActionConfigParams {
entity?: string;
camera_image?: string;
image_entity?: string;
hold_action?: ActionConfig;
tap_action?: ActionConfig;
double_tap_action?: ActionConfig;
}
export const handleAction = async (
node: HTMLElement,
hass: HomeAssistant,
config: ActionConfigParams,
action: string
): Promise<void> => {
let actionConfig: ActionConfig | undefined;
if (action === "double_tap" && config.double_tap_action) {
actionConfig = config.double_tap_action;
} else if (action === "hold" && config.hold_action) {
actionConfig = config.hold_action;
} else if (action === "tap" && config.tap_action) {
actionConfig = config.tap_action;
}
if (!actionConfig) {
actionConfig = {
action: "more-info",
};
}
if (
actionConfig.confirmation &&
(!actionConfig.confirmation.exemptions ||
!actionConfig.confirmation.exemptions.some(
(e) => e.user === hass!.user?.id
))
) {
forwardHaptic(node, "warning");
if (
!(await showConfirmationDialog(node, {
text:
actionConfig.confirmation.text ||
(await getConfirmationDefaultText(hass, actionConfig)),
title: actionConfig.confirmation.title,
dismissText: actionConfig.confirmation.dismiss_text,
confirmText: actionConfig.confirmation.confirm_text,
}))
) {
return;
}
}
switch (actionConfig.action) {
case "more-info": {
const entityId =
actionConfig.entity ||
config.entity ||
config.camera_image ||
config.image_entity;
if (entityId) {
fireEvent(node, "hass-more-info", { entityId });
} else {
showToast(node, {
message: hass.localize(
"ui.panel.lovelace.cards.actions.no_entity_more_info"
),
});
forwardHaptic(node, "failure");
}
break;
}
case "navigate":
if (actionConfig.navigation_path) {
navigate(actionConfig.navigation_path, {
replace: actionConfig.navigation_replace,
});
} else {
showToast(node, {
message: hass.localize(
"ui.panel.lovelace.cards.actions.no_navigation_path"
),
});
forwardHaptic(node, "failure");
}
break;
case "url": {
if (actionConfig.url_path) {
window.open(actionConfig.url_path);
} else {
showToast(node, {
message: hass.localize("ui.panel.lovelace.cards.actions.no_url"),
});
forwardHaptic(node, "failure");
}
break;
}
case "toggle": {
if (config.entity) {
toggleEntity(hass, config.entity!);
forwardHaptic(node, "light");
} else {
showToast(node, {
message: hass.localize(
"ui.panel.lovelace.cards.actions.no_entity_toggle"
),
});
forwardHaptic(node, "failure");
}
break;
}
case "perform-action":
case "call-service": {
if (!actionConfig.perform_action && !actionConfig.service) {
showToast(node, {
message: hass.localize("ui.panel.lovelace.cards.actions.no_action"),
});
forwardHaptic(node, "failure");
return;
}
const [domain, service] = (actionConfig.perform_action ||
actionConfig.service)!.split(".", 2);
hass.callService(
domain,
service,
actionConfig.data ?? actionConfig.service_data,
actionConfig.target
);
forwardHaptic(node, "light");
break;
}
case "assist": {
showVoiceCommandDialog(node, hass, {
start_listening: actionConfig.start_listening ?? false,
pipeline_id: actionConfig.pipeline_id ?? "last_used",
});
break;
}
case "fire-dom-event": {
fireEvent(node, "ll-custom", actionConfig);
}
}
};