feat: add homeassistant integration (#578)

This commit is contained in:
Manuel
2024-06-10 21:16:39 +02:00
committed by GitHub
parent 19498854fc
commit 2e782ae442
28 changed files with 468 additions and 31 deletions

View File

@@ -0,0 +1,76 @@
"use client";
import React, { useState } from "react";
import { Center, Stack, Text, UnstyledButton } from "@mantine/core";
import { clientApi } from "@homarr/api/client";
import type { WidgetComponentProps } from "../../definition";
export default function SmartHomeEntityStateWidget({
options,
integrationIds,
isEditMode,
}: WidgetComponentProps<"smartHome-entityState">) {
const [lastState, setLastState] = useState<{
entityId: string;
state: string;
}>();
const utils = clientApi.useUtils();
clientApi.widget.smartHome.subscribeEntityState.useSubscription(
{
entityId: options.entityId,
},
{
onData(data) {
setLastState(data);
},
},
);
const { mutate } = clientApi.widget.smartHome.switchEntity.useMutation({
onSettled: () => {
void utils.widget.smartHome.invalidate();
},
});
const attribute = options.entityUnit.length > 0 ? " " + options.entityUnit : "";
const handleClick = React.useCallback(() => {
if (isEditMode) {
return;
}
if (!options.clickable) {
return;
}
mutate({
entityId: options.entityId,
integrationId: integrationIds[0] ?? "",
});
}, []);
return (
<UnstyledButton
onClick={handleClick}
w="100%"
h="100%"
styles={{ root: { cursor: options.clickable && !isEditMode ? "pointer" : "initial" } }}
>
<Center h="100%" w="100%">
<Stack align="center" gap="md">
<Text ta="center" fw="bold" size="lg">
{options.displayName}
</Text>
<Text ta="center">
{lastState?.state}
{attribute}
</Text>
</Stack>
</Center>
</UnstyledButton>
);
}

View File

@@ -0,0 +1,19 @@
import { IconBinaryTree } from "@tabler/icons-react";
import { createWidgetDefinition } from "../../definition";
import { optionsBuilder } from "../../options";
export const { definition, componentLoader } = createWidgetDefinition("smartHome-entityState", {
icon: IconBinaryTree,
options: optionsBuilder.from((factory) => ({
entityId: factory.text({
defaultValue: "sun.sun",
}),
displayName: factory.text({
defaultValue: "Sun",
}),
entityUnit: factory.text(),
clickable: factory.switch(),
})),
supportedIntegrations: ["homeAssistant"],
}).withDynamicImport(() => import("./component"));