From 2953ac45dd46d157068f8f900c3d619ce680c62d Mon Sep 17 00:00:00 2001 From: Ari Date: Wed, 13 Apr 2022 13:35:52 -0400 Subject: [PATCH] added support for more condition editors --- Makefile | 4 + api/src/hass_config/__init__.py | 2 + webapp/package.json | 4 +- webapp/public/index.html | 40 +++---- .../ConditionEditor/editorRender.tsx | 101 +++++++++++++++++- .../components/Inputs/InputTime.stories.tsx | 27 +++++ webapp/src/components/Inputs/InputTime.tsx | 56 ++++++++++ webapp/src/components/Inputs/InputWrapper.tsx | 5 +- webapp/src/components/Inputs/styles.ts | 26 +++++ webapp/src/utils/time.ts | 13 +++ 10 files changed, 253 insertions(+), 25 deletions(-) create mode 100644 webapp/src/components/Inputs/InputTime.stories.tsx create mode 100644 webapp/src/components/Inputs/InputTime.tsx create mode 100644 webapp/src/utils/time.ts diff --git a/Makefile b/Makefile index a506373..eebb95d 100644 --- a/Makefile +++ b/Makefile @@ -12,3 +12,7 @@ local_logs: local_ps: $(DC) ps + +local_stop: + $(DC) stop + diff --git a/api/src/hass_config/__init__.py b/api/src/hass_config/__init__.py index 0acfaaa..2e909c7 100644 --- a/api/src/hass_config/__init__.py +++ b/api/src/hass_config/__init__.py @@ -8,7 +8,9 @@ from .loader import HassConfig def find_hass_config() -> HassConfig: """Try to find HASS config.""" if config("USE_EXAMPLE_REPO", False, cast=bool): + print("using USE_EXAMPLE_REPO") from tests.utils import create_copy_of_example_config + return HassConfig(create_copy_of_example_config()) if "HASSIO_TOKEN" in os.environ: diff --git a/webapp/package.json b/webapp/package.json index 329173c..bd58ee3 100644 --- a/webapp/package.json +++ b/webapp/package.json @@ -2,7 +2,7 @@ "name": "webapp", "version": "0.1.0", "private": true, - "homepage": "/web", + "homepage": ".", "dependencies": { "@types/jest": "^26.0.15", "@types/node": "^12.0.0", @@ -71,4 +71,4 @@ "@types/js-yaml": "^4.0.5", "ts-node": "^10.4.0" } -} +} \ No newline at end of file diff --git a/webapp/public/index.html b/webapp/public/index.html index d84f0fe..a35c402 100644 --- a/webapp/public/index.html +++ b/webapp/public/index.html @@ -1,21 +1,19 @@ - - - - - - - - - - Shortumations - - - -
- - + Shortumations + + + + +
+ + + \ No newline at end of file diff --git a/webapp/src/components/ConditionEditor/editorRender.tsx b/webapp/src/components/ConditionEditor/editorRender.tsx index 6318e00..39403de 100644 --- a/webapp/src/components/ConditionEditor/editorRender.tsx +++ b/webapp/src/components/ConditionEditor/editorRender.tsx @@ -1,5 +1,5 @@ import { FC } from "react"; -import { AutomationCondition, LogicCondition, NumericCondition, TemplateCondition } from "types/automations/conditions"; +import { AutomationCondition, LogicCondition, NumericCondition, StateCondition, TemplateCondition, TimeCondition } from "types/automations/conditions"; import { getDescriptionFromAutomationNode } from "utils/formatting"; import InputNumber from "components/Inputs/InputNumber"; import InputText from "components/Inputs/InputText"; @@ -7,6 +7,7 @@ import InputTextArea from "components/Inputs/InputTextArea"; import { InputEntity } from "components/Inputs/InputTextBubble"; import { ConditionNode } from "./ConditionNode"; import { genUpdateMethods } from "./nestedUpdater"; +import InputTime from "components/Inputs/InputTime"; interface Editor extends FC<{ @@ -27,6 +28,10 @@ export const getEditor = (condition: AutomationCondition): Editor => { return LogicViewer; case 'numeric_state': return NumericStateEditor; + case 'state': + return StateEditor; + case 'time': + return TimeEditor; default: return () =>
Not Ready
} @@ -134,3 +139,97 @@ export const LogicViewer: Editor = ({ })} } + + + +export const StateEditor: Editor = ({ + onChange, + condition, +}) => { + return
+ onChange({ + ...condition, + condition_data: { + ...condition.condition_data, + alias + } + })} /> + onChange({ + ...condition, + condition_data: { + ...condition.condition_data, + entity_id + } + })} + /> + onChange({ + ...condition, + condition_data: { + ...condition.condition_data, + attribute + } + })} /> + onChange({ + ...condition, + condition_data: { + ...condition.condition_data, + state, + } + })} + /> + onChange({ + ...condition, + condition_data: { + ...condition.condition_data, + for: _for, + } + })} + /> +
+} + +export const TimeEditor: Editor = ({ + onChange, + condition, +}) => { + return
+ onChange({ + ...condition, + condition_data: { + ...condition.condition_data, + alias + } + })} /> + onChange({ + ...condition, + condition_data: { + ...condition.condition_data, + after, + } + })} + /> + onChange({ + ...condition, + condition_data: { + ...condition.condition_data, + before, + } + })} + /> + Weekday not support for no, use yaml for now {condition.condition_data.weekday} +
+} \ No newline at end of file diff --git a/webapp/src/components/Inputs/InputTime.stories.tsx b/webapp/src/components/Inputs/InputTime.stories.tsx new file mode 100644 index 0000000..cb2ba68 --- /dev/null +++ b/webapp/src/components/Inputs/InputTime.stories.tsx @@ -0,0 +1,27 @@ +import { ComponentMeta, ComponentStory } from "@storybook/react"; +import { useState } from "react"; +import { AutomationTime } from "types/automations/common"; + +import InputTime from "./InputTime"; + +export default { + title: 'Inputs/InputTime', + component: InputTime, + parameters: { actions: { argTypesRegex: '^on.*' } }, +} as ComponentMeta; + + +const Template: ComponentStory = args => { + + const [value, setValue] = useState({ + + }) + return
+ +
+} + +export const SimpleText = Template.bind({}) +SimpleText.args = { + label: "Time Stamp", +} diff --git a/webapp/src/components/Inputs/InputTime.tsx b/webapp/src/components/Inputs/InputTime.tsx new file mode 100644 index 0000000..1c1542c --- /dev/null +++ b/webapp/src/components/Inputs/InputTime.tsx @@ -0,0 +1,56 @@ +import { PropsWithChildren, useState } from "react"; +import { AutomationTime } from "types/automations/common"; +import { milisecondsToObject } from "utils/time"; +import InputWrapper from "./InputWrapper"; +import { useInputTimeStyles } from "./styles"; + +export interface Props { + textBoxFor?: string; + label: string; + value?: AutomationTime; + onChange: (v?: AutomationTime) => void; + onEnter?: () => void; +} +export default function InputTime({ + label, + textBoxFor, + value = {}, + onChange, + onEnter = () => { }, + children, +}: PropsWithChildren) { + let cleanValue = ''; + if (value) { + const { + hours, + minutes, + seconds, + milliseconds, + } = value; + cleanValue = [hours, minutes, seconds].map(x => String(x).padStart(2, '0')).join(':') + `.${String(milliseconds).padStart(3, '0')}`; + } + const { classes } = useInputTimeStyles({}); + const [isFocused, setIsFocused] = useState(false) + return + { + if (e.key === 'Enter') { + onEnter(); + } + }} + onChange={e => { + e.preventDefault(); + onChange(milisecondsToObject(e.target.valueAsNumber)) + }} + onBlur={e => { + setIsFocused(false) + }} + /> + + {children} + +} diff --git a/webapp/src/components/Inputs/InputWrapper.tsx b/webapp/src/components/Inputs/InputWrapper.tsx index 99cb0db..45bdeb0 100644 --- a/webapp/src/components/Inputs/InputWrapper.tsx +++ b/webapp/src/components/Inputs/InputWrapper.tsx @@ -6,10 +6,11 @@ export interface Props { label: string; labelSize?: 'normal' | 'small'; noMargin?: boolean; + className?: string; } -const InputWrapper: FC = ({ label, children, labelSize = 'normal', noMargin = false }) => { +const InputWrapper: FC = ({ label, children, labelSize = 'normal', noMargin = false, className = "" }) => { const { classes } = useInputWrapperStyles({ labelSize, noMargin }) - return
+ return
{children}
diff --git a/webapp/src/components/Inputs/styles.ts b/webapp/src/components/Inputs/styles.ts index 441b7c4..53ba6a9 100644 --- a/webapp/src/components/Inputs/styles.ts +++ b/webapp/src/components/Inputs/styles.ts @@ -62,6 +62,32 @@ export const useInputTextStyles = createAppUseStyles<{}>(theme => ({ ...commonInputStyle(theme), })); + +export const useInputTimeStyles = createAppUseStyles<{}>(theme => ({ + input: { + ...commonInputStyle(theme).input, + flex: 1, + }, + wrapper: { + flexDirection: 'row !important', + justifyContent: 'center', + alignItems: 'center', + }, + cancel: { + cursor: 'pointer', + maxWidth: "2em", + maxHeight: "2em", + fontSize: '1em', + padding: "0.25em 0.5em", + borderRadius: '20px', + background: theme.primary, + color: theme.primaryAccent, + borderColor: theme.primaryAccent, + borderStyle: "solid", + borderWidth: '1.5px', + }, +})); + export const useInputNumberStyles = createAppUseStyles<{}>(theme => { const styles = commonInputStyle(theme); return { diff --git a/webapp/src/utils/time.ts b/webapp/src/utils/time.ts new file mode 100644 index 0000000..be77e29 --- /dev/null +++ b/webapp/src/utils/time.ts @@ -0,0 +1,13 @@ +import { AutomationTime } from "types/automations/common"; + +const cleanTime = (t: number) => Math.max(0, Math.floor(t)) + +export const milisecondsToObject = (total_miliseconds: number): AutomationTime => { + 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, + } +} \ No newline at end of file