mirror of
https://github.com/gethomepage/homepage.git
synced 2026-01-14 13:12:09 +08:00
Run pre-commit hooks over existing codebase
Co-Authored-By: Ben Phelps <ben@phelps.io>
This commit is contained in:
@@ -9,8 +9,12 @@ export default function Component({ service }) {
|
||||
if (error) {
|
||||
return <Container service={service} error={error} />;
|
||||
}
|
||||
|
||||
return <Container service={service}>
|
||||
{data?.map(d => <Block label={d.label} value={d.value} key={d.label} />)}
|
||||
</Container>;
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
{data?.map((d) => (
|
||||
<Block label={d.label} value={d.value} key={d.label} />
|
||||
))}
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -7,21 +7,27 @@ const logger = createLogger("homeassistantProxyHandler");
|
||||
const defaultQueries = [
|
||||
{
|
||||
template: "{{ states.person|selectattr('state','equalto','home')|list|length }} / {{ states.person|list|length }}",
|
||||
label: "homeassistant.people_home"
|
||||
label: "homeassistant.people_home",
|
||||
},
|
||||
{
|
||||
template: "{{ states.light|selectattr('state','equalto','on')|list|length }} / {{ states.light|list|length }}",
|
||||
label: "homeassistant.lights_on"
|
||||
label: "homeassistant.lights_on",
|
||||
},
|
||||
{
|
||||
template: "{{ states.switch|selectattr('state','equalto','on')|list|length }} / {{ states.switch|list|length }}",
|
||||
label: "homeassistant.switches_on"
|
||||
}
|
||||
label: "homeassistant.switches_on",
|
||||
},
|
||||
];
|
||||
|
||||
function formatOutput(output, data) {
|
||||
return output.replace(/\{.*?\}/g,
|
||||
(match) => match.replace(/\{|\}/g, "").split(".").reduce((o, p) => o ? o[p] : "", data) ?? "");
|
||||
return output.replace(
|
||||
/\{.*?\}/g,
|
||||
(match) =>
|
||||
match
|
||||
.replace(/\{|\}/g, "")
|
||||
.split(".")
|
||||
.reduce((o, p) => (o ? o[p] : ""), data) ?? "",
|
||||
);
|
||||
}
|
||||
|
||||
async function getQuery(query, { url, key }) {
|
||||
@@ -31,15 +37,15 @@ async function getQuery(query, { url, key }) {
|
||||
return {
|
||||
result: await httpProxy(new URL(`${url}/api/states/${state}`), {
|
||||
headers,
|
||||
method: "GET"
|
||||
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)
|
||||
value: formatOutput(value ?? "{state} {attributes.unit_of_measurement}", jsonData),
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
if (template) {
|
||||
@@ -47,9 +53,9 @@ async function getQuery(query, { url, key }) {
|
||||
result: await httpProxy(new URL(`${url}/api/template`), {
|
||||
headers,
|
||||
method: "POST",
|
||||
body: JSON.stringify({ template })
|
||||
body: JSON.stringify({ template }),
|
||||
}),
|
||||
output: (data) => ({ label, value: data.toString() })
|
||||
output: (data) => ({ label, value: data.toString() }),
|
||||
};
|
||||
}
|
||||
return { result: [500, null, { error: { message: `invalid query ${JSON.stringify(query)}` } }] };
|
||||
@@ -68,12 +74,12 @@ 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" });
|
||||
}
|
||||
|
||||
|
||||
let queries = defaultQueries;
|
||||
if (!widget.fields && widget.custom) {
|
||||
if (typeof widget.custom === 'string') {
|
||||
if (typeof widget.custom === "string") {
|
||||
try {
|
||||
widget.custom = JSON.parse(widget.custom)
|
||||
widget.custom = JSON.parse(widget.custom);
|
||||
} catch (error) {
|
||||
logger.debug("Error parsing HASS widget custom label: %s", JSON.stringify(error));
|
||||
return res.status(400).json({ error: "Error parsing widget custom label" });
|
||||
@@ -82,16 +88,18 @@ export default async function homeassistantProxyHandler(req, res) {
|
||||
queries = widget.custom.slice(0, 4);
|
||||
}
|
||||
|
||||
const results = await Promise.all(queries.map(q => getQuery(q, widget)));
|
||||
const results = await Promise.all(queries.map((q) => getQuery(q, widget)));
|
||||
|
||||
const err = results.find(r => r.result[2]?.error);
|
||||
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() };
|
||||
}));
|
||||
return res.status(200).send(
|
||||
results.map((r) => {
|
||||
const [status, , data] = r.result;
|
||||
return status === 200 ? r.output(data) : { label: status, value: data.toString() };
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user