From 352b4146f7984b2000dda18838665abd53a876e7 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Wed, 12 Apr 2023 01:06:48 -0700 Subject: [PATCH] homeassistant widget minor code cleanup / changes - limit to 4 blocks - allow container to handle field filtering --- src/components/services/widget/container.jsx | 2 +- src/widgets/homeassistant/component.jsx | 10 +++++----- src/widgets/homeassistant/proxy.js | 12 +++++++----- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/components/services/widget/container.jsx b/src/components/services/widget/container.jsx index 11dd4f563..c2249f56f 100644 --- a/src/components/services/widget/container.jsx +++ b/src/components/services/widget/container.jsx @@ -14,7 +14,7 @@ export default function Container({ error = false, children, service }) { // fields: [ "resources.cpu", "resources.mem", "field"] // or even // fields: [ "resources.cpu", "widget_type.field" ] - visibleChildren = children.filter(child => fields.some(field => { + visibleChildren = children?.filter(child => fields.some(field => { let fullField = field; if (!field.includes(".")) { fullField = `${type}.${field}`; diff --git a/src/widgets/homeassistant/component.jsx b/src/widgets/homeassistant/component.jsx index 2ba809285..48fa61ffe 100644 --- a/src/widgets/homeassistant/component.jsx +++ b/src/widgets/homeassistant/component.jsx @@ -5,12 +5,12 @@ import useWidgetAPI from "utils/proxy/use-widget-api"; export default function Component({ service }) { const { widget } = service; - const { data, error } = useWidgetAPI(widget, "", { refreshInterval: 60000 }); + const { data, error } = useWidgetAPI(widget, null, { refreshInterval: 60000 }); if (error) { return ; } - const panels = []; - data?.forEach(d => panels.push()); - - return {panels}; + + return + {data?.map(d => )} + ; } diff --git a/src/widgets/homeassistant/proxy.js b/src/widgets/homeassistant/proxy.js index 2c6a517be..648525ef5 100644 --- a/src/widgets/homeassistant/proxy.js +++ b/src/widgets/homeassistant/proxy.js @@ -6,7 +6,7 @@ const logger = createLogger("homeassistantProxyHandler"); const defaultQueries = [ { - template: "{{ states.person|selectattr('state','equalto','home')|list|length }} / {{ states.person|selectattr('state','search','(^home$|^not home$)')|list|length }}", + template: "{{ states.person|selectattr('state','equalto','home')|list|length }} / {{ states.person|list|length }}", label: "homeassistant.people_home" }, { @@ -52,7 +52,7 @@ async function getQuery(query, { url, key }) { output: (data) => ({ label, value: data.toString() }) }; } - return { result: [500, "", { error: { message: `invalid query ${JSON.stringify(query)}` } }] }; + return { result: [500, null, { error: { message: `invalid query ${JSON.stringify(query)}` } }] }; } export default async function homeassistantProxyHandler(req, res) { @@ -68,9 +68,11 @@ export default async function homeassistantProxyHandler(req, res) { logger.debug("Invalid or missing widget for service '%s' in group '%s'", service, group); return res.status(400).json({ error: "Invalid proxy service type" }); } - - const queries = widget.custom !== undefined && widget.fields === undefined ? - widget.custom : defaultQueries.filter(q => widget.fields?.includes(q.label.split(".")[1]) ?? true); + + 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)));