1
0
mirror of https://github.com/louislam/uptime-kuma.git synced 2024-12-24 22:14:47 +02:00
uptime-kuma/server/notification-providers/home-assistant.js
Frank Elsinga a9a1cf1353
Chore: General notification reformatting (#3182)
- I unified where in file the name of `NotificationProvider.name` is placed
- I made sure that all the providers adhere to the signature of `NotificationProvider.send()`
- I made sure that all the providers use `okMsg` if returning success messages directly from the function.
  Here a discussion should be had:
  Should this be refactored into a constant of `NotificationProvider`? I could imagine that `NotificationProvider.SENDING_SUCCESSFULL`  could be a suitable alternative.
- I made sure all providers have the URL they `POST`/`GET` to be extraced into a variable.
  => refactored this way due to Nelsons suggestion
2024-03-14 14:21:15 +01:00

44 lines
1.4 KiB
JavaScript

const NotificationProvider = require("./notification-provider");
const axios = require("axios");
const defaultNotificationService = "notify";
class HomeAssistant extends NotificationProvider {
name = "HomeAssistant";
/**
* @inheritdoc
*/
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
const okMsg = "Sent Successfully.";
const notificationService = notification?.notificationService || defaultNotificationService;
try {
await axios.post(
`${notification.homeAssistantUrl.trim().replace(/\/*$/, "")}/api/services/notify/${notificationService}`,
{
title: "Uptime Kuma",
message: msg,
...(notificationService !== "persistent_notification" && { data: {
name: monitorJSON?.name,
status: heartbeatJSON?.status,
} }),
},
{
headers: {
Authorization: `Bearer ${notification.longLivedAccessToken}`,
"Content-Type": "application/json",
},
}
);
return okMsg;
} catch (error) {
this.throwGeneralAxiosError(error);
}
}
}
module.exports = HomeAssistant;