diff --git a/src/widgets/olivetin/component.jsx b/src/widgets/olivetin/component.jsx new file mode 100644 index 000000000..aa6f31b33 --- /dev/null +++ b/src/widgets/olivetin/component.jsx @@ -0,0 +1,60 @@ +import { useTranslation } from "next-i18next"; + +import Container from "components/services/widget/container"; +import Block from "components/services/widget/block"; +import useWidgetAPI from "utils/proxy/use-widget-api"; + +export default function Component({ service }) { + const { widget } = service; + const { t } = useTranslation(); + + const { data: upsData, error: upsError } = useWidgetAPI(widget, "devices"); + + if (upsError) { + return ; + } + + if (!upsData) { + return ( + + + + + + ); + } + + // backwards compatibility with peanut v1 + if ("battery.charge" in upsData) { + upsData.battery_charge = upsData["battery.charge"]; + } + if ("ups.load" in upsData) { + upsData.ups_load = upsData["ups.load"]; + } + if ("ups.status" in upsData) { + upsData.ups_status = upsData["ups.status"]; + } + + let status; + switch (upsData.ups_status) { + case "OL": + status = t("peanut.online"); + break; + case "OB": + status = t("peanut.on_battery"); + break; + case "LB": + status = t("peanut.low_battery"); + break; + default: + status = upsData.ups_status; + } + + return ( + + + + + + ); +} diff --git a/src/widgets/olivetin/widget.js b/src/widgets/olivetin/widget.js new file mode 100644 index 000000000..2697dfb13 --- /dev/null +++ b/src/widgets/olivetin/widget.js @@ -0,0 +1,14 @@ +import genericProxyHandler from "utils/proxy/handlers/generic"; + +const widget = { + api: "{url}/api/v1/{endpoint}/{key}", + proxyHandler: genericProxyHandler, + + mappings: { + devices: { + endpoint: "devices", + }, + }, +}; + +export default widget;