cleaned up time trigger
This commit is contained in:
1 parent
ef02abe0cb
commit
3a44057a40
14 files changed
+536
-167
No files matched your search
@@ -10,6 +10,8 @@ export type InputEntityProps = InputAutoCompletePropsBase & {
|
||||
restrictToDomain?: string[];
|
||||
restrictedIntegrations?: string[];
|
||||
preSelectedEntityIds?: string[];
|
||||
restrictedDeviceClass?: string[];
|
||||
restrictMode?: "and" | "or";
|
||||
};
|
||||
|
||||
export const InputEntity: FC<InputEntityProps> = (props) => {
|
||||
@@ -17,7 +19,9 @@ export const InputEntity: FC<InputEntityProps> = (props) => {
|
||||
const entities = useHAEntities();
|
||||
let options = entities.getOptions(
|
||||
props.restrictToDomain,
|
||||
props.restrictedIntegrations
|
||||
props.restrictedIntegrations,
|
||||
props.restrictedDeviceClass,
|
||||
props.restrictMode ?? "and"
|
||||
);
|
||||
if (props.preSelectedEntityIds) {
|
||||
options = options.filter((opt) =>
|
||||
|
||||
@@ -6,29 +6,35 @@ import { AutomationTime } from "types/automations/common";
|
||||
import { InputTime } from "./InputTime";
|
||||
|
||||
export default {
|
||||
title: 'Inputs/InputTime',
|
||||
title: "Inputs/InputTime",
|
||||
component: InputTime,
|
||||
parameters: { actions: { argTypesRegex: '^on.*' } },
|
||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||
} as ComponentMeta<typeof InputTime>;
|
||||
|
||||
|
||||
const Template: ComponentStory<typeof InputTime> = args => {
|
||||
|
||||
const Template: ComponentStory<typeof InputTime> = (args) => {
|
||||
const [value, setValue] = useState<AutomationTime | undefined>(undefined);
|
||||
return <Page>
|
||||
<div style={{
|
||||
padding: '1em'
|
||||
}}>
|
||||
<InputTime {...args} value={value} onChange={t => {
|
||||
setValue(t)
|
||||
args.onChange(t)
|
||||
}} />
|
||||
</div>
|
||||
</Page>
|
||||
}
|
||||
return (
|
||||
<Page>
|
||||
<div
|
||||
style={{
|
||||
padding: "1em",
|
||||
}}
|
||||
>
|
||||
<InputTime
|
||||
{...args}
|
||||
value={args.restrictEmpty ? "00:00:00" : value}
|
||||
onChange={(t) => {
|
||||
setValue(t);
|
||||
args.onChange(t);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
|
||||
export const SimpleText = Template.bind({})
|
||||
export const SimpleText = Template.bind({});
|
||||
SimpleText.args = {
|
||||
label: "Time Stamp",
|
||||
value: "10"
|
||||
}
|
||||
value: "10",
|
||||
};
|
||||
@@ -19,13 +19,14 @@ const fields: Array<keyof AutomationTimeObject> = [
|
||||
"seconds",
|
||||
"milliseconds",
|
||||
];
|
||||
export interface Props {
|
||||
export type InputTimeProps = {
|
||||
label: string;
|
||||
value?: AutomationTime;
|
||||
onChange: (v?: AutomationTimeObject) => void;
|
||||
className?: string;
|
||||
}
|
||||
export const InputTime: FC<Props> = (props) => {
|
||||
value?: AutomationTime;
|
||||
restrictEmpty?: boolean;
|
||||
};
|
||||
export const InputTime: FC<InputTimeProps> = (props) => {
|
||||
// state
|
||||
const [disabled, setDisabled] = useState(!props.value);
|
||||
const [displayValue, setDisplayValue] = useState(
|
||||
@@ -140,10 +141,12 @@ export const InputTime: FC<Props> = (props) => {
|
||||
onChange={(e) => updateOne(name)(e.target.value)}
|
||||
/>
|
||||
))}
|
||||
<ButtonIcon
|
||||
onClick={() => setDisabled(!disabled)}
|
||||
icon={disabled ? <EditIcon /> : <CloseIcon />}
|
||||
/>
|
||||
{!props.restrictEmpty && (
|
||||
<ButtonIcon
|
||||
onClick={() => setDisabled(!disabled)}
|
||||
icon={disabled ? <EditIcon /> : <CloseIcon />}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
.trigger-time--help {
|
||||
margin-top: 0.5em;
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
gap: 0.5em;
|
||||
flex-wrap: wrap;
|
||||
justify-content: flex-start;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.trigger-time--help .text {
|
||||
word-wrap: break-word;
|
||||
flex-wrap: wrap;
|
||||
overflow: hidden;
|
||||
text-overflow: clip;
|
||||
cursor: pointer;
|
||||
padding: 0.5em 0.75em;
|
||||
max-width: 40em;
|
||||
}
|
||||
|
||||
.trigger-time--help .text a {
|
||||
color: var(--mui-info-light);
|
||||
}
|
||||
|
||||
.trigger-time--help .text a:hover {
|
||||
color: var(--mui-info-main);
|
||||
border-radius: 10px;
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.trigger-time--help .trigger-time--mode-selection {
|
||||
width: 10em;
|
||||
}
|
||||
|
||||
.trigger-time--pattern {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
gap: 0.5em;
|
||||
margin-top: 0.5em;
|
||||
}
|
||||
|
||||
.trigger-time--exact {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 0.25em;
|
||||
}
|
||||
|
||||
.trigger-time--exact .add {
|
||||
border-color: var(--mui-info-light);
|
||||
color: var(--mui-info-contrastText);
|
||||
background-color: var(--mui-info-main);
|
||||
}
|
||||
|
||||
.trigger-time--exact .add:hover {
|
||||
border-color: var(--mui-info-light);
|
||||
color: var(--mui-info-main);
|
||||
background-color: var(--mui-info-contrastText);
|
||||
}
|
||||
|
||||
.trigger-time--exact--option {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
padding-top: 0.5em;
|
||||
gap: 0.5em;
|
||||
}
|
||||
|
||||
.trigger-time--exact--option button.MuiButtonBase-root {
|
||||
border-color: var(--mui-info-light);
|
||||
background-color: var(--mui-info-main);
|
||||
color: var(--mui-info-contrastText);
|
||||
}
|
||||
|
||||
.trigger-time--exact--option button.MuiButtonBase-root:hover {
|
||||
background-color: var(--mui-info-light);
|
||||
}
|
||||
|
||||
.trigger-time--exact--option .input-autocomplete {
|
||||
min-width: 10em;
|
||||
}
|
||||
@@ -1,25 +1,199 @@
|
||||
import { OptionManager } from './OptionManager';
|
||||
import { AutomationTriggerTime } from 'types/automations/triggers';
|
||||
import { InputTime } from 'components/Inputs/InputTime';
|
||||
import { convertObjectToAutomationTimeString } from 'utils/time';
|
||||
import "./TriggerTime.css";
|
||||
|
||||
import HelpOutlinedIcon from "@mui/icons-material/HelpOutlined";
|
||||
import AccessTimeOutlinedIcon from "@mui/icons-material/AccessTimeOutlined";
|
||||
import MoreTimeOutlinedIcon from "@mui/icons-material/MoreTimeOutlined";
|
||||
|
||||
import { OptionManager } from "./OptionManager";
|
||||
import {
|
||||
AutomationTriggerTime,
|
||||
AutomationTriggerExactTime,
|
||||
AutomationTriggerTimePattern,
|
||||
} from "types/automations/triggers";
|
||||
import { InputTime } from "components/Inputs/InputTime";
|
||||
import {
|
||||
convertObjectToAutomationTimeString,
|
||||
isAutomationTimeString24Hours,
|
||||
} from "utils/time";
|
||||
import InputText from "components/Inputs/InputText";
|
||||
import { FC } from "react";
|
||||
import { InputList } from "components/Inputs/InputList";
|
||||
import { InputEntity } from "components/Inputs/InputEntities";
|
||||
import { Button } from "components/Inputs/Button";
|
||||
import { useState } from "react";
|
||||
import AddIcon from "@mui/icons-material/Add";
|
||||
import { ButtonIcon } from "components/Icons/ButtonIcons";
|
||||
|
||||
export const TriggerTime: OptionManager<AutomationTriggerTime> = {
|
||||
defaultState: () => ({
|
||||
"platform": 'time',
|
||||
"at": "",
|
||||
platform: "time",
|
||||
at: "00:00:00",
|
||||
}),
|
||||
isReady: () => true,
|
||||
renderOptionList: (state, setState) => {
|
||||
return <>
|
||||
<InputTime
|
||||
label="At"
|
||||
value={state.at}
|
||||
onChange={_at => setState({
|
||||
...state,
|
||||
at: _at ? convertObjectToAutomationTimeString(_at) : "00:00:00",
|
||||
})}
|
||||
Component: ({ state, setState }) => {
|
||||
const [showHelp, setHelp] = useState(false);
|
||||
const setTimeType = (t: AutomationTriggerTime["platform"]) => {
|
||||
if (t === "time") {
|
||||
setState({
|
||||
platform: "time",
|
||||
at: ["00:00:00"],
|
||||
});
|
||||
} else {
|
||||
setState({
|
||||
platform: "time_pattern",
|
||||
});
|
||||
}
|
||||
};
|
||||
return (
|
||||
<>
|
||||
<div className="trigger-time--help">
|
||||
<InputList
|
||||
className="trigger-time--mode-selection"
|
||||
label="Mode"
|
||||
current={state.platform}
|
||||
onChange={setTimeType}
|
||||
options={["time", "time_pattern"]}
|
||||
/>
|
||||
{state.platform === "time_pattern" && (
|
||||
<>
|
||||
<Button onClick={() => setHelp(!showHelp)}>
|
||||
<HelpOutlinedIcon /> Help
|
||||
</Button>
|
||||
</>
|
||||
)}
|
||||
{state.platform === "time_pattern" && showHelp && (
|
||||
<div className="text">
|
||||
You can prefix the value with a "/" to match whenever the value is
|
||||
divisible by that number. You can specify "*" to match any value.{" "}
|
||||
<a
|
||||
href="https://www.home-assistant.io/docs/automation/trigger/#time-pattern-trigger"
|
||||
target="_blank"
|
||||
>
|
||||
More Info.
|
||||
</a>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{state.platform === "time_pattern" ? (
|
||||
<TriggerTimePattern state={state as any} setState={setState} />
|
||||
) : (
|
||||
<TriggerExactTime state={state as any} setState={setState} />
|
||||
)}
|
||||
</>
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
export const TriggerExactTime: FC<{
|
||||
state: AutomationTriggerExactTime;
|
||||
setState: (t: AutomationTriggerExactTime) => void;
|
||||
}> = ({ state, setState }) => {
|
||||
const times = Array.isArray(state.at) ? state.at : [state.at];
|
||||
const update = (t: string, i: number) =>
|
||||
setState({
|
||||
...state,
|
||||
at: [...times.slice(0, i), t, ...times.slice(i + 1)],
|
||||
});
|
||||
const add = () =>
|
||||
setState({
|
||||
...state,
|
||||
at: [...times, "00:00:00"],
|
||||
});
|
||||
return (
|
||||
<div className="trigger-time--exact">
|
||||
{times.map((v, i) => {
|
||||
let editor: JSX.Element;
|
||||
const is24Hour = isAutomationTimeString24Hours(v);
|
||||
if (is24Hour) {
|
||||
editor = (
|
||||
<InputTime
|
||||
key={`${i}-editor`}
|
||||
label={`#${i + 1} - At`}
|
||||
value={v ?? "00:00:00"}
|
||||
restrictEmpty
|
||||
onChange={(_at) =>
|
||||
update(
|
||||
_at ? convertObjectToAutomationTimeString(_at) : "00:00:00",
|
||||
i
|
||||
)
|
||||
}
|
||||
/>
|
||||
);
|
||||
} else {
|
||||
editor = (
|
||||
<InputEntity
|
||||
key={`${i}-editor`}
|
||||
label="Time Entity"
|
||||
restrictMode="or"
|
||||
restrictedDeviceClass={["timestamp"]}
|
||||
restrictToDomain={["input_datetime"]}
|
||||
value={v}
|
||||
multiple={false}
|
||||
onChange={(_at) => update(_at ?? "", i)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<div className="trigger-time--exact--option">
|
||||
{editor}{" "}
|
||||
<ButtonIcon
|
||||
icon={
|
||||
is24Hour ? <AccessTimeOutlinedIcon /> : <MoreTimeOutlinedIcon />
|
||||
}
|
||||
onClick={() => {
|
||||
if (is24Hour) {
|
||||
update("", i);
|
||||
} else {
|
||||
update("00:00:00", i);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
<Button onClick={add} className="add">
|
||||
<AddIcon />
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export const TriggerTimePattern: FC<{
|
||||
state: AutomationTriggerTimePattern;
|
||||
setState: (t: AutomationTriggerTimePattern) => void;
|
||||
}> = ({ state, setState }) => {
|
||||
return (
|
||||
<div className="trigger-time--pattern">
|
||||
<InputText
|
||||
label="Hours"
|
||||
value={state.hours ?? ""}
|
||||
onChange={(hours) =>
|
||||
setState({
|
||||
...state,
|
||||
hours,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</>
|
||||
}
|
||||
}
|
||||
<InputText
|
||||
label="Minutes"
|
||||
value={state.minutes ?? ""}
|
||||
onChange={(minutes) =>
|
||||
setState({
|
||||
...state,
|
||||
minutes,
|
||||
})
|
||||
}
|
||||
/>
|
||||
<InputText
|
||||
label="Seconds"
|
||||
value={state.seconds ?? ""}
|
||||
onChange={(seconds) =>
|
||||
setState({
|
||||
...state,
|
||||
seconds,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -1,27 +0,0 @@
|
||||
import { OptionManager } from './OptionManager';
|
||||
import { AutomationTriggerTimePattern } from 'types/automations/triggers';
|
||||
import InputText from 'components/Inputs/InputText';
|
||||
|
||||
|
||||
export const TriggerTimePattern: OptionManager<AutomationTriggerTimePattern> = {
|
||||
defaultState: () => ({
|
||||
"platform": 'time_pattern',
|
||||
}),
|
||||
isReady: () => true,
|
||||
renderOptionList: (state, setState) => {
|
||||
return <>
|
||||
<InputText label="Hours" value={state.hours ?? ""} onChange={hours => setState({
|
||||
...state,
|
||||
hours
|
||||
})} />
|
||||
<InputText label="Minutes" value={state.minutes ?? ""} onChange={minutes => setState({
|
||||
...state,
|
||||
minutes
|
||||
})} />
|
||||
<InputText label="Seconds" value={state.seconds ?? ""} onChange={seconds => setState({
|
||||
...state,
|
||||
seconds
|
||||
})} />
|
||||
</>
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,6 @@ import { TriggerNumericState } from "./TriggerNumericState";
|
||||
import { TriggerState } from "./TriggerState";
|
||||
import { TriggerTemplate } from "./TriggerTemplate";
|
||||
import { TriggerTime } from "./TriggerTime";
|
||||
import { TriggerTimePattern } from "./TriggerTimePattern";
|
||||
import { TriggerWebhook } from "./TriggerWebhook";
|
||||
import { TriggerZone } from "./TriggerZone";
|
||||
import { TriggerDevice } from "./TriggerDevice";
|
||||
@@ -67,8 +66,6 @@ export const getOptionManager = <T extends AutomationNodeTypes>(
|
||||
return TriggerTemplate;
|
||||
case "time":
|
||||
return TriggerTime;
|
||||
case "time_pattern":
|
||||
return TriggerTimePattern;
|
||||
case "webhook":
|
||||
return TriggerWebhook;
|
||||
case "zone":
|
||||
|
||||
@@ -106,35 +106,82 @@ export const useHAEntities = () => {
|
||||
},
|
||||
getOptions(
|
||||
restrictToDomain?: string[],
|
||||
restrictedIntegrations?: string[]
|
||||
restrictedIntegrations?: string[],
|
||||
restrictedDeviceClass?: string[],
|
||||
mode: "and" | "or" = "and"
|
||||
): EntityOption[] {
|
||||
if (!entities.ready) {
|
||||
return [];
|
||||
}
|
||||
let keys = Object.keys(entities.collection.state);
|
||||
if (restrictToDomain) {
|
||||
const domains = restrictToDomain.map((x) => x.toLowerCase());
|
||||
keys = keys.filter((key) =>
|
||||
domains.includes(key.toLowerCase().split(".")[0])
|
||||
);
|
||||
}
|
||||
if (restrictedIntegrations && entitySource.ready) {
|
||||
const integrations = restrictedIntegrations.map((x) => x.toLowerCase());
|
||||
keys = keys.filter((key) => {
|
||||
const integration = (
|
||||
entitySource.collection.state[key]?.domain ?? ""
|
||||
).toLowerCase();
|
||||
return integrations.includes(integration);
|
||||
});
|
||||
}
|
||||
return keys.map((key) => ({
|
||||
id: key,
|
||||
label: entities.collection.state[key].attributes.friendly_name ?? key,
|
||||
domain: key.split(".")[0] ?? "n/a",
|
||||
integration: entitySource.ready
|
||||
? (entitySource.collection.state[key] ?? {}).domain ?? "n/a"
|
||||
: "...",
|
||||
}));
|
||||
const deviceClasses = (restrictedDeviceClass ?? []).map((x) =>
|
||||
x.toLowerCase()
|
||||
);
|
||||
const domains = (restrictToDomain ?? []).map((x) => x.toLowerCase());
|
||||
const integrations = (restrictedIntegrations ?? []).map((x) =>
|
||||
x.toLowerCase()
|
||||
);
|
||||
return Object.keys(entities.collection.state)
|
||||
.filter((entityId) => {
|
||||
if (mode === "and") {
|
||||
let keep = true;
|
||||
if (domains.length > 0) {
|
||||
keep =
|
||||
keep && domains.includes(entityId.toLowerCase().split(".")[0]);
|
||||
}
|
||||
if (entitySource.ready && integrations.length > 0) {
|
||||
keep =
|
||||
keep &&
|
||||
integrations.includes(
|
||||
entitySource.collection.state[entityId]?.domain ?? ""
|
||||
);
|
||||
}
|
||||
if (deviceClasses.length > 0) {
|
||||
keep =
|
||||
keep &&
|
||||
deviceClasses.includes(
|
||||
entities.collection.state[
|
||||
entityId
|
||||
].attributes.device_class?.toLowerCase() ?? ""
|
||||
);
|
||||
}
|
||||
return keep;
|
||||
} else {
|
||||
if (
|
||||
!entitySource.ready &&
|
||||
deviceClasses.length === 0 &&
|
||||
domains.length === 0 &&
|
||||
integrations.length === 0
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
let keep = false;
|
||||
keep =
|
||||
keep || domains.includes(entityId.toLowerCase().split(".")[0]);
|
||||
if (entitySource.ready) {
|
||||
keep =
|
||||
keep ||
|
||||
integrations.includes(
|
||||
entitySource.collection.state[entityId]?.domain ?? ""
|
||||
);
|
||||
}
|
||||
keep =
|
||||
keep ||
|
||||
deviceClasses.includes(
|
||||
entities.collection.state[
|
||||
entityId
|
||||
].attributes.device_class?.toLowerCase() ?? ""
|
||||
);
|
||||
return keep;
|
||||
}
|
||||
})
|
||||
.map((key) => ({
|
||||
id: key,
|
||||
label: entities.collection.state[key].attributes.friendly_name ?? key,
|
||||
domain: key.split(".")[0] ?? "n/a",
|
||||
integration: entitySource.ready
|
||||
? (entitySource.collection.state[key] ?? {}).domain ?? "n/a"
|
||||
: "...",
|
||||
}));
|
||||
},
|
||||
getLabel: (opt: EntityOption): string => {
|
||||
if (typeof opt === "string") {
|
||||
|
||||
@@ -1,31 +1,57 @@
|
||||
import * as st from 'superstruct'
|
||||
import * as st from "superstruct";
|
||||
import * as v from "types/validators/triggers";
|
||||
|
||||
export type AutomationTriggerEvent = st.Infer<typeof v.AutomationTriggerEvent>;
|
||||
export type AutomationTriggerHA = st.Infer<typeof v.AutomationTriggerHA>;
|
||||
export type AutomationTriggerMQTT = st.Infer<typeof v.AutomationTriggerMQTT>;
|
||||
export type AutomationTriggerNumericState = st.Infer<typeof v.AutomationTriggerNumericState>;
|
||||
export type AutomationTriggerNumericState = st.Infer<
|
||||
typeof v.AutomationTriggerNumericState
|
||||
>;
|
||||
export type AutomationTriggerState = st.Infer<typeof v.AutomationTriggerState>;
|
||||
export type AutomationTriggerTag = st.Infer<typeof v.AutomationTriggerTag>;
|
||||
export type AutomationTriggerTemplate = st.Infer<typeof v.AutomationTriggerTemplate>;
|
||||
export type AutomationTriggerTemplate = st.Infer<
|
||||
typeof v.AutomationTriggerTemplate
|
||||
>;
|
||||
export type AutomationTriggerExactTime = st.Infer<
|
||||
typeof v.AutomationTriggerExactTime
|
||||
>;
|
||||
export type AutomationTriggerTime = st.Infer<typeof v.AutomationTriggerTime>;
|
||||
export type AutomationTriggerTimePattern = st.Infer<typeof v.AutomationTriggerTimePattern>;
|
||||
export type AutomationTriggerWebhook = st.Infer<typeof v.AutomationTriggerWebhook>;
|
||||
export type AutomationTriggerTimePattern = st.Infer<
|
||||
typeof v.AutomationTriggerTimePattern
|
||||
>;
|
||||
export type AutomationTriggerWebhook = st.Infer<
|
||||
typeof v.AutomationTriggerWebhook
|
||||
>;
|
||||
export type AutomationTriggerZone = st.Infer<typeof v.AutomationTriggerZone>;
|
||||
export type AutomationTriggerDevice = st.Infer<typeof v.AutomationTriggerDevice>;
|
||||
export type AutomationTriggerDevice = st.Infer<
|
||||
typeof v.AutomationTriggerDevice
|
||||
>;
|
||||
|
||||
export type ActionTriggerMapping = {
|
||||
homeassistant: AutomationTriggerHA;
|
||||
device: AutomationTriggerDevice;
|
||||
event: AutomationTriggerEvent;
|
||||
mqtt: AutomationTriggerMQTT;
|
||||
numeric_state: AutomationTriggerNumericState;
|
||||
state: AutomationTriggerState;
|
||||
tag: AutomationTriggerTag;
|
||||
template: AutomationTriggerTemplate;
|
||||
time: AutomationTriggerTime;
|
||||
webhook: AutomationTriggerWebhook;
|
||||
zone: AutomationTriggerZone;
|
||||
};
|
||||
|
||||
export type TriggerType = keyof ActionTriggerMapping;
|
||||
|
||||
export type AutomationTrigger =
|
||||
| AutomationTriggerEvent
|
||||
| AutomationTriggerHA
|
||||
| AutomationTriggerMQTT
|
||||
| AutomationTriggerNumericState
|
||||
| AutomationTriggerState
|
||||
| AutomationTriggerTag
|
||||
| AutomationTriggerTemplate
|
||||
| AutomationTriggerTime
|
||||
| AutomationTriggerTimePattern
|
||||
| AutomationTriggerWebhook
|
||||
| AutomationTriggerZone
|
||||
| AutomationTriggerDevice
|
||||
;
|
||||
export type TriggerType = AutomationTrigger['platform'];
|
||||
| AutomationTriggerEvent
|
||||
| AutomationTriggerHA
|
||||
| AutomationTriggerMQTT
|
||||
| AutomationTriggerNumericState
|
||||
| AutomationTriggerState
|
||||
| AutomationTriggerTag
|
||||
| AutomationTriggerTemplate
|
||||
| AutomationTriggerTime
|
||||
| AutomationTriggerWebhook
|
||||
| AutomationTriggerZone
|
||||
| AutomationTriggerDevice;
|
||||
@@ -67,10 +67,10 @@ export const AutomationTriggerTemplate = st.type({
|
||||
for: st.optional(AutomationTime),
|
||||
});
|
||||
|
||||
export const AutomationTriggerTime = st.type({
|
||||
export const AutomationTriggerExactTime = st.type({
|
||||
...AutomationTriggerBase,
|
||||
platform: st.literal("time"),
|
||||
at: AutomationTimeString,
|
||||
at: st.union([st.string(), st.array(st.string())]),
|
||||
});
|
||||
|
||||
export const AutomationTriggerTimePattern = st.type({
|
||||
@@ -81,6 +81,11 @@ export const AutomationTriggerTimePattern = st.type({
|
||||
seconds: st.optional(st.string()),
|
||||
});
|
||||
|
||||
export const AutomationTriggerTime = st.union([
|
||||
AutomationTriggerExactTime,
|
||||
AutomationTriggerTimePattern,
|
||||
]);
|
||||
|
||||
export const AutomationTriggerWebhook = st.type({
|
||||
...AutomationTriggerBase,
|
||||
platform: st.literal("webhook"),
|
||||
|
||||
@@ -70,7 +70,6 @@ export const getSubTypeList = <T extends AutomationNodeTypes>(nodeType: T) => {
|
||||
"state",
|
||||
"numeric_state",
|
||||
"time",
|
||||
"time_pattern",
|
||||
"zone",
|
||||
"event",
|
||||
"homeassistant",
|
||||
@@ -122,6 +121,9 @@ export const getNodeSubType = <
|
||||
if ("condition" in node) {
|
||||
return node.condition as any;
|
||||
} else if ("platform" in node) {
|
||||
if (node.platform === "time_pattern") {
|
||||
return "time" as any;
|
||||
}
|
||||
return node.platform as any;
|
||||
} else if ("service" in node) {
|
||||
return "service" as any;
|
||||
@@ -157,7 +159,6 @@ export const TriggerValidators: Record<TriggerType, any> = {
|
||||
tag: v.triggers.AutomationTriggerTag,
|
||||
template: v.triggers.AutomationTriggerTemplate,
|
||||
time: v.triggers.AutomationTriggerTime,
|
||||
time_pattern: v.triggers.AutomationTriggerTimePattern,
|
||||
webhook: v.triggers.AutomationTriggerWebhook,
|
||||
zone: v.triggers.AutomationTriggerZone,
|
||||
};
|
||||
|
||||
@@ -41,7 +41,16 @@ export const getDescriptionFromAutomationNode = <
|
||||
return node.id ?? "";
|
||||
}
|
||||
if (node.platform === "time") {
|
||||
return node.at;
|
||||
const out = `At `;
|
||||
if (Array.isArray(node.at)) {
|
||||
return out + node.at.join(" or ");
|
||||
}
|
||||
return out + node.at;
|
||||
}
|
||||
if (node.platform === "time_pattern") {
|
||||
return `At ${node.hours ?? "*"}:${node.minutes ?? "*"}:${
|
||||
node.seconds ?? "*"
|
||||
}`;
|
||||
}
|
||||
if (node.platform === "homeassistant") {
|
||||
if (node.event === "start") {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import { isAutomationTimeString24Hours } from "./time";
|
||||
|
||||
test("isAutomationTimeString24Hours to parse correctly", () => {
|
||||
expect(isAutomationTimeString24Hours("00:01")).toEqual(true);
|
||||
expect(isAutomationTimeString24Hours("10:01")).toEqual(true);
|
||||
expect(isAutomationTimeString24Hours("12:01")).toEqual(true);
|
||||
expect(isAutomationTimeString24Hours("12:01:01")).toEqual(true);
|
||||
expect(isAutomationTimeString24Hours("22:01:01")).toEqual(true);
|
||||
expect(isAutomationTimeString24Hours("23:01:01")).toEqual(true);
|
||||
|
||||
expect(isAutomationTimeString24Hours("@:01:01")).toEqual(false);
|
||||
expect(isAutomationTimeString24Hours("input_datetime.time")).toEqual(false);
|
||||
expect(isAutomationTimeString24Hours("sensor.me_yes")).toEqual(false);
|
||||
expect(isAutomationTimeString24Hours("25:01:01")).toEqual(false);
|
||||
});
|
||||
+68
-41
@@ -1,48 +1,75 @@
|
||||
import { AutomationTime, AutomationTimeObject, AutomationTimeString } from "types/automations/common";
|
||||
import {
|
||||
AutomationTime,
|
||||
AutomationTimeObject,
|
||||
AutomationTimeString,
|
||||
} from "types/automations/common";
|
||||
|
||||
const cleanTime = (t: number) => Math.max(0, Math.floor(t))
|
||||
const cleanTime = (t: number) => Math.max(0, Math.floor(t));
|
||||
|
||||
export const milisecondsToObject = (total_miliseconds: number): AutomationTimeObject => {
|
||||
const hours = cleanTime(total_miliseconds / (60 * 60 * 1000));
|
||||
const minutes = cleanTime((total_miliseconds - hours * 60 * 60 * 1000) / (60 * 1000));
|
||||
const seconds = cleanTime((total_miliseconds - hours * 60 * 60 * 1000 - minutes * 60 * 1000) / 1000);
|
||||
const milliseconds = total_miliseconds - hours * 60 * 60 * 1000 - minutes * 60 * 1000 - seconds * 1000;
|
||||
return {
|
||||
hours, minutes, seconds, milliseconds,
|
||||
}
|
||||
}
|
||||
export const milisecondsToObject = (
|
||||
total_miliseconds: number
|
||||
): AutomationTimeObject => {
|
||||
const hours = cleanTime(total_miliseconds / (60 * 60 * 1000));
|
||||
const minutes = cleanTime(
|
||||
(total_miliseconds - hours * 60 * 60 * 1000) / (60 * 1000)
|
||||
);
|
||||
const seconds = cleanTime(
|
||||
(total_miliseconds - hours * 60 * 60 * 1000 - minutes * 60 * 1000) / 1000
|
||||
);
|
||||
const milliseconds =
|
||||
total_miliseconds -
|
||||
hours * 60 * 60 * 1000 -
|
||||
minutes * 60 * 1000 -
|
||||
seconds * 1000;
|
||||
return {
|
||||
hours,
|
||||
minutes,
|
||||
seconds,
|
||||
milliseconds,
|
||||
};
|
||||
};
|
||||
|
||||
export const convertStringToAutomationTimeObject = (t: AutomationTimeString): AutomationTimeObject => {
|
||||
const bits = t.split(':')
|
||||
return {
|
||||
hours: Number(bits[0]),
|
||||
minutes: Number(bits[1]),
|
||||
seconds: bits.length === 3 ? Number(bits[2]) : 0,
|
||||
}
|
||||
}
|
||||
export const convertStringToAutomationTimeObject = (
|
||||
t: AutomationTimeString
|
||||
): AutomationTimeObject => {
|
||||
const bits = t.split(":");
|
||||
return {
|
||||
hours: Number(bits[0]),
|
||||
minutes: Number(bits[1]),
|
||||
seconds: bits.length === 3 ? Number(bits[2]) : 0,
|
||||
};
|
||||
};
|
||||
|
||||
export const convertObjectToAutomationTimeString = (t: AutomationTimeObject): AutomationTimeString => {
|
||||
const {
|
||||
hours = 0,
|
||||
minutes = 0,
|
||||
seconds = 0
|
||||
} = t
|
||||
return [hours, minutes, seconds].map(x => String(x).padStart(2, '0')).join(':')
|
||||
}
|
||||
export const convertObjectToAutomationTimeString = (
|
||||
t: AutomationTimeObject
|
||||
): AutomationTimeString => {
|
||||
const { hours = 0, minutes = 0, seconds = 0 } = t;
|
||||
return [hours, minutes, seconds]
|
||||
.map((x) => String(x).padStart(2, "0"))
|
||||
.join(":");
|
||||
};
|
||||
|
||||
export const convertAutomationTimeToTimeString = (t: AutomationTime | undefined): AutomationTimeString => {
|
||||
let obj: AutomationTimeObject;
|
||||
if (typeof t === 'string') {
|
||||
obj = convertStringToAutomationTimeObject(t)
|
||||
} else if (!t) {
|
||||
return "00:00:00"
|
||||
} else {
|
||||
obj = t
|
||||
}
|
||||
return convertObjectToAutomationTimeString(obj);
|
||||
}
|
||||
export const convertAutomationTimeToTimeString = (
|
||||
t: AutomationTime | undefined
|
||||
): AutomationTimeString => {
|
||||
let obj: AutomationTimeObject;
|
||||
if (typeof t === "string") {
|
||||
obj = convertStringToAutomationTimeObject(t);
|
||||
} else if (!t) {
|
||||
return "00:00:00";
|
||||
} else {
|
||||
obj = t;
|
||||
}
|
||||
return convertObjectToAutomationTimeString(obj);
|
||||
};
|
||||
|
||||
export const isAutomationTimeString24Hours = (t: string): boolean =>
|
||||
/^([0-1][0-9]|2[0-4]):[0-5][0-9](:[0-5][0-9])?/.test(t);
|
||||
|
||||
export const convertAutomationTimeToTimeObject = (t: AutomationTime | undefined): AutomationTimeObject => {
|
||||
return convertStringToAutomationTimeObject(convertAutomationTimeToTimeString(t))
|
||||
}
|
||||
export const convertAutomationTimeToTimeObject = (
|
||||
t: AutomationTime | undefined
|
||||
): AutomationTimeObject => {
|
||||
return convertStringToAutomationTimeObject(
|
||||
convertAutomationTimeToTimeString(t)
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user