added all evcc related files
This commit is contained in:
parent
27837c6db8
commit
e254a6a61b
@ -51,6 +51,13 @@
|
||||
"series": "Series",
|
||||
"episodes": "Episodes",
|
||||
"songs": "Songs"
|
||||
},
|
||||
"evcc": {
|
||||
"pvPower": "Erzeugung",
|
||||
"batterySoc": "Batterie",
|
||||
"gridpower": "Netz",
|
||||
"homepower": "Verbrauch",
|
||||
"chargePower": "Ladepunkt"
|
||||
},
|
||||
"tautulli": {
|
||||
"playing": "Spielen",
|
||||
|
||||
@ -91,6 +91,13 @@
|
||||
"series": "Series",
|
||||
"episodes": "Episodes",
|
||||
"songs": "Songs"
|
||||
},
|
||||
"evcc": {
|
||||
"pvPower": "Production",
|
||||
"batterySoc": "Battery",
|
||||
"gridpower": "Grid",
|
||||
"homepower": "Consumption",
|
||||
"chargePower": "Charger"
|
||||
},
|
||||
"flood": {
|
||||
"download": "Download",
|
||||
|
||||
@ -16,6 +16,7 @@ const components = {
|
||||
docker: dynamic(() => import("./docker/component")),
|
||||
kubernetes: dynamic(() => import("./kubernetes/component")),
|
||||
emby: dynamic(() => import("./emby/component")),
|
||||
evcc: dynamic(() => import("./evcc/component")),
|
||||
fileflows: dynamic(() => import("./fileflows/component")),
|
||||
flood: dynamic(() => import("./flood/component")),
|
||||
freshrss: dynamic(() => import("./freshrss/component")),
|
||||
|
||||
39
src/widgets/evcc/component.jsx
Normal file
39
src/widgets/evcc/component.jsx
Normal file
@ -0,0 +1,39 @@
|
||||
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 { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
const { data: resultData, error: resultError } = useWidgetAPI(widget, "result");
|
||||
|
||||
|
||||
if (resultError) {
|
||||
return <Container service={service} error={resultError} />;
|
||||
}
|
||||
|
||||
if (!resultData) {
|
||||
return (
|
||||
<Container service={service}>,
|
||||
<Block label="evcc.pvPower" />
|
||||
<Block label="evcc.batterySoc" />
|
||||
<Block label="evcc.gridpower" />
|
||||
<Block label="evcc.homepower" />
|
||||
<Block label="evcc.chargePower"/>
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="evcc.homepower" value={t("common.number", { value: resultData.result.homePower }) + " W"} />
|
||||
<Block label="evcc.batterySoc" value={t("common.number", { value: resultData.result.batterySoc }) + " %"} />
|
||||
<Block label="evcc.gridpower" value={t("common.number", { value: resultData.result.gridPower }) + " W"} />
|
||||
<Block label="evcc.homepower" value={t("common.number", { value: resultData.result.homePower }) + " W"} />
|
||||
<Block label="evcc.chargePower" value={t("common.number", { value: resultData.result.loadpoints[0].chargePower }) + " W"} />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
89
src/widgets/evcc/proxy.js
Normal file
89
src/widgets/evcc/proxy.js
Normal file
@ -0,0 +1,89 @@
|
||||
import { httpProxy } from "utils/proxy/http";
|
||||
import getServiceWidget from "utils/config/service-helpers";
|
||||
import createLogger from "utils/logger";
|
||||
|
||||
const logger = createLogger("homeassistantProxyHandler");
|
||||
|
||||
const defaultQueries = [
|
||||
{
|
||||
template: "{{ states.person|selectattr('state','equalto','home')|list|length }} / {{ states.person|list|length }}",
|
||||
label: "homeassistant.people_home"
|
||||
},
|
||||
{
|
||||
template: "{{ states.light|selectattr('state','equalto','on')|list|length }} / {{ states.light|list|length }}",
|
||||
label: "homeassistant.lights_on"
|
||||
},
|
||||
{
|
||||
template: "{{ states.switch|selectattr('state','equalto','on')|list|length }} / {{ states.switch|list|length }}",
|
||||
label: "homeassistant.switches_on"
|
||||
}
|
||||
];
|
||||
|
||||
function formatOutput(output, data) {
|
||||
return output.replace(/\{.*?\}/g,
|
||||
(match) => match.replace(/\{|\}/g, "").split(".").reduce((o, p) => o ? o[p] : "", data) ?? "");
|
||||
}
|
||||
|
||||
async function getQuery(query, { url, key }) {
|
||||
const headers = { Authorization: `Bearer ${key}` };
|
||||
const { state, template, label, value } = query;
|
||||
if (state) {
|
||||
return {
|
||||
result: await httpProxy(new URL(`${url}/api/states/${state}`), {
|
||||
headers,
|
||||
method: "GET"
|
||||
}),
|
||||
output: (data) => {
|
||||
const jsonData = JSON.parse(data);
|
||||
return {
|
||||
label: formatOutput(label ?? "{attributes.friendly_name}", jsonData),
|
||||
value: formatOutput(value ?? "{state} {attributes.unit_of_measurement}", jsonData)
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
if (template) {
|
||||
return {
|
||||
result: await httpProxy(new URL(`${url}/api/template`), {
|
||||
headers,
|
||||
method: "POST",
|
||||
body: JSON.stringify({ template })
|
||||
}),
|
||||
output: (data) => ({ label, value: data.toString() })
|
||||
};
|
||||
}
|
||||
return { result: [500, null, { error: { message: `invalid query ${JSON.stringify(query)}` } }] };
|
||||
}
|
||||
|
||||
export default async function evccProxyHandler(req, res) {
|
||||
const { group, service } = req.query;
|
||||
|
||||
if (!group || !service) {
|
||||
logger.debug("Invalid or missing service '%s' or group '%s'", service, group);
|
||||
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||
}
|
||||
|
||||
const widget = await getServiceWidget(group, service);
|
||||
if (!widget) {
|
||||
logger.debug("Invalid or missing widget for service '%s' in group '%s'", service, group);
|
||||
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||
}
|
||||
|
||||
let queries = defaultQueries;
|
||||
if (!widget.fields && widget.custom) {
|
||||
queries = widget.custom.slice(0, 4);
|
||||
}
|
||||
|
||||
const results = await Promise.all(queries.map(q => getQuery(q, widget)));
|
||||
|
||||
const err = results.find(r => r.result[2]?.error);
|
||||
if (err) {
|
||||
const [status, , data] = err.result;
|
||||
return res.status(status).send(data);
|
||||
}
|
||||
|
||||
return res.status(200).send(results.map(r => {
|
||||
const [status, , data] = r.result;
|
||||
return status === 200 ? r.output(data) : { label: status, value: data.toString() };
|
||||
}));
|
||||
}
|
||||
14
src/widgets/evcc/widget.js
Normal file
14
src/widgets/evcc/widget.js
Normal file
@ -0,0 +1,14 @@
|
||||
import genericProxyHandler from "utils/proxy/handlers/generic";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/api/{endpoint}",
|
||||
proxyHandler: genericProxyHandler,
|
||||
|
||||
mappings: {
|
||||
result: {
|
||||
endpoint: "state",
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export default widget;
|
||||
@ -11,6 +11,7 @@ import deluge from "./deluge/widget";
|
||||
import diskstation from "./diskstation/widget";
|
||||
import downloadstation from "./downloadstation/widget";
|
||||
import emby from "./emby/widget";
|
||||
import evcc from "./evcc/widget";
|
||||
import fileflows from "./fileflows/widget";
|
||||
import flood from "./flood/widget";
|
||||
import freshrss from "./freshrss/widget";
|
||||
@ -92,6 +93,7 @@ const widgets = {
|
||||
diskstation,
|
||||
downloadstation,
|
||||
emby,
|
||||
evcc,
|
||||
fileflows,
|
||||
flood,
|
||||
freshrss,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user