reloading autos

This commit is contained in:
ari committed 2022-06-13 19:26:55 -04:00
1 parent c17783064c
commit 0cecae5faa
9 files changed
+103 -31

No files matched your search

+3 -3
View File
@@ -5,7 +5,7 @@ import { AutomationAPI } from './automations';
export const useDefaultApiState = () => useState<ApiState>({
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,
}
});
}
@@ -46,6 +46,7 @@ export const AutomationEditor: FC<Props> = ({
if (dims.flipped !== flipped) {
setFlipped(dims.flipped)
}
// eslint-disable-next-line
}, [dims.flipped])
// render
@@ -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)
+19 -4
View File
@@ -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<ConnectedAutomationListParams> = ({
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 <div className="automation-list--root loading">
<LinearProgress />
@@ -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';
+2 -6
View File
@@ -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<keyof AutomationTimeObject> = [
@@ -120,6 +115,7 @@ export const InputTime: FC<Props> = (props) => {
props.onChange(displayValue)
}
}
// eslint-disable-next-line
}, [disabled, displayValue])
@@ -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) => [S, Dispatch<SetStateAction<S>>]
+46
View File
@@ -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<T = {}> = 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")
}
}
+24 -13
View File
@@ -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 <div style={{ overflow: 'auto', maxHeight: '100vh' }}>
<ul>
<InputList label="Domains" current={domain} onChange={setDomain} options={options} />
<InputList label="Services" current={service} onChange={setService} options={domains} />
<Button onClick={() => callService(domain, service)}>Call</Button>
<InputYaml label="Service Data" value={(data[domain] ?? {})[service] ?? {}} onChange={() => { }} />
<ul style={{
maxHeight: '20vh',
overflow: 'auto',
}}>
{Object.keys(summary).map(k => <li key={k}>
<b>{k}</b> <ul>
{Object.keys(summary[k]).map(t => <li key={k + t}>
@@ -89,7 +97,10 @@ const Test: FC = () => {
</li>)}
</ul>
<h1>Fields</h1>
<ul>
<ul style={{
maxHeight: '20vh',
overflow: 'auto',
}}>
{Object.keys(fields).map(k => <li key={k}>
<b>{k}</b> <ul>
{Object.keys(fields[k]).map(t => <li key={k + t}>
@@ -104,7 +115,10 @@ const Test: FC = () => {
</li>)}
</ul>
<h1>Targets</h1>
<ul>
<ul style={{
maxHeight: '20vh',
overflow: 'auto',
}}>
{Object.keys(target).map(k => <li key={k}>
<b>{k}</b> <ul>
{Object.keys(target[k]).map(t => <li key={k + t}>
@@ -118,9 +132,6 @@ const Test: FC = () => {
</ul>
</li>)}
</ul>
<InputList label="Services" current={current} onChange={setCurrent} options={options} />
<InputList label="Domain" current={secCurrent} onChange={setSecCurrent} options={suboptions} />
<InputYaml label="Service Data" value={(data[current] ?? {})[secCurrent] ?? {}} onChange={() => { }} />
</div>
}