diff --git a/webapp/src/apiService/util.ts b/webapp/src/apiService/util.ts index 7b09167..a406c4a 100644 --- a/webapp/src/apiService/util.ts +++ b/webapp/src/apiService/util.ts @@ -5,7 +5,7 @@ import { AutomationAPI } from './automations'; export const useDefaultApiState = () => useState({ - automations: {ready: false}, + automations: { ready: false }, }); export const makeReloadAutomations = ( @@ -21,8 +21,8 @@ export const makeReloadAutomations = ( const resp = await automationAPI.list(params); setState({ ...state, automations: { - ready: true, - ...resp, + ready: true, + ...resp, } }); } diff --git a/webapp/src/components/AutomationEditor/index.tsx b/webapp/src/components/AutomationEditor/index.tsx index 954837b..b57fd5f 100644 --- a/webapp/src/components/AutomationEditor/index.tsx +++ b/webapp/src/components/AutomationEditor/index.tsx @@ -46,6 +46,7 @@ export const AutomationEditor: FC = ({ if (dims.flipped !== flipped) { setFlipped(dims.flipped) } + // eslint-disable-next-line }, [dims.flipped]) // render diff --git a/webapp/src/components/AutomationList/AutomationListBox.tsx b/webapp/src/components/AutomationList/AutomationListBox.tsx index 080f061..138ac36 100644 --- a/webapp/src/components/AutomationList/AutomationListBox.tsx +++ b/webapp/src/components/AutomationList/AutomationListBox.tsx @@ -18,7 +18,12 @@ export type Props = { } const useAutomationListBoxState = () => { - const [cookies, setCookies, _] = useCookies(['albSearchText', 'albSelected']); + const [ + cookies, + setCookies, + // eslint-disable-next-line + _ + ] = useCookies(['albSearchText', 'albSelected']); let initialSelected = []; try { initialSelected = JSON.parse(cookies.albSelected) diff --git a/webapp/src/components/AutomationList/index.tsx b/webapp/src/components/AutomationList/index.tsx index 63f7568..966c87a 100644 --- a/webapp/src/components/AutomationList/index.tsx +++ b/webapp/src/components/AutomationList/index.tsx @@ -14,6 +14,7 @@ import { makeTagDB } from "./TagDB"; import LinearProgress from "@mui/material/LinearProgress"; import CircularProgress from "@mui/material/CircularProgress"; import { useCookies } from 'react-cookie'; +import { useHA } from "haService"; interface AutomationListParams { @@ -26,11 +27,15 @@ interface AutomationListParams { } export const useAutomationListState = () => { - const [cookies, setCookies, _] = useCookies(['alCurrent']); + const [ + cookies, + setCookies, + // eslint-disable-next-line + _ + ] = useCookies(['alCurrent']); let initialCurrent = 0; try { initialCurrent = Number(cookies.alCurrent ?? '0'); - // eslint-disable-next-line } catch (_) { } const [hideList, setHideList] = useState(false); @@ -109,11 +114,21 @@ export const ConnectedAutomationList: FC = ({ state: { automations, }, - removeAutomation, - updateAutomation, + ...methods }, dims }) => { + const { reloadAutomations } = useHA(); + const removeAutomation = async (args: any) => { + const resp = await methods.removeAutomation(args) + await reloadAutomations(); + return resp + } + const updateAutomation = async (args: any) => { + const resp = await methods.updateAutomation(args) + await reloadAutomations(); + return resp + } if (!automations.ready) { return
diff --git a/webapp/src/components/Inputs/InputList.tsx b/webapp/src/components/Inputs/InputList.tsx index c0fa6d9..379bb75 100644 --- a/webapp/src/components/Inputs/InputList.tsx +++ b/webapp/src/components/Inputs/InputList.tsx @@ -1,4 +1,3 @@ -import { FC } from "react"; import FormHelperText from '@mui/material/FormHelperText'; import InputLabel from '@mui/material/InputLabel'; import MenuItem from '@mui/material/MenuItem'; diff --git a/webapp/src/components/Inputs/InputTime.tsx b/webapp/src/components/Inputs/InputTime.tsx index c1f249f..7c1dea7 100644 --- a/webapp/src/components/Inputs/InputTime.tsx +++ b/webapp/src/components/Inputs/InputTime.tsx @@ -5,13 +5,8 @@ import EditIcon from '@mui/icons-material/Edit'; import CloseIcon from "@mui/icons-material/Close"; import IconButton from "@mui/material/IconButton" import { AutomationTime, AutomationTimeObject } from "types/automations/common"; -import { convertAutomationTimeToTimeObject, convertObjectToAutomationTimeString, milisecondsToObject } from "utils/time"; -import { convertStringToAutomationTimeObject, convertAutomationTimeToTimeString } from 'utils/time'; +import { convertAutomationTimeToTimeObject, convertObjectToAutomationTimeString, convertAutomationTimeToTimeString } from 'utils/time'; import { prettyName } from "utils/formatting"; -import FormControl from "@mui/material/FormControl"; -import InputLabel from '@mui/material/InputLabel'; -import Input from "@mui/material/Input"; -import FormHelperText from '@mui/material/FormHelperText'; // constants const fields: Array = [ @@ -120,6 +115,7 @@ export const InputTime: FC = (props) => { props.onChange(displayValue) } } + // eslint-disable-next-line }, [disabled, displayValue]) diff --git a/webapp/src/components/NodeEditor/OptionManager/index.tsx b/webapp/src/components/NodeEditor/OptionManager/index.tsx index 8319975..ff69cba 100644 --- a/webapp/src/components/NodeEditor/OptionManager/index.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/index.tsx @@ -1,4 +1,4 @@ -import { Dispatch, SetStateAction, useEffect, useState, Component } from 'react'; +import { Dispatch, SetStateAction, useEffect, useState } from 'react'; import { AutomationNode, AutomationNodeTypes } from 'types/automations'; import { AutomationNodeSubtype } from 'types/automations'; import { getOptionManager } from './getOptionManager'; @@ -8,8 +8,7 @@ import InputText from 'components/Inputs/InputText'; import { getDescriptionFromAutomationNode } from 'utils/formatting'; import { useHA } from 'haService'; import { Generic } from './Generic'; -import { getNodeTypeAndValidate } from '../../../utils/automations'; -import InputBoolean from 'components/Inputs/InputBoolean'; +import { getNodeTypeAndValidate } from 'utils/automations'; type UseState = (s: S) => [S, Dispatch>] diff --git a/webapp/src/haService/index.ts b/webapp/src/haService/index.ts index 44c939e..9e537ac 100644 --- a/webapp/src/haService/index.ts +++ b/webapp/src/haService/index.ts @@ -4,6 +4,8 @@ import { deviceRegistryColl } from "./DeviceRegistry"; import { entityRegistryColl } from "./EntityRegistry"; import { DeviceRegistryItem } from "./types"; import { useHassCollection } from "./useHassCollection" +import { useHAConnection } from './connection'; +import { useSnackbar } from "notistack"; export type Option = string | ({ id: string, @@ -165,6 +167,48 @@ export const useHA = () => { const entities = useHAEntities(); const devices = useHADeviceRegistry(); const services = useHAServices(); + const conn = useHAConnection(); + const { enqueueSnackbar } = useSnackbar(); + + const callService = async ( + domain: string, + service: string, + data: any = {}, + target: any = {}, + ) => { + if (conn.status === 'loaded') { + try { + const result = await conn.connection.sendMessagePromise({ + "type": "call_service", + domain, + service, + target, + service_data: data + }) + enqueueSnackbar(`Successfully called ${domain}.${service}`, { + "variant": "success", + }) + return result + } catch (err: any) { + let msg = JSON.stringify(err) + if ('message' in err) { + msg = err.message + } + enqueueSnackbar(`Failed to call ${domain}.${service} because '${msg}'`, { + "variant": "error", + }) + return + } + } if (conn.status === 'error') { + enqueueSnackbar(`Failed to call service because connection is '${conn.status}' -- ${conn.error}`, { + "variant": "error" + }) + } else { + enqueueSnackbar(`Failed to call service because connection is '${conn.status}'`, { + "variant": "error" + }) + } + } const namer: Namer = { getDeviceName(device_id) { @@ -186,5 +230,7 @@ export const useHA = () => { devices, services, namer, + callService, + reloadAutomations: () => callService("automation", "reload") } } \ No newline at end of file diff --git a/webapp/src/haService/services.stories.tsx b/webapp/src/haService/services.stories.tsx index 9b4bc02..4bc07e0 100644 --- a/webapp/src/haService/services.stories.tsx +++ b/webapp/src/haService/services.stories.tsx @@ -1,12 +1,13 @@ import React, { FC, useEffect, useState } from "react"; import { ComponentMeta, ComponentStory } from '@storybook/react'; import { Page } from "components/Page"; -import { useHAServices } from "haService"; +import { useHA } from "haService"; import InputYaml from "components/Inputs/InputYaml"; import { InputList } from "components/Inputs/InputList"; +import { Button } from "components/Inputs/Button"; const Test: FC = () => { - const services = useHAServices(); + const { services, callService } = useHA(); const data = services.collection?.state ?? {}; const options = Object.keys(data).sort(); @@ -62,19 +63,26 @@ const Test: FC = () => { ) ) - const [current, setCurrent] = useState(options[0] ?? ""); + const [domain, setDomain] = useState(options[0] ?? ""); - const suboptions = Object.keys(data[current] ?? "").sort(); + const domains = Object.keys(data[domain] ?? "").sort(); - const [secCurrent, setSecCurrent] = useState(suboptions[0] ?? ""); + const [service, setService] = useState(domains[0] ?? ""); useEffect(() => { - setSecCurrent(suboptions[0] ?? "") - }, [current, setSecCurrent]) + setService(domains[0] ?? "") + }, [domain, setService]) return
-
    + + + + { }} /> +
      {Object.keys(summary).map(k =>
    • {k}
        {Object.keys(summary[k]).map(t =>
      • @@ -89,7 +97,10 @@ const Test: FC = () => {
      • )}

      Fields

      -
        +
          {Object.keys(fields).map(k =>
        • {k}
            {Object.keys(fields[k]).map(t =>
          • @@ -104,7 +115,10 @@ const Test: FC = () => {
          • )}

          Targets

          -
            +
              {Object.keys(target).map(k =>
            • {k}
                {Object.keys(target[k]).map(t =>
              • @@ -118,9 +132,6 @@ const Test: FC = () => {
            • )}
            - - - { }} />
}