From 184d8a8584e32e3f5c8ec255318ede4d94b07a1f Mon Sep 17 00:00:00 2001 From: Ari Date: Tue, 14 Jun 2022 21:52:16 -0400 Subject: [PATCH] added entity input for service target --- .../components/Inputs/InputAutoComplete.tsx | 9 +- .../components/NodeEditor/index.stories.tsx | 130 +++--- .../ServiceEditor/ServiceEditorField.tsx | 16 + webapp/src/components/ServiceEditor/index.css | 13 + .../ServiceEditor/index.stories.tsx | 389 +++++++++++++++++- webapp/src/components/ServiceEditor/index.tsx | 83 ++-- .../src/components/ServiceEditor/options.ts | 22 +- 7 files changed, 540 insertions(+), 122 deletions(-) diff --git a/webapp/src/components/Inputs/InputAutoComplete.tsx b/webapp/src/components/Inputs/InputAutoComplete.tsx index 83a8cb0..62502f5 100644 --- a/webapp/src/components/Inputs/InputAutoComplete.tsx +++ b/webapp/src/components/Inputs/InputAutoComplete.tsx @@ -60,9 +60,11 @@ export function InputAutoComplete( ? [props.value] : []; const errors = props.validateOption ? props.validateOption(value) : null; - const options = props.options.sort((a, b) => { - return getID(a) > getID(b) ? 1 : -1; - }); + const options = props.options + .filter((opt) => !value.includes(getID(opt))) + .sort((a, b) => { + return getID(a) > getID(b) ? 1 : -1; + }); // processing of errors let helperText = props.helperText ?? <>; @@ -104,7 +106,6 @@ export function InputAutoComplete( props.onChange(cleanValue[cleanValue.length - 1]); } }; - return ( + data: {}, + service: "", + target: "", + }, + }, +} as ComponentMeta; -const Base: ComponentStory = props => { - - return - - +const Base: ComponentStory = (props) => { + return ( + + + + ); }; -export const Action = Base.bind({}) -export const SingleOption = Action.bind({}) +export const Action = Base.bind({}); +export const SingleOption = Action.bind({}); SingleOption.args = { ...SingleOption.args, - allowedTypes: ['action'], -} -export const Condition = Action.bind({}) + allowedTypes: ["action"], +}; +export const Condition = Action.bind({}); Condition.args = { ...Condition.args, node: { - "condition": "and", - "conditions": [ + condition: "and", + conditions: [ { - "condition": "numeric_state", - "entity_id": [ - "sensor.kitchen_humidity" - ], - "conditions": [], - "above": "40" + condition: "numeric_state", + entity_id: ["sensor.kitchen_humidity"], + conditions: [], + above: "40", }, { - "condition": "template", - "value_template": "states('switch.kitchen') == 'on'" - } - ] + condition: "template", + value_template: "states('switch.kitchen') == 'on'", + }, + ], } as AutomationCondition, -} +}; -export const UnSupported = Action.bind({}) +export const UnSupported = Action.bind({}); UnSupported.args = { ...UnSupported.args, node: { - "condition": "bargs", - "platform": "rouge" + condition: "bargs", + platform: "rouge", } as any, -} +}; -export const DeviceTriggerExample = Action.bind({}) +export const DeviceTriggerExample = Action.bind({}); DeviceTriggerExample.args = { ...DeviceTriggerExample.args, node: { - "platform": 'device', + platform: "device", }, -} - - -export const InAModal: ComponentStory = props => { - - const [open, setOpen] = useState(false); - return - - - setOpen(!open)} /> - - }; +export const InAModal: ComponentStory = (props) => { + const [open, setOpen] = useState(false); + return ( + + + + setOpen(!open)} /> + + + ); +}; -export const ServiceExample = Action.bind({}) +export const ServiceExample = Action.bind({}); ServiceExample.args = { ...ServiceExample.args, node: { - "target": {}, - "data": {}, - "service": "", + target: {}, + data: {}, + service: "", }, -} +}; +export const ServiceWithDataExample = Action.bind({}); +ServiceWithDataExample.args = { + ...ServiceWithDataExample.args, + node: { + target: { + entity: "light.switch_baby", + }, + data: { + color_name: "red", + }, + service: "light.turn_on", + }, +}; -export const DelayExample = Action.bind({}) +export const DelayExample = Action.bind({}); DelayExample.args = { ...DelayExample.args, node: { - "wait_template": "", + wait_template: "", }, -} +}; diff --git a/webapp/src/components/ServiceEditor/ServiceEditorField.tsx b/webapp/src/components/ServiceEditor/ServiceEditorField.tsx index 5914700..ee743f9 100644 --- a/webapp/src/components/ServiceEditor/ServiceEditorField.tsx +++ b/webapp/src/components/ServiceEditor/ServiceEditorField.tsx @@ -5,6 +5,7 @@ import InputText from "components/Inputs/InputText"; import InputNumber from "components/Inputs/InputNumber"; import { IconButton } from "@mui/material"; import { RemoveCircle } from "@mui/icons-material"; +import { InputEntity } from "components/Inputs/InputEntities"; export type ServiceEditorFieldProps = { option: ServiceEditorOption; @@ -71,6 +72,21 @@ export const ServiceEditorField: FC = ({ endAdornment={removeIcon} /> ); + } else { + if ("domain" in option.data) { + if (option.id === "entity") { + return ( + + ); + } + } } return input; diff --git a/webapp/src/components/ServiceEditor/index.css b/webapp/src/components/ServiceEditor/index.css index 31101e5..93b1929 100644 --- a/webapp/src/components/ServiceEditor/index.css +++ b/webapp/src/components/ServiceEditor/index.css @@ -1,6 +1,19 @@ .service-editor { display: flex; flex-direction: column; + padding: 0.5em 1em; + margin: 0.5em 1em; + border-color: var(--primary-accent); + border-width: 1px; + border-style: solid; + border-radius: 5px; + background-color: var(--secondary); +} + +.service-editor hr { + width: 90%; + margin-top: 0.5em; + margin-bottom: 1em; } .service-editor h4 { diff --git a/webapp/src/components/ServiceEditor/index.stories.tsx b/webapp/src/components/ServiceEditor/index.stories.tsx index c23b148..d404f68 100644 --- a/webapp/src/components/ServiceEditor/index.stories.tsx +++ b/webapp/src/components/ServiceEditor/index.stories.tsx @@ -3,6 +3,7 @@ import { ComponentMeta, ComponentStory } from "@storybook/react"; import { Page } from "components/Page"; import { ServiceEditor } from "."; import { TypedHassService } from "haService/fieldTypes"; +import InputYaml from "components/Inputs/InputYaml"; export default { title: "Services/ServiceEditor", @@ -12,10 +13,45 @@ export default { } as ComponentMeta; const Template: ComponentStory = (props) => { + const [state, setState] = useState(props.data); return ( - - {JSON.stringify(props.service)} + { + setState(d); + props.onUpdate(d); + }} + /> +
+ {}} /> +
+
+ {}} /> +
+
+ {}} + /> +
); }; @@ -25,6 +61,10 @@ const makeExample = (service: TypedHassService) => { Eg.args = { ...Eg, service, + data: { + target: {}, + field: {}, + }, }; return Eg; }; @@ -42,3 +82,348 @@ export const NumberSetValue = makeExample({ }, target: { entity: { domain: "number" } }, }); + +export const LightTurnOn = makeExample({ + name: "Turn on", + description: + "Turn on one or more lights and adjust properties of the light, even when they are turned on already.\n", + target: { + entity: { + domain: "light", + }, + }, + fields: { + transition: { + name: "Transition", + description: "Duration it takes to get to next state.", + selector: { + number: { + min: 0, + max: 300, + unit_of_measurement: "seconds", + }, + }, + }, + rgb_color: { + name: "Color", + description: "The color for the light (based on RGB - red, green, blue).", + selector: { + color_rgb: null, + }, + }, + rgbw_color: { + name: "RGBW-color", + description: + "A list containing four integers between 0 and 255 representing the RGBW (red, green, blue, white) color for the light.", + advanced: true, + example: "[255, 100, 100, 50]", + selector: { + object: null, + }, + }, + rgbww_color: { + name: "RGBWW-color", + description: + "A list containing five integers between 0 and 255 representing the RGBWW (red, green, blue, cold white, warm white) color for the light.", + advanced: true, + example: "[255, 100, 100, 50, 70]", + selector: { + object: null, + }, + }, + color_name: { + name: "Color name", + description: "A human readable color name.", + advanced: true, + selector: { + select: { + options: [ + "homeassistant", + "aliceblue", + "antiquewhite", + "aqua", + "aquamarine", + "azure", + "beige", + "bisque", + "blanchedalmond", + "blue", + "blueviolet", + "brown", + "burlywood", + "cadetblue", + "chartreuse", + "chocolate", + "coral", + "cornflowerblue", + "cornsilk", + "crimson", + "cyan", + "darkblue", + "darkcyan", + "darkgoldenrod", + "darkgray", + "darkgreen", + "darkgrey", + "darkkhaki", + "darkmagenta", + "darkolivegreen", + "darkorange", + "darkorchid", + "darkred", + "darksalmon", + "darkseagreen", + "darkslateblue", + "darkslategray", + "darkslategrey", + "darkturquoise", + "darkviolet", + "deeppink", + "deepskyblue", + "dimgray", + "dimgrey", + "dodgerblue", + "firebrick", + "floralwhite", + "forestgreen", + "fuchsia", + "gainsboro", + "ghostwhite", + "gold", + "goldenrod", + "gray", + "green", + "greenyellow", + "grey", + "honeydew", + "hotpink", + "indianred", + "indigo", + "ivory", + "khaki", + "lavender", + "lavenderblush", + "lawngreen", + "lemonchiffon", + "lightblue", + "lightcoral", + "lightcyan", + "lightgoldenrodyellow", + "lightgray", + "lightgreen", + "lightgrey", + "lightpink", + "lightsalmon", + "lightseagreen", + "lightskyblue", + "lightslategray", + "lightslategrey", + "lightsteelblue", + "lightyellow", + "lime", + "limegreen", + "linen", + "magenta", + "maroon", + "mediumaquamarine", + "mediumblue", + "mediumorchid", + "mediumpurple", + "mediumseagreen", + "mediumslateblue", + "mediumspringgreen", + "mediumturquoise", + "mediumvioletred", + "midnightblue", + "mintcream", + "mistyrose", + "moccasin", + "navajowhite", + "navy", + "navyblue", + "oldlace", + "olive", + "olivedrab", + "orange", + "orangered", + "orchid", + "palegoldenrod", + "palegreen", + "paleturquoise", + "palevioletred", + "papayawhip", + "peachpuff", + "peru", + "pink", + "plum", + "powderblue", + "purple", + "red", + "rosybrown", + "royalblue", + "saddlebrown", + "salmon", + "sandybrown", + "seagreen", + "seashell", + "sienna", + "silver", + "skyblue", + "slateblue", + "slategray", + "slategrey", + "snow", + "springgreen", + "steelblue", + "tan", + "teal", + "thistle", + "tomato", + "turquoise", + "violet", + "wheat", + "white", + "whitesmoke", + "yellow", + "yellowgreen", + ], + }, + }, + }, + hs_color: { + name: "Hue/Sat color", + description: + "Color for the light in hue/sat format. Hue is 0-360 and Sat is 0-100.", + advanced: true, + example: "[300, 70]", + selector: { + object: null, + }, + }, + xy_color: { + name: "XY-color", + description: "Color for the light in XY-format.", + advanced: true, + example: "[0.52, 0.43]", + selector: { + object: null, + }, + }, + color_temp: { + name: "Color temperature", + description: "Color temperature for the light in mireds.", + selector: { + color_temp: { + min_mireds: 153, + max_mireds: 500, + }, + }, + }, + kelvin: { + name: "Color temperature (Kelvin)", + description: "Color temperature for the light in Kelvin.", + advanced: true, + selector: { + number: { + min: 2000, + max: 6500, + step: 100, + unit_of_measurement: "K", + }, + }, + }, + brightness: { + name: "Brightness value", + description: + "Number indicating brightness, where 0 turns the light off, 1 is the minimum brightness and 255 is the maximum brightness supported by the light.", + advanced: true, + selector: { + number: { + min: 0, + max: 255, + }, + }, + }, + brightness_pct: { + name: "Brightness", + description: + "Number indicating percentage of full brightness, where 0 turns the light off, 1 is the minimum brightness and 100 is the maximum brightness supported by the light.", + selector: { + number: { + min: 0, + max: 100, + unit_of_measurement: "%", + }, + }, + }, + brightness_step: { + name: "Brightness step value", + description: "Change brightness by an amount.", + advanced: true, + selector: { + number: { + min: -225, + max: 255, + }, + }, + }, + brightness_step_pct: { + name: "Brightness step", + description: "Change brightness by a percentage.", + selector: { + number: { + min: -100, + max: 100, + unit_of_measurement: "%", + }, + }, + }, + white: { + name: "White", + description: + "Set the light to white mode and change its brightness, where 0 turns the light off, 1 is the minimum brightness and 255 is the maximum brightness supported by the light.", + advanced: true, + selector: { + number: { + min: 0, + max: 255, + }, + }, + }, + profile: { + name: "Profile", + description: "Name of a light profile to use.", + advanced: true, + example: "relax", + selector: { + text: null, + }, + }, + flash: { + name: "Flash", + description: "If the light should flash.", + advanced: true, + selector: { + select: { + options: [ + { + label: "Long", + value: "long", + }, + { + label: "Short", + value: "short", + }, + ], + }, + }, + }, + effect: { + name: "Effect", + description: "Light effect.", + selector: { + text: null, + }, + }, + }, +}); diff --git a/webapp/src/components/ServiceEditor/index.tsx b/webapp/src/components/ServiceEditor/index.tsx index 359797f..1b087c0 100644 --- a/webapp/src/components/ServiceEditor/index.tsx +++ b/webapp/src/components/ServiceEditor/index.tsx @@ -1,6 +1,5 @@ import "./index.css"; -import { InputList } from "components/Inputs/InputList"; import { cleanUpUndefined } from "components/NodeEditor/OptionManager/OptionManager"; import { TypedHassService } from "haService/fieldTypes"; import { FC, useState } from "react"; @@ -9,14 +8,11 @@ import { splitUpOptions, convertFieldToOption, convertTargetToOption, + ServiceEditorData, } from "./options"; import { ServiceEditorField } from "./ServiceEditorField"; import { InputAutoComplete } from "components/Inputs/InputAutoComplete"; -export type ServiceEditorData = { - field: Record; - target: Record; -}; export type ServiceEditorProps = { serviceId: string; service: TypedHassService; @@ -29,23 +25,25 @@ export const ServiceEditor: FC = ({ data, onUpdate, }) => { - const options = splitUpOptions(service); + const options = splitUpOptions(service, data); + console.log(service); const [additionalFields, setAdditionalFields] = useState<{ field: string[]; - target: string[]; optionalList: ServiceEditorOption[]; }>({ - field: [], - target: [], + field: options.incmFields, optionalList: options.additional, }); // aliases const hasRequired = options.required.length > 0; - const hasOptional = - additionalFields.field.length + additionalFields.target.length > 0; + const hasTargets = !!service.target && Object.keys(service.target).length > 0; + const hasOptional = additionalFields.field.length > 0; const hasNothing = - !hasRequired && !hasOptional && additionalFields.optionalList.length === 0; + !hasRequired && + !hasOptional && + !hasTargets && + additionalFields.optionalList.length === 0; // render return ( @@ -76,6 +74,30 @@ export const ServiceEditor: FC = ({ } /> ))} + {hasTargets && ( +
+ {Object.keys(service.target ?? {}).map((key) => { + const targetData = (service.target as any)[key]; + const option = convertTargetToOption(key, targetData); + return ( + + onUpdate({ + ...data, + target: { + ...data.target, + [key]: v, + }, + }) + } + /> + ); + })} +
+ )}
{additionalFields.field.map((key) => { const serviceData = service.fields[key]; @@ -112,45 +134,10 @@ export const ServiceEditor: FC = ({ /> ); })} - {additionalFields.target.map((key) => { - const targetData = (service.target as any)[key]; - const option = convertTargetToOption(key, targetData); - return ( - { - setAdditionalFields({ - ...additionalFields, - target: additionalFields.target.filter((k) => k !== key), - optionalList: - additionalFields.optionalList.concat(option), - }); - onUpdate({ - ...data, - target: cleanUpUndefined({ - ...data.target, - [key]: undefined, - }), - }); - }} - onChange={(v) => - onUpdate({ - ...data, - target: { - ...data.target, - [key]: v, - }, - }) - } - /> - ); - })}
)} -
+
{additionalFields.optionalList.length > 0 && (
; + target: Record; +}; export type ServiceEditorOption = BaseOption< | { type: "field"; @@ -32,21 +37,22 @@ export const convertTargetToOption = (id: string, data: TargetData): ServiceEdit }, }) -export const splitUpOptions = (service: TypedHassService) => { +export const splitUpOptions = (service: TypedHassService, data: ServiceEditorData) => { const required: ServiceEditorOption[] = []; const additional: ServiceEditorOption[] = []; + const incmFields: string[] = []; Object.keys(service.fields ?? {}).forEach((key) => { const serviceData = service.fields[key]; const opt = convertFieldToOption(key, serviceData); if (serviceData.required) { required.push(opt); } else { - additional.push(opt); + if (data.field[opt.id]) { + incmFields.push(opt.id) + } else { + additional.push(opt); + } } }); - Object.keys(service.target ?? {}).forEach((key) =>{ - const serviceData = (service.target ?? ({} as any))[key]; - additional.push(convertTargetToOption(key, serviceData)) - }); - return { required, additional }; + return { required, additional, incmFields }; };