forked from bernikr/lovelace-notify-card
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlovelace-multiselect-notify-card.js
104 lines (91 loc) · 3.82 KB
/
lovelace-multiselect-notify-card.js
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
class NotifyCard extends HTMLElement {
setConfig(config) {
if (!config.targets) {
throw new Error('You need to define targets with labels');
}
this.config = config;
this.targets = this.config.targets;
this.render();
}
render() {
if (!this.content) {
this.card = document.createElement('ha-card');
this.content = document.createElement('div');
this.content.style.padding = '0 16px 16px';
this.card.appendChild(this.content);
this.appendChild(this.card);
}
this.card.header = this.config.card_title ?? "Send Notification";
this.card.style.padding = '16px';
this.card.style.boxShadow = '0 4px 8px rgba(0,0,0,0.1)';
this.content.style.display = 'flex';
this.content.style.flexDirection = 'column';
this.content.innerHTML = "";
// Checkboxes for targets
const targetsContainer = document.createElement('div');
targetsContainer.style.marginBottom = '16px';
this.targets.forEach(targetObj => {
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.id = targetObj.entity;
checkbox.name = 'notification_target';
checkbox.value = targetObj.entity;
const label = document.createElement('label');
label.htmlFor = targetObj.entity;
label.textContent = targetObj.label || targetObj.entity;
label.style.fontSize = '1.1em'; // Slightly increased font size
label.style.marginLeft = '8px';
const container = document.createElement('div');
container.appendChild(checkbox);
container.appendChild(label);
targetsContainer.appendChild(container);
});
this.content.appendChild(targetsContainer);
// Editable notification title field
let notificationTitle = this.config.notification_title ?? "Home Assistant Notification";
this.content.innerHTML += `
<div style="margin-bottom: 16px;">
<label for="notification_title">Notification Title:</label>
<input type="text" id="notification_title" name="notification_title" value="${notificationTitle}" style="width: 100%;">
</div>
`;
// Notification text area
let labelText = this.config.label ?? "Notification Text";
this.content.innerHTML += `
<div style="display: flex; margin-bottom: 16px;">
<textarea id="notification_text" style="flex-grow: 1" placeholder="${labelText}"></textarea>
</div>
`;
// Send button
this.content.innerHTML += `<button id="send_button">Send</button>`;
const sendButton = this.content.querySelector("#send_button");
sendButton.style.backgroundColor = '#0d6efd';
sendButton.style.color = 'white';
sendButton.style.border = 'none';
sendButton.style.padding = '10px 20px';
sendButton.style.borderRadius = '5px';
sendButton.style.cursor = 'pointer';
sendButton.onmouseover = () => sendButton.style.backgroundColor = '#0b5ed7';
sendButton.onmouseout = () => sendButton.style.backgroundColor = '#0d6efd';
this.content.querySelector("#send_button").addEventListener("click", this.send.bind(this), false);
this.content.querySelector("#notification_text").addEventListener("keydown", this.keydown.bind(this), false);
}
send() {
let msg = this.content.querySelector("#notification_text").value;
let title = this.content.querySelector("#notification_title").value;
let selectedTargets = Array.from(this.content.querySelectorAll('input[name="notification_target"]:checked')).map(checkbox => checkbox.value);
selectedTargets.forEach(target => {
this.hass.callService('notify', target, {
message: msg,
title: title
});
});
this.content.querySelector("#notification_text").value = "";
}
keydown(e) {
if (e.code == "Enter" && e.ctrlKey) {
this.send();
}
}
}
customElements.define('lovelace-multiselect-notify-card', NotifyCard);