From 3a44057a4056301d2618058fd8db14a288772569 Mon Sep 17 00:00:00 2001 From: Ari Date: Wed, 20 Jul 2022 21:21:02 -0400 Subject: [PATCH] cleaned up time trigger --- .../src/components/Inputs/InputEntities.tsx | 6 +- .../components/Inputs/InputTime.stories.tsx | 44 ++-- webapp/src/components/Inputs/InputTime.tsx | 19 +- .../NodeEditor/OptionManager/TriggerTime.css | 82 +++++++ .../NodeEditor/OptionManager/TriggerTime.tsx | 210 ++++++++++++++++-- .../OptionManager/TriggerTimePattern.tsx | 27 --- .../OptionManager/getOptionManager.ts | 3 - webapp/src/haService/HAEntities.ts | 97 +++++--- webapp/src/types/automations/triggers.ts | 66 ++++-- webapp/src/types/validators/triggers.ts | 9 +- webapp/src/utils/automations.ts | 5 +- webapp/src/utils/formatting.tsx | 11 +- webapp/src/utils/time.test.ts | 15 ++ webapp/src/utils/time.ts | 109 +++++---- 14 files changed, 536 insertions(+), 167 deletions(-) create mode 100644 webapp/src/components/NodeEditor/OptionManager/TriggerTime.css delete mode 100644 webapp/src/components/NodeEditor/OptionManager/TriggerTimePattern.tsx create mode 100644 webapp/src/utils/time.test.ts diff --git a/webapp/src/components/Inputs/InputEntities.tsx b/webapp/src/components/Inputs/InputEntities.tsx index a71ac61..f9d4cca 100644 --- a/webapp/src/components/Inputs/InputEntities.tsx +++ b/webapp/src/components/Inputs/InputEntities.tsx @@ -10,6 +10,8 @@ export type InputEntityProps = InputAutoCompletePropsBase & { restrictToDomain?: string[]; restrictedIntegrations?: string[]; preSelectedEntityIds?: string[]; + restrictedDeviceClass?: string[]; + restrictMode?: "and" | "or"; }; export const InputEntity: FC = (props) => { @@ -17,7 +19,9 @@ export const InputEntity: FC = (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) => diff --git a/webapp/src/components/Inputs/InputTime.stories.tsx b/webapp/src/components/Inputs/InputTime.stories.tsx index 61189fe..a3422bd 100644 --- a/webapp/src/components/Inputs/InputTime.stories.tsx +++ b/webapp/src/components/Inputs/InputTime.stories.tsx @@ -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; - -const Template: ComponentStory = args => { - +const Template: ComponentStory = (args) => { const [value, setValue] = useState(undefined); - return -
- { - setValue(t) - args.onChange(t) - }} /> -
-
-} + return ( + +
+ { + setValue(t); + args.onChange(t); + }} + /> +
+
+ ); +}; -export const SimpleText = Template.bind({}) +export const SimpleText = Template.bind({}); SimpleText.args = { label: "Time Stamp", - value: "10" -} + value: "10", +}; diff --git a/webapp/src/components/Inputs/InputTime.tsx b/webapp/src/components/Inputs/InputTime.tsx index 7196735..a217ccc 100644 --- a/webapp/src/components/Inputs/InputTime.tsx +++ b/webapp/src/components/Inputs/InputTime.tsx @@ -19,13 +19,14 @@ const fields: Array = [ "seconds", "milliseconds", ]; -export interface Props { +export type InputTimeProps = { label: string; - value?: AutomationTime; onChange: (v?: AutomationTimeObject) => void; className?: string; -} -export const InputTime: FC = (props) => { + value?: AutomationTime; + restrictEmpty?: boolean; +}; +export const InputTime: FC = (props) => { // state const [disabled, setDisabled] = useState(!props.value); const [displayValue, setDisplayValue] = useState( @@ -140,10 +141,12 @@ export const InputTime: FC = (props) => { onChange={(e) => updateOne(name)(e.target.value)} /> ))} - setDisabled(!disabled)} - icon={disabled ? : } - /> + {!props.restrictEmpty && ( + setDisabled(!disabled)} + icon={disabled ? : } + /> + )} ); diff --git a/webapp/src/components/NodeEditor/OptionManager/TriggerTime.css b/webapp/src/components/NodeEditor/OptionManager/TriggerTime.css new file mode 100644 index 0000000..190bb3e --- /dev/null +++ b/webapp/src/components/NodeEditor/OptionManager/TriggerTime.css @@ -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; +} \ No newline at end of file diff --git a/webapp/src/components/NodeEditor/OptionManager/TriggerTime.tsx b/webapp/src/components/NodeEditor/OptionManager/TriggerTime.tsx index 86b2a12..0eddaf1 100644 --- a/webapp/src/components/NodeEditor/OptionManager/TriggerTime.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/TriggerTime.tsx @@ -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 = { defaultState: () => ({ - "platform": 'time', - "at": "", + platform: "time", + at: "00:00:00", }), isReady: () => true, - renderOptionList: (state, setState) => { - return <> - 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 ( + <> +
+ + {state.platform === "time_pattern" && ( + <> + + + )} + {state.platform === "time_pattern" && showHelp && ( +
+ You can prefix the value with a "/" to match whenever the value is + divisible by that number. You can specify "*" to match any value.{" "} + + More Info. + +
+ )} +
+ {state.platform === "time_pattern" ? ( + + ) : ( + + )} + + ); + }, +}; + +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 ( +
+ {times.map((v, i) => { + let editor: JSX.Element; + const is24Hour = isAutomationTimeString24Hours(v); + if (is24Hour) { + editor = ( + + update( + _at ? convertObjectToAutomationTimeString(_at) : "00:00:00", + i + ) + } + /> + ); + } else { + editor = ( + update(_at ?? "", i)} + /> + ); + } + return ( +
+ {editor}{" "} + : + } + onClick={() => { + if (is24Hour) { + update("", i); + } else { + update("00:00:00", i); + } + }} + /> +
+ ); + })} + +
+ ); +}; + +export const TriggerTimePattern: FC<{ + state: AutomationTriggerTimePattern; + setState: (t: AutomationTriggerTimePattern) => void; +}> = ({ state, setState }) => { + return ( +
+ + setState({ + ...state, + hours, + }) + } /> - - } -} + + setState({ + ...state, + minutes, + }) + } + /> + + setState({ + ...state, + seconds, + }) + } + /> +
+ ); +}; diff --git a/webapp/src/components/NodeEditor/OptionManager/TriggerTimePattern.tsx b/webapp/src/components/NodeEditor/OptionManager/TriggerTimePattern.tsx deleted file mode 100644 index a2a1a7a..0000000 --- a/webapp/src/components/NodeEditor/OptionManager/TriggerTimePattern.tsx +++ /dev/null @@ -1,27 +0,0 @@ -import { OptionManager } from './OptionManager'; -import { AutomationTriggerTimePattern } from 'types/automations/triggers'; -import InputText from 'components/Inputs/InputText'; - - -export const TriggerTimePattern: OptionManager = { - defaultState: () => ({ - "platform": 'time_pattern', - }), - isReady: () => true, - renderOptionList: (state, setState) => { - return <> - setState({ - ...state, - hours - })} /> - setState({ - ...state, - minutes - })} /> - setState({ - ...state, - seconds - })} /> - - } -} diff --git a/webapp/src/components/NodeEditor/OptionManager/getOptionManager.ts b/webapp/src/components/NodeEditor/OptionManager/getOptionManager.ts index b26a726..c2d273d 100644 --- a/webapp/src/components/NodeEditor/OptionManager/getOptionManager.ts +++ b/webapp/src/components/NodeEditor/OptionManager/getOptionManager.ts @@ -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 = ( return TriggerTemplate; case "time": return TriggerTime; - case "time_pattern": - return TriggerTimePattern; case "webhook": return TriggerWebhook; case "zone": diff --git a/webapp/src/haService/HAEntities.ts b/webapp/src/haService/HAEntities.ts index 772846d..dcb83b5 100644 --- a/webapp/src/haService/HAEntities.ts +++ b/webapp/src/haService/HAEntities.ts @@ -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") { diff --git a/webapp/src/types/automations/triggers.ts b/webapp/src/types/automations/triggers.ts index 66e8289..467ee8a 100644 --- a/webapp/src/types/automations/triggers.ts +++ b/webapp/src/types/automations/triggers.ts @@ -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; export type AutomationTriggerHA = st.Infer; export type AutomationTriggerMQTT = st.Infer; -export type AutomationTriggerNumericState = st.Infer; +export type AutomationTriggerNumericState = st.Infer< + typeof v.AutomationTriggerNumericState +>; export type AutomationTriggerState = st.Infer; export type AutomationTriggerTag = st.Infer; -export type AutomationTriggerTemplate = st.Infer; +export type AutomationTriggerTemplate = st.Infer< + typeof v.AutomationTriggerTemplate +>; +export type AutomationTriggerExactTime = st.Infer< + typeof v.AutomationTriggerExactTime +>; export type AutomationTriggerTime = st.Infer; -export type AutomationTriggerTimePattern = st.Infer; -export type AutomationTriggerWebhook = st.Infer; +export type AutomationTriggerTimePattern = st.Infer< + typeof v.AutomationTriggerTimePattern +>; +export type AutomationTriggerWebhook = st.Infer< + typeof v.AutomationTriggerWebhook +>; export type AutomationTriggerZone = st.Infer; -export type AutomationTriggerDevice = st.Infer; +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; diff --git a/webapp/src/types/validators/triggers.ts b/webapp/src/types/validators/triggers.ts index 64f3dff..b175c39 100644 --- a/webapp/src/types/validators/triggers.ts +++ b/webapp/src/types/validators/triggers.ts @@ -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"), diff --git a/webapp/src/utils/automations.ts b/webapp/src/utils/automations.ts index 0cad2b3..94fe1bf 100644 --- a/webapp/src/utils/automations.ts +++ b/webapp/src/utils/automations.ts @@ -70,7 +70,6 @@ export const getSubTypeList = (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 = { 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, }; diff --git a/webapp/src/utils/formatting.tsx b/webapp/src/utils/formatting.tsx index b419654..088ac05 100644 --- a/webapp/src/utils/formatting.tsx +++ b/webapp/src/utils/formatting.tsx @@ -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") { diff --git a/webapp/src/utils/time.test.ts b/webapp/src/utils/time.test.ts new file mode 100644 index 0000000..12a3e60 --- /dev/null +++ b/webapp/src/utils/time.test.ts @@ -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); +}); diff --git a/webapp/src/utils/time.ts b/webapp/src/utils/time.ts index 77484b8..11e5eca 100644 --- a/webapp/src/utils/time.ts +++ b/webapp/src/utils/time.ts @@ -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)) -} \ No newline at end of file +export const convertAutomationTimeToTimeObject = ( + t: AutomationTime | undefined +): AutomationTimeObject => { + return convertStringToAutomationTimeObject( + convertAutomationTimeToTimeString(t) + ); +};