feature: trigger automations (#1799)

This commit is contained in:
Manuel
2024-01-11 17:49:27 +01:00
committed by GitHub
parent 6d6750c665
commit 35e8c76120
8 changed files with 195 additions and 18 deletions

View File

@@ -12,6 +12,7 @@ import mediaServer from './media-server/MediaServerTile';
import notebook from './notebook/NotebookWidgetTile';
import rss from './rss/RssWidgetTile';
import smartHomeEntityState from './smart-home/entity-state/entity-state.widget';
import smartHomeTriggerAutomation from './smart-home/trigger-automation/trigger-automation.widget';
import torrent from './torrent/TorrentTile';
import usenet from './useNet/UseNetTile';
import videoStream from './video/VideoStreamTile';
@@ -35,5 +36,6 @@ export default {
'dns-hole-controls': dnsHoleControls,
bookmark,
notebook,
'smart-home/entity-state': smartHomeEntityState
'smart-home/entity-state': smartHomeEntityState,
'smart-home/trigger-automation': smartHomeTriggerAutomation,
};

View File

@@ -16,6 +16,11 @@ const definition = defineWidget({
defaultValue: 'sun.sun',
info: true,
},
automationId: {
type: 'text',
info: true,
defaultValue: '',
},
displayName: {
type: 'text',
defaultValue: 'Sun',
@@ -39,6 +44,7 @@ interface SmartHomeEntityStateWidgetProps {
function EntityStateTile({ widget }: SmartHomeEntityStateWidgetProps) {
const { t } = useTranslation('modules/smart-home/entity-state');
const { name: configName } = useConfigContext();
const utils = api.useUtils();
const { data, isInitialLoading, isLoading, isError, error } =
api.smartHomeEntityState.retrieveStatus.useQuery(
@@ -48,10 +54,27 @@ function EntityStateTile({ widget }: SmartHomeEntityStateWidgetProps) {
},
{
enabled: !!configName,
refetchInterval: 2 * 60 * 1000
}
refetchInterval: 2 * 60 * 1000,
},
);
const { mutateAsync: mutateTriggerAutomationAsync } = api.smartHomeEntityState.triggerAutomation.useMutation({
onSuccess: () => {
void utils.smartHomeEntityState.invalidate();
},
});
const handleClick = async () => {
if (!widget.properties.automationId) {
return;
}
await mutateTriggerAutomationAsync({
configName: configName as string,
widgetId: widget.id,
});
};
let dataComponent = null;
if (isError) {
@@ -84,7 +107,15 @@ function EntityStateTile({ widget }: SmartHomeEntityStateWidgetProps) {
}
return (
<Center h="100%" w="100%">
<Center
onClick={handleClick}
sx={() => {
return {
cursor: widget.properties.automationId?.length > 0 ? 'pointer' : undefined,
};
}}
h="100%"
w="100%">
<Stack align="center" spacing={3}>
<Text align="center" weight="bold" size="lg">
{widget.properties.displayName}

View File

@@ -0,0 +1,65 @@
import { defineWidget } from '~/widgets/helper';
import { IconSettingsAutomation } from '@tabler/icons-react';
import { IWidget } from '~/widgets/widgets';
import { useConfigContext } from '~/config/provider';
import { api } from '~/utils/api';
import { Center, Stack, Text } from '@mantine/core';
const definition = defineWidget({
id: 'smart-home/trigger-automation',
icon: IconSettingsAutomation,
options: {
automationId: {
type: 'text',
info: true,
defaultValue: ''
},
displayName: {
type: 'text',
defaultValue: 'Sun',
},
},
gridstack: {
minWidth: 1,
minHeight: 1,
maxWidth: 12,
maxHeight: 12,
},
component: TriggerAutomationTile,
});
export type ISmartHomeTriggerAutomationWidget = IWidget<(typeof definition)['id'], typeof definition>;
interface SmartHomeTriggerAutomationWidgetProps {
widget: ISmartHomeTriggerAutomationWidget;
}
function TriggerAutomationTile({ widget }: SmartHomeTriggerAutomationWidgetProps) {
const { name: configName } = useConfigContext();
const utils = api.useUtils();
const { mutateAsync: mutateTriggerAutomationAsync } = api.smartHomeEntityState.triggerAutomation.useMutation({
onSuccess: () => {
void utils.smartHomeEntityState.invalidate();
}
});
const handleClick = async () => {
await mutateTriggerAutomationAsync({
configName: configName as string,
widgetId: widget.id
});
}
return (
<Center onClick={handleClick} style={{ cursor: 'pointer' }} h="100%" w="100%">
<Stack align="center" spacing={3}>
<Text align="center" weight="bold" size="lg">
{widget.properties.displayName}
</Text>
</Stack>
</Center>
);
}
export default definition;