diff --git a/webapp/src/components/AutomationList/AutomationList.stories.tsx b/webapp/src/components/AutomationList/AutomationList.stories.tsx index 1de1738..69f8721 100644 --- a/webapp/src/components/AutomationList/AutomationList.stories.tsx +++ b/webapp/src/components/AutomationList/AutomationList.stories.tsx @@ -2,7 +2,7 @@ import "styles/root.css"; import { ComponentMeta, ComponentStory } from "@storybook/react"; import { useState } from "react"; import { AutomationList } from "."; -import { createMockAuto } from "utils/mocks"; +import { bigMockAutoList } from "utils/mocks"; import * as dgconst from 'components/DAGFlow/constants'; import { Page } from "components/Page"; @@ -52,457 +52,7 @@ EmptyStart.args = { export const FewAutos = Template.bind({}) FewAutos.args = { ...FewAutos.args, - automations: [ - createMockAuto({ "Room": "Bathroom", "Type": "Lights", "NewDevice": "Yes", "Remote": "No", "Floor": "1" }), - createMockAuto({ "Room": "Bathroom", "Type": "Climate" }), - createMockAuto({ "Room": "Living Room", "Type": "Climate" }), - createMockAuto({ "Type": "Climate" }), - createMockAuto({ "Type": "Lights", "Routine": "Myself" }), - createMockAuto({ "Routine": "DNS", "Type": "SSL" }), - createMockAuto({ "Room": "Bedroom", "Type": "Lights", "Routine": "Baby" }), - createMockAuto({ "Room": "Office", "Type": "Lights" }), - createMockAuto({ "Room": "Office" }), - createMockAuto(), - { - "metadata": { - "id": "1649947692702", - "alias": "Bed Remote", - "description": "", - "trigger_variables": null, - "mode": "single" - }, - "trigger": [ - { - "device_id": "asdasdasdas", - "domain": "zha", - "platform": "device", - "type": "remote_button_short_press", - "subtype": "turn_on", - "id": "turn_on" - }, - { - "device_id": "asdasdasdas", - "domain": "zha", - "platform": "device", - "type": "remote_button_short_press", - "subtype": "turn_off", - "id": "turn_off" - }, - { - "device_id": "asdasdasdas", - "domain": "zha", - "platform": "device", - "type": "remote_button_short_press", - "subtype": "dim_up", - "id": "dim_up" - }, - { - "device_id": "asdasdasdas", - "domain": "zha", - "platform": "device", - "type": "remote_button_short_press", - "subtype": "dim_down", - "id": "dim_down" - }, - { - "device_id": "asdasdasdas", - "domain": "zha", - "platform": "device", - "type": "remote_button_double_press", - "subtype": "turn_on", - "id": "up_dbclk" - } - ], - "condition": [], - "sequence": [ - { - "choose": [ - { - "conditions": [ - { - "condition": "trigger", - "id": "turn_off" - } - ], - "sequence": [ - { - "type": "turn_off", - "device_id": "asdasdasdasdas", - "entity_id": "light.bulb_ari_lamp", - "domain": "light" - } - ] - }, - { - "conditions": [ - { - "condition": "trigger", - "id": "turn_on" - } - ], - "sequence": [ - { - "service": "light.turn_on", - "data": { - "color_temp": 153, - "brightness_pct": 100 - }, - "target": { - "device_id": "asdasdasdasdas" - } - } - ] - }, - { - "conditions": [ - { - "condition": "trigger", - "id": "dim_up" - } - ], - "sequence": [ - { - "device_id": "asdasdasdasdas", - "domain": "light", - "entity_id": "light.bulb_ari_lamp", - "type": "brightness_increase" - } - ] - }, - { - "conditions": [ - { - "condition": "trigger", - "id": "dim_down" - } - ], - "sequence": [ - { - "device_id": "asdasdasdasdas", - "domain": "light", - "entity_id": "light.bulb_ari_lamp", - "type": "brightness_decrease" - } - ] - }, - { - "conditions": [ - { - "condition": "trigger", - "id": "up_dbclk" - } - ], - "sequence": [ - { - "service": "light.turn_on", - "data": { - "color_temp": 500, - "brightness_pct": 100 - }, - "target": { - "device_id": "asdasdasdasdas" - } - } - ] - } - ], - "default": [] - } - ], - "tags": { - "Type": "Remote", - "Room": "Bedroom" - } - }, - { - "metadata": { - "id": "1641617168123", - "alias": "Ensure On", - "description": "", - "trigger_variables": null, - "mode": "single" - }, - "trigger": [ - { - "platform": "device", - "type": "turned_off", - "device_id": "tasdasdasdasdasdasda", - "entity_id": "switch.power_washer", - "domain": "switch" - }, - { - "platform": "device", - "type": "turned_off", - "device_id": "dasdasdasdasdas", - "entity_id": "switch.plug_living_room_tv", - "domain": "switch" - } - ], - "condition": [], - "sequence": [ - { - "type": "turn_on", - "device_id": "tasdasdasdasdasdasda", - "entity_id": "switch.power_washer", - "domain": "switch" - }, - { - "type": "turn_on", - "device_id": "dasdasdasdasdas", - "entity_id": "switch.plug_living_room_tv", - "domain": "switch" - } - ], - "tags": {} - }, - { - "metadata": { - "id": "1649726828269", - "alias": "Climate Heat Mode Settings", - "description": "Ensures that the climate is always in sync with the house settings", - "trigger_variables": null, - "mode": "single" - }, - "trigger": [ - { - "platform": "state", - "entity_id": "climate.main_floor", - "id": "main" - }, - { - "platform": "state", - "entity_id": "climate.second_floor", - "id": "second" - }, - { - "platform": "state", - "entity_id": "input_boolean.thermostats_house_is_heating", - "id": "setting" - } - ], - "condition": [], - "sequence": [ - { - "choose": [ - { - "conditions": [ - { - "condition": "trigger", - "id": "setting" - } - ], - "sequence": [ - { - "service": "climate.turn_off", - "data": {}, - "target": { - "device_id": [ - "d88dcfe82bc9a1c0f2b77b871848155e", - "ef2d47fdeef19f8984b85f92f2597f73" - ] - } - } - ] - }, - { - "conditions": [ - { - "condition": "state", - "entity_id": "input_boolean.thermostats_house_is_heating", - "state": "on" - } - ], - "sequence": [ - { - "choose": [ - { - "conditions": [ - { - "condition": "and", - "conditions": [ - { - "condition": "template", - "value_template": "{{ trigger.to_state.state != 'heat' }}" - }, - { - "condition": "template", - "value_template": "{{ trigger.to_state.state != 'off' }}" - } - ] - } - ], - "sequence": [ - { - "service": "climate.set_hvac_mode", - "data": { - "hvac_mode": "heat" - }, - "target": { - "entity_id": "{{ trigger.entity_id }}" - } - } - ] - } - ] - } - ] - } - ], - "default": [ - { - "choose": [ - { - "conditions": [ - { - "condition": "and", - "conditions": [ - { - "condition": "template", - "value_template": "{{ trigger.to_state.state != 'cool' }}" - }, - { - "condition": "template", - "value_template": "{{ trigger.to_state.state != 'off' }}" - } - ] - } - ], - "sequence": [ - { - "service": "climate.set_hvac_mode", - "data": { - "hvac_mode": "cool" - }, - "target": { - "entity_id": "{{ trigger.entity_id }}" - } - } - ] - } - ] - } - ] - } - ], - "tags": {} - }, - { - "metadata": { - "id": "1628024070687", - "alias": "Baby Light Setting", - "description": "Configures max/min light settings for the baby ", - "trigger_variables": null, - "mode": "single" - }, - "trigger": [ - { - "platform": "time", - "at": "21:00:00", - "id": "night" - }, - { - "platform": "time", - "at": "07:30:00", - "id": "day" - } - ], - "condition": [], - "sequence": [ - { - "choose": [ - { - "conditions": [ - { - "condition": "trigger", - "id": "night" - } - ], - "sequence": [ - { - "service": "zwave_js.set_config_parameter", - "data": { - "parameter": "5", - "value": "15" - } - } - ] - }, - { - "conditions": [ - { - "condition": "trigger", - "id": "day" - } - ], - "sequence": [ - { - "service": "zwave_js.set_config_parameter", - "data": { - "parameter": "5", - "value": 100 - } - } - ] - } - ], - "default": [] - } - ], - "tags": {} - }, - { - "metadata": { - "id": "alarmanlage_an", - "alias": "alarmanlage_an (Uhrzeit)", - "mode": "single" - }, - tags: {}, - "trigger": [ - { - "entity_id": "sensor.time", - "platform": "state", - "to": "23:30" - } - ], - "condition": [ - { - "condition": "or", - "conditions": [ - { - "condition": "state", - "entity_id": "person.simon", - "state": "home" - }, - { - "condition": "state", - "entity_id": "person.leni", - "state": "home" - } - ] - } - ], - "sequence": [ - { - "service": "alarm_control_panel.alarm_arm_night", - "target": { - "entity_id": [ - "alarm_control_panel.alarmo" - ] - }, - "data": { - "code": "xxx" - } - }, - { - "service": "alarm_control_panel.alarm_arm_away", - "data": {}, - "target": { - "entity_id": "alarm_control_panel.blink_home" - } - } - ], - } - ], + automations: bigMockAutoList, } diff --git a/webapp/src/components/AutomationList/AutomationListBox.css b/webapp/src/components/AutomationList/AutomationListBox.css index 5fb98ce..fda4424 100644 --- a/webapp/src/components/AutomationList/AutomationListBox.css +++ b/webapp/src/components/AutomationList/AutomationListBox.css @@ -49,7 +49,8 @@ padding: 0.1em; } -.automation-list-box--body--group>b:hover { +.automation-list-box--body--group>b:hover, +.automation-list-box--body--group.selected>b { background-color: var(--secondary-accent); color: var(--secondary); } diff --git a/webapp/src/components/AutomationList/AutomationListBox.test.ts b/webapp/src/components/AutomationList/AutomationListBox.test.ts index c964bd3..0beaac1 100644 --- a/webapp/src/components/AutomationList/AutomationListBox.test.ts +++ b/webapp/src/components/AutomationList/AutomationListBox.test.ts @@ -1,79 +1,18 @@ -import { groupAutomations, getTagList } from './AutomationListBox'; -import { AutomationData } from 'types/automations/index'; +import { getTagList } from './AutomationListBox'; import { createMockAuto } from 'utils/mocks'; -const makeDummy = (id: string, tags: Record) => ({ - condition: [], - metadata: { - id, - alias: '', - description: '', - mode: 'single', - }, - tags, - sequence: [], - trigger: [], -}) - -test('group automations', () => { - const automations: AutomationData[] = [ - makeDummy('1', { - "Room": 'Living Room', - "Type": 'Lights' - }), - makeDummy('2', { - "Room": 'Living Room', - "Type": 'Climate' - }), - makeDummy('3', { - "Room": 'Kitchen', - "Type": 'Climate' - }), - makeDummy('4', { - "Type": 'Climate' - }), - makeDummy('5', { - "Room": 'Living Room' - }), - makeDummy('6', {}), - ]; - expect(groupAutomations(automations, [], ['Room', "Type"])).toStrictEqual([ - 0, 1, 2, 3, 4, 5, - ]) - expect(groupAutomations(automations, [1], ['Room', "Type"])).toStrictEqual({ - "Lights": [0], - "Climate": [1, 2, 3], - "": [4, 5] - }) - expect(groupAutomations(automations, [0], ['Room', "Type"])).toStrictEqual({ - "Living Room": [0, 1, 4], - "Kitchen": [2], - "": [3, 5] - }) - expect(groupAutomations(automations, [0, 1], ['Room', "Type"])).toStrictEqual({ - "Living Room": { - "Lights": [0], - "Climate": [1], - "": [4], - }, - "Kitchen": { - "Climate": [2], - }, - "": [3, 5] - }) - expect(groupAutomations(automations, [1, 0], ['Room', "Type"])).toStrictEqual({ - "Lights": { - "Living Room": [0], - }, - "Climate": { - "Living Room": [1], - "Kitchen": [2], - "": [3], - }, - "": [4, 5] - }) -}); - +// const makeDummy = (id: string, tags: Record) => ({ +// condition: [], +// metadata: { +// id, +// alias: '', +// description: '', +// mode: 'single', +// }, +// tags, +// sequence: [], +// trigger: [], +// }) test('getTagList', () => { expect(getTagList([ diff --git a/webapp/src/components/AutomationList/AutomationListBox.tsx b/webapp/src/components/AutomationList/AutomationListBox.tsx index fa5e104..d066c3e 100644 --- a/webapp/src/components/AutomationList/AutomationListBox.tsx +++ b/webapp/src/components/AutomationList/AutomationListBox.tsx @@ -4,8 +4,8 @@ import { AutomationData, AutomationMetadata } from "types/automations"; import InputMultiSelect from "components/Inputs/InputMultiSelect"; import InputText from "components/Inputs/InputText"; import { Button } from "components/Inputs/Button"; -import { TrashIcon } from "components/Icons"; -import { MetadataBox } from "./AutomationMetadataBox"; +import { makeGrouping } from "./automationGrouper"; +import { AutomationListBoxGroup } from "./AutomationListBoxGroup"; export type Props = { @@ -28,7 +28,11 @@ export const AutomationListBox: FC = ({ const [searchText, setSearchText] = useState(''); const [selectedTagIdx, setSelectedTagIdx] = useState([]); const filteredAutomations = filterAutomations(automations, searchText, selectedTagIdx, tags); - const groups = groupAutomations(filteredAutomations.map(([a, _]) => a), selectedTagIdx, tags); + const grouping = makeGrouping( + filteredAutomations.map(([a, _]) => a), + selectedTagIdx.map(i => tags[i]), + selected, + ) return
@@ -46,7 +50,15 @@ export const AutomationListBox: FC = ({ />
- {convertGroupsToItems(groups, filteredAutomations, onSelectAutomation, onRemove, selected)} +
@@ -81,119 +93,3 @@ const filterAutomations = (autos: AutomationData[], searchText: string, selected } return true }) - - -export const groupAutomations = ( - autos: AutomationData[], - selected: number[], - tags: string[], -) => { - if (selected.length === 0) { - return autos.map((_, i) => i); - } - const out: any = {}; - for (let ai = 0; ai < autos.length; ai++) { - const a = autos[ai]; - let where = out; - let tag: string = ''; - let lastKey: string | null = null; - for (const i of selected) { - tag = tags[i]; - if (lastKey) { - where = where[lastKey] - } - if (a.tags[tag]) { - if (!where[a.tags[tag]]) { - where[a.tags[tag]] = {} - } - lastKey = a.tags[tag] - } else { - if (!where[""]) { - where[""] = [] - } - lastKey = "" - break - } - } - if (lastKey !== null) { - if (!where[lastKey] || !Array.isArray(where[lastKey])) { - where[lastKey] = [] - } - where[lastKey].push(ai) - } - } - return out; -} - -const convertGroupsToItems = ( - groups: any, - autos: Array<[AutomationData, number]>, - onSelectAutomation: (i: number) => void, - onRemove: (i: number) => void, - selected: number, -) => { - if (Array.isArray(groups)) { - return groups.map(i => { - const [auto, autoIndex] = autos[i]; - let title = "BadAuto<>" - if (auto.metadata) { - title = `Name = ${auto.metadata.alias}\nID = ${auto.metadata.id}\nDescription = ${auto.metadata.description}` - } - return
onSelectAutomation(autoIndex)} - > - - -
- }) - } else { - return Object.keys(groups).sort((a, b) => { - if (!a) { - return 1 - } - return a < b ? 1 : -1 - }).map(groupName => { - return - }) - } -} - - - -export const AutomationListBoxGroup: FC<{ - groupName: string, - groups: any, - autos: Array<[AutomationData, number]>, - onSelectAutomation: (i: number) => void, - onRemove: (i: number) => void; - selected: number, -}> = ({ - groupName, - groups, - autos, - onSelectAutomation, - onRemove, - selected, -}) => { - const [open, setOpen] = useState(false); - const isOther = !groupName; - const openIcon = !open ? "⊕" : "⊖"; - const total = isOther ? - {groups.length} item{groups.length !== 1 ? 's' : ''} (untagged) - : <> - return
- setOpen(!open)}>{groupName} {openIcon} {total} - {open && convertGroupsToItems(groups, autos, onSelectAutomation, onRemove, selected)} -
- } \ No newline at end of file diff --git a/webapp/src/components/AutomationList/AutomationListBoxGroup.tsx b/webapp/src/components/AutomationList/AutomationListBoxGroup.tsx index 1e7f234..f2a48b4 100644 --- a/webapp/src/components/AutomationList/AutomationListBoxGroup.tsx +++ b/webapp/src/components/AutomationList/AutomationListBoxGroup.tsx @@ -3,50 +3,139 @@ import { TrashIcon } from "components/Icons"; import { Button } from "components/Inputs/Button"; import { MetadataBox } from "./AutomationMetadataBox"; import { AutomationData } from "types/automations"; -import { AutomationGrouper } from "./automationGrouper"; - +import { AutomationGrouping } from "./automationGrouper"; +import { FC, useState } from "react"; +export type AutomationListBoxEvents = { + onSelectAutomation: (i: number) => void, + onRemove: (i: number) => void, +} +export type AutomationListBoxGroups = { + grouping: AutomationGrouping, + currentNode: number, +} +export type AutomationListBoxProps = { + events: AutomationListBoxEvents, + groups: AutomationListBoxGroups, + autos: Array<[AutomationData, number]>, + selectedAutomationIdx: number, +}; +export type AutomationListBoxGroupProps = { + events: AutomationListBoxEvents, + grouping: AutomationGrouping, + autos: Array<[AutomationData, number]>, + selectedAutomationIdx: number, +} +export const AutomationListBoxGroup: FC = ({ + events, grouping, autos, selectedAutomationIdx, +}) => { + if (grouping.top.length === 1) { + return <> + {convertGroupsToItems(autos, { + grouping, currentNode: grouping.top[0], + }, events, selectedAutomationIdx)} + + } else { + return <> + {grouping.top.map(groupIdx => )} + + } +} export const convertGroupsToItems = ( autos: Array<[AutomationData, number]>, - groups: AutomationGrouper, - currentNode: number, - onSelectAutomation: (i: number) => void, - onRemove: (i: number) => void, + { grouping, currentNode }: AutomationListBoxGroups, + events: AutomationListBoxEvents, + selectedAutomationIdx: number, ) => { - if (Array.isArray(groups)) { - return groups.map(i => { - const [auto, autoIndex] = autos[i]; - let title = "BadAuto<>" - if (auto.metadata) { - title = `Name = ${auto.metadata.alias}\nID = ${auto.metadata.id}\nDescription = ${auto.metadata.description}` - } - return
onSelectAutomation(autoIndex)} - > - - -
- }) - } else { - return Object.keys(groups).sort((a, b) => { - if (!a) { - return 1 - } - return a < b ? 1 : -1 - }).map(groupName => { - return 0) { + return automations.map(autoIndex => + - }) + ) } + const subgroups = grouping.getSubGroups(currentNode); + if (subgroups.length > 0) { + return subgroups.map(groupIdx => ) + } + return <> } + +export const AutomationListBoxGroupItem: FC<{ + events: AutomationListBoxEvents, + groups: AutomationListBoxGroups, + autos: Array<[AutomationData, number]>, + selectedAutomationIdx: number, +}> = ({ + autos, groups, events, selectedAutomationIdx, +}) => { + const groupData = groups.grouping.getData(groups.currentNode); + const automations = groups.grouping.getAutomations(groups.currentNode); + const [open, setOpen] = useState(false); + const isOther = groupData.isOther; + const openIcon = !open ? "⊕" : "⊖"; + const total = isOther ? + {automations.length} automation{automations.length !== 1 ? 's' : ''} {groupData.isOther ? "" : "(untagged)"} + : <> + return
+ setOpen(!open)}>{groupData.name} {openIcon} {total} + {open && convertGroupsToItems(autos, groups, events, selectedAutomationIdx)} +
+ } + + +export const AutomationListBoxItem: FC<{ + auto: [AutomationData, number], + isSelected: boolean, + events: AutomationListBoxEvents, +}> = ({ + events: { + onRemove, + onSelectAutomation, + }, + auto: [ + auto, autoIndex + ], + isSelected, +}) => { + let title = "BadAuto<>" + if (auto.metadata) { + title = `Name = ${auto.metadata.alias}\nID = ${auto.metadata.id}\nDescription = ${auto.metadata.description}` + } + return
onSelectAutomation(autoIndex)} + > + + +
+ } \ No newline at end of file diff --git a/webapp/src/components/AutomationList/automationGrouper.test.ts b/webapp/src/components/AutomationList/automationGrouper.test.ts index 6b75b0b..ed76066 100644 --- a/webapp/src/components/AutomationList/automationGrouper.test.ts +++ b/webapp/src/components/AutomationList/automationGrouper.test.ts @@ -1,6 +1,6 @@ import { makeGrouping } from "./automationGrouper"; import { AutomationData } from 'types/automations/index'; -import { createMockAuto } from 'utils/mocks'; +import { bigMockAutoList, createMockAuto } from 'utils/mocks'; const automations: AutomationData[] = [ createMockAuto({ @@ -45,4 +45,28 @@ test('2 groupings', () => { expect(grouped.getSubGroups(grouped.top[1])).toHaveLength(1) expect(grouped.getAutomations(grouped.top[2])).toHaveLength(2) expect(grouped.getSubGroups(grouped.top[2])).toHaveLength(0) -}); \ No newline at end of file +}); +test('bigAutoList groupings has not duplicate ids', () => { + const grouped = makeGrouping(bigMockAutoList, ['Room', "Remote"], 0); + const flattenAndCount = (gid: number): Record => { + const subgroups = grouped.getSubGroups(gid); + if (subgroups.length > 0) { + return flattenAndConsolidate(subgroups) + } else { + return grouped.getAutomations(gid).reduce((sum: Record, b) => { + sum[b] = (sum[b] ?? []).concat(gid) + return sum + }, {}) + } + } + const flattenAndConsolidate = (gids: number[]): Record => gids.map(flattenAndCount).reduce((a, b) => { + for (const k of Object.keys(b).map(Number)) { + a[k] = (a[k] ?? []).concat(b[k]) + } + return a + }, {}) + const countedAutos = flattenAndConsolidate(grouped.top) + for (const key of Object.keys(countedAutos).map(Number)) { + expect(countedAutos[key]).toHaveLength(1) + } +}) \ No newline at end of file diff --git a/webapp/src/components/AutomationList/automationGrouper.tsx b/webapp/src/components/AutomationList/automationGrouper.tsx index 21b504d..8bb5b9d 100644 --- a/webapp/src/components/AutomationList/automationGrouper.tsx +++ b/webapp/src/components/AutomationList/automationGrouper.tsx @@ -2,11 +2,13 @@ import { AutomationData } from "types/automations"; const makeGrouper = () => { const groupIdNames: string[] = [] - const groupSubGroups: number[][] = [] + const groupSubGroups: Array> = [] const groupAutomations: number[][] = [] const groupData: Array<{ name: string; selected: boolean; + isOther: boolean; + isTop: boolean; }> = []; const topGroups: number[] = []; @@ -16,13 +18,15 @@ const makeGrouper = () => { groupAutomations, groupData, topGroups, - addNew(groupId: string, groupName: string, theChosenOne: boolean, isTopLevel: boolean): number { + addNew(groupId: string, groupName: string, theChosenOne: boolean, isTopLevel: boolean, isOther: boolean = false): number { groupIdNames.push(groupId); - groupSubGroups.push([]); + groupSubGroups.push(new Set()); groupAutomations.push([]); groupData.push({ name: groupName, selected: theChosenOne, + isTop: isTopLevel, + isOther }) const groupIdx = groupIdNames.length - 1; if (isTopLevel) { @@ -42,7 +46,7 @@ const makeGrouper = () => { groupData[groupIdx].selected = true; } if (update.lastGroupIdx !== null) { - groupSubGroups[update.lastGroupIdx].push(groupIdx); + groupSubGroups[update.lastGroupIdx].add(groupIdx); } if (update.autoIdx !== null) { groupAutomations[groupIdx].push(update.autoIdx) @@ -51,7 +55,7 @@ const makeGrouper = () => { } } -export type AutomationGrouper = ReturnType; +export type AutomationGrouping = ReturnType; export const makeGrouping = ( autos: AutomationData[], selectedTags: string[], @@ -60,18 +64,19 @@ export const makeGrouping = ( const grouper = makeGrouper(); if (selectedTags.length === 0) { - grouper.addNew("$", "", selectedAutomationIdx >= 0, true) + grouper.addNew("$", "", selectedAutomationIdx >= 0, true, true) grouper.groupAutomations = [autos.map((_, i) => i)]; } else { autos.forEach((auto, autoIdx) => { const theChosenOne = selectedAutomationIdx === autoIdx; let lastGroupIdx: number | null = null; + let lastGroupId: string = "$"; for (let ti = 0; ti < selectedTags.length; ti++) { const isTopLevel = ti === 0; const selTag = selectedTags[ti]; if (selTag in auto.tags) { const groupName = auto.tags[selTag]; - const groupId = `${selTag}:${groupName}`; + const groupId = `${lastGroupId}>${selTag}:${groupName}`; let groupIdx = grouper.groupIdNames.indexOf(groupId); if (groupIdx < 0) { groupIdx = grouper.addNew(groupId, groupName, theChosenOne, isTopLevel); @@ -82,10 +87,12 @@ export const makeGrouping = ( autoIdx: ti === selectedTags.length - 1 ? autoIdx : null, }) lastGroupIdx = groupIdx; + lastGroupId = groupId; } else { - let groupIdx = grouper.groupIdNames.indexOf(`${selTag}:$`); + const groupId = `${lastGroupId}>${selTag}:$`; + let groupIdx = grouper.groupIdNames.indexOf(groupId); if (groupIdx < 0) { - groupIdx = grouper.addNew(`${selTag}:$`, "", theChosenOne, isTopLevel); + groupIdx = grouper.addNew(groupId, "", theChosenOne, isTopLevel, true); } grouper.updateGroup(groupIdx, { isChosen: theChosenOne, @@ -97,12 +104,19 @@ export const makeGrouping = ( } }); } - - console.log(grouper) + const sortGroupIds = (groupIds: number[]) => groupIds.sort((a, b) => { + const adata = grouper.groupData[a]; + const bdata = grouper.groupData[b]; + if (adata.isOther) { + return 1 + } else { + return adata.name > bdata.name ? -1 : 1 + } + }) return { - top: grouper.topGroups, + top: sortGroupIds(grouper.topGroups), getSubGroups(groupIdx: number) { - return grouper.groupSubGroups[groupIdx] + return sortGroupIds(Array.from(grouper.groupSubGroups[groupIdx].values())) }, getAutomations(groupIdx: number) { return grouper.groupAutomations[groupIdx] diff --git a/webapp/src/components/Inputs/InputNumber.tsx b/webapp/src/components/Inputs/InputNumber.tsx index 5cc8c45..8dec170 100644 --- a/webapp/src/components/Inputs/InputNumber.tsx +++ b/webapp/src/components/Inputs/InputNumber.tsx @@ -1,4 +1,3 @@ -// import { useState } from "react"; import InputWrapper from "./InputWrapper"; import { useInputNumberStyles } from "./styles"; diff --git a/webapp/src/utils/mocks.ts b/webapp/src/utils/mocks.ts index 224be0f..5658b9c 100644 --- a/webapp/src/utils/mocks.ts +++ b/webapp/src/utils/mocks.ts @@ -51,3 +51,456 @@ export const createMockAuto = (tags: Record = {}): AutomationDat } ] }) + + +export const bigMockAutoList: AutomationData[] = [ + createMockAuto({ "Room": "Bathroom", "Type": "Lights", "NewDevice": "Yes", "Remote": "No", "Floor": "1" }), + createMockAuto({ "Room": "Bathroom", "Type": "Climate" }), + createMockAuto({ "Room": "Living Room", "Type": "Climate" }), + createMockAuto({ "Type": "Climate" }), + createMockAuto({ "Type": "Lights", "Routine": "Myself" }), + createMockAuto({ "Routine": "DNS", "Type": "SSL" }), + createMockAuto({ "Room": "Bedroom", "Type": "Lights", "Routine": "Baby" }), + createMockAuto({ "Room": "Office", "Type": "Lights" }), + createMockAuto({ "Room": "Office" }), + createMockAuto(), + { + "metadata": { + "id": "1649947692702", + "alias": "Bed Remote", + "description": "", + "trigger_variables": null, + "mode": "single" + }, + "trigger": [ + { + "device_id": "asdasdasdas", + "domain": "zha", + "platform": "device", + "type": "remote_button_short_press", + "subtype": "turn_on", + "id": "turn_on" + }, + { + "device_id": "asdasdasdas", + "domain": "zha", + "platform": "device", + "type": "remote_button_short_press", + "subtype": "turn_off", + "id": "turn_off" + }, + { + "device_id": "asdasdasdas", + "domain": "zha", + "platform": "device", + "type": "remote_button_short_press", + "subtype": "dim_up", + "id": "dim_up" + }, + { + "device_id": "asdasdasdas", + "domain": "zha", + "platform": "device", + "type": "remote_button_short_press", + "subtype": "dim_down", + "id": "dim_down" + }, + { + "device_id": "asdasdasdas", + "domain": "zha", + "platform": "device", + "type": "remote_button_double_press", + "subtype": "turn_on", + "id": "up_dbclk" + } + ], + "condition": [], + "sequence": [ + { + "choose": [ + { + "conditions": [ + { + "condition": "trigger", + "id": "turn_off" + } + ], + "sequence": [ + { + "type": "turn_off", + "device_id": "asdasdasdasdas", + "entity_id": "light.bulb_ari_lamp", + "domain": "light" + } + ] + }, + { + "conditions": [ + { + "condition": "trigger", + "id": "turn_on" + } + ], + "sequence": [ + { + "service": "light.turn_on", + "data": { + "color_temp": 153, + "brightness_pct": 100 + }, + "target": { + "device_id": "asdasdasdasdas" + } + } + ] + }, + { + "conditions": [ + { + "condition": "trigger", + "id": "dim_up" + } + ], + "sequence": [ + { + "device_id": "asdasdasdasdas", + "domain": "light", + "entity_id": "light.bulb_ari_lamp", + "type": "brightness_increase" + } + ] + }, + { + "conditions": [ + { + "condition": "trigger", + "id": "dim_down" + } + ], + "sequence": [ + { + "device_id": "asdasdasdasdas", + "domain": "light", + "entity_id": "light.bulb_ari_lamp", + "type": "brightness_decrease" + } + ] + }, + { + "conditions": [ + { + "condition": "trigger", + "id": "up_dbclk" + } + ], + "sequence": [ + { + "service": "light.turn_on", + "data": { + "color_temp": 500, + "brightness_pct": 100 + }, + "target": { + "device_id": "asdasdasdasdas" + } + } + ] + } + ], + "default": [] + } + ], + "tags": { + "Type": "Remote", + "Room": "Bedroom" + } + }, + { + "metadata": { + "id": "1641617168123", + "alias": "Ensure On", + "description": "", + "trigger_variables": null, + "mode": "single" + }, + "trigger": [ + { + "platform": "device", + "type": "turned_off", + "device_id": "tasdasdasdasdasdasda", + "entity_id": "switch.power_washer", + "domain": "switch" + }, + { + "platform": "device", + "type": "turned_off", + "device_id": "dasdasdasdasdas", + "entity_id": "switch.plug_living_room_tv", + "domain": "switch" + } + ], + "condition": [], + "sequence": [ + { + "type": "turn_on", + "device_id": "tasdasdasdasdasdasda", + "entity_id": "switch.power_washer", + "domain": "switch" + }, + { + "type": "turn_on", + "device_id": "dasdasdasdasdas", + "entity_id": "switch.plug_living_room_tv", + "domain": "switch" + } + ], + "tags": {} + }, + { + "metadata": { + "id": "1649726828269", + "alias": "Climate Heat Mode Settings", + "description": "Ensures that the climate is always in sync with the house settings", + "trigger_variables": null, + "mode": "single" + }, + "trigger": [ + { + "platform": "state", + "entity_id": "climate.main_floor", + "id": "main" + }, + { + "platform": "state", + "entity_id": "climate.second_floor", + "id": "second" + }, + { + "platform": "state", + "entity_id": "input_boolean.thermostats_house_is_heating", + "id": "setting" + } + ], + "condition": [], + "sequence": [ + { + "choose": [ + { + "conditions": [ + { + "condition": "trigger", + "id": "setting" + } + ], + "sequence": [ + { + "service": "climate.turn_off", + "data": {}, + "target": { + "device_id": [ + "d88dcfe82bc9a1c0f2b77b871848155e", + "ef2d47fdeef19f8984b85f92f2597f73" + ] + } + } + ] + }, + { + "conditions": [ + { + "condition": "state", + "entity_id": "input_boolean.thermostats_house_is_heating", + "state": "on" + } + ], + "sequence": [ + { + "choose": [ + { + "conditions": [ + { + "condition": "and", + "conditions": [ + { + "condition": "template", + "value_template": "{{ trigger.to_state.state != 'heat' }}" + }, + { + "condition": "template", + "value_template": "{{ trigger.to_state.state != 'off' }}" + } + ] + } + ], + "sequence": [ + { + "service": "climate.set_hvac_mode", + "data": { + "hvac_mode": "heat" + }, + "target": { + "entity_id": "{{ trigger.entity_id }}" + } + } + ] + } + ] + } + ] + } + ], + "default": [ + { + "choose": [ + { + "conditions": [ + { + "condition": "and", + "conditions": [ + { + "condition": "template", + "value_template": "{{ trigger.to_state.state != 'cool' }}" + }, + { + "condition": "template", + "value_template": "{{ trigger.to_state.state != 'off' }}" + } + ] + } + ], + "sequence": [ + { + "service": "climate.set_hvac_mode", + "data": { + "hvac_mode": "cool" + }, + "target": { + "entity_id": "{{ trigger.entity_id }}" + } + } + ] + } + ] + } + ] + } + ], + "tags": {} + }, + { + "metadata": { + "id": "1628024070687", + "alias": "Baby Light Setting", + "description": "Configures max/min light settings for the baby ", + "trigger_variables": null, + "mode": "single" + }, + "trigger": [ + { + "platform": "time", + "at": "21:00:00", + "id": "night" + }, + { + "platform": "time", + "at": "07:30:00", + "id": "day" + } + ], + "condition": [], + "sequence": [ + { + "choose": [ + { + "conditions": [ + { + "condition": "trigger", + "id": "night" + } + ], + "sequence": [ + { + "service": "zwave_js.set_config_parameter", + "data": { + "parameter": "5", + "value": "15" + } + } + ] + }, + { + "conditions": [ + { + "condition": "trigger", + "id": "day" + } + ], + "sequence": [ + { + "service": "zwave_js.set_config_parameter", + "data": { + "parameter": "5", + "value": 100 + } + } + ] + } + ], + "default": [] + } + ], + "tags": {} + }, + { + "metadata": { + "id": "alarmanlage_an", + "alias": "alarmanlage_an (Uhrzeit)", + "mode": "single" + }, + tags: {}, + "trigger": [ + { + "entity_id": "sensor.time", + "platform": "state", + "to": "23:30" + } + ], + "condition": [ + { + "condition": "or", + "conditions": [ + { + "condition": "state", + "entity_id": "person.simon", + "state": "home" + }, + { + "condition": "state", + "entity_id": "person.leni", + "state": "home" + } + ] + } + ], + "sequence": [ + { + "service": "alarm_control_panel.alarm_arm_night", + "target": { + "entity_id": [ + "alarm_control_panel.alarmo" + ] + }, + "data": { + "code": "xxx" + } + }, + { + "service": "alarm_control_panel.alarm_arm_away", + "data": {}, + "target": { + "entity_id": "alarm_control_panel.blink_home" + } + } + ], + } +] \ No newline at end of file