forked from home-assistant/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
persistent_notification.ts
43 lines (38 loc) · 973 Bytes
/
persistent_notification.ts
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
import {
Connection,
createCollection,
HassEntity,
} from "home-assistant-js-websocket";
export interface PersitentNotificationEntity extends HassEntity {
notification_id?: string;
created_at?: string;
title?: string;
message?: string;
}
export interface PersistentNotification {
created_at: string;
message: string;
notification_id: string;
title: string;
status: "read" | "unread";
}
const fetchNotifications = (conn) =>
conn.sendMessagePromise({
type: "persistent_notification/get",
});
const subscribeUpdates = (conn, store) =>
conn.subscribeEvents(
() => fetchNotifications(conn).then((ntf) => store.setState(ntf, true)),
"persistent_notifications_updated"
);
export const subscribeNotifications = (
conn: Connection,
onChange: (notifications: PersistentNotification[]) => void
) =>
createCollection<PersistentNotification[]>(
"_ntf",
fetchNotifications,
subscribeUpdates,
conn,
onChange
);