From 774ebe482a64af89ba414b87043a2e1e4c0c7873 Mon Sep 17 00:00:00 2001 From: Ari Date: Thu, 1 Sep 2022 17:21:00 -0400 Subject: [PATCH] handling missing ids better + more tranlsations --- api/src/api/routes/automations.py | 4 + api/src/automations/db.py | 8 +- api/src/automations/loader.py | 18 +- api/src/automations/types.py | 2 +- .../samples/config-13/automations/ui.yaml | 21 ++ .../samples/config-13/configuration.yaml | 30 ++ api/tests/samples/config-13/packages.yaml | 16 + api/tests/samples/config-13/packages/ha.yaml | 1 + .../config-13/packages/sub/automation.yaml | 13 + .../unittests/automations/test_loader.py | 2 + api/tests/utils.py | 1 + webapp/src/apiService/mock.test.ts | 2 +- .../AutomationEditor/AutoInfoBox.stories.tsx | 2 +- .../AutomationEditor/AutoInfoBox.tsx | 4 +- .../AutomationEditor/index.stories.tsx | 2 +- .../src/components/AutomationEditor/index.tsx | 57 ++- .../ListBoxGroup/index.stories.tsx | 6 + .../AutomationManager/Loaded/automationDB.ts | 70 +++- .../AutomationManager/Loaded/index.tsx | 2 + .../AutomationManager/Loaded/state.ts | 18 +- .../Sidebar/index.stories.tsx | 2 + .../automationGrouper.test.ts | 6 +- .../src/components/AutomationManager/types.ts | 1 + .../ConditionEditor/editorRender.tsx | 2 +- .../Inputs/AutoComplete/InputArea.tsx | 4 +- .../Inputs/AutoComplete/InputAutoComplete.tsx | 4 +- .../Inputs/AutoComplete/InputDevice.tsx | 4 +- .../Inputs/AutoComplete/InputService.tsx | 38 +- .../Inputs/AutoComplete/InputTimeEntity.tsx | 4 +- .../src/components/Inputs/Base/InputTime.tsx | 37 +- .../NodeEditor/OptionManager/ActionEvent.tsx | 6 +- .../NodeEditor/OptionManager/ActionRepeat.tsx | 6 +- .../NodeEditor/OptionManager/ActionScene.tsx | 4 +- .../NodeEditor/OptionManager/ActionStop.tsx | 4 +- .../NodeEditor/OptionManager/ActionWait.tsx | 12 +- .../NodeEditor/OptionManager/OptionManager.ts | 2 + .../NodeEditor/OptionManager/TriggerTime.tsx | 8 +- .../NodeEditor/OptionManager/index.tsx | 1 + webapp/src/components/NodeEditor/index.tsx | 2 +- webapp/src/lang/eng.json | 23 +- webapp/src/lang/fra.json | 23 +- webapp/src/lang/ita.json | 336 ++++++++++-------- webapp/src/types/validators/autmation.ts | 4 +- webapp/src/types/validators/metadata.ts | 2 +- webapp/src/utils/defaults.ts | 4 +- 45 files changed, 556 insertions(+), 262 deletions(-) create mode 100644 api/tests/samples/config-13/automations/ui.yaml create mode 100644 api/tests/samples/config-13/configuration.yaml create mode 100644 api/tests/samples/config-13/packages.yaml create mode 100644 api/tests/samples/config-13/packages/ha.yaml create mode 100644 api/tests/samples/config-13/packages/sub/automation.yaml diff --git a/api/src/api/routes/automations.py b/api/src/api/routes/automations.py index b277bf0..ea06087 100644 --- a/api/src/api/routes/automations.py +++ b/api/src/api/routes/automations.py @@ -29,11 +29,15 @@ def make_automation_router(hass_config: HassConfig) -> APIRouter: @router.post("/item") def insert_auto(body: Automation): + if body.id is None: + raise HTTPException(status_code=400, detail={"msg": "must contain an id"}) automations = AutomationManager(hass_config) automations.create(body) @router.put("/item") def update_auto(body: ExtenededAutomation): + if body.id is None: + raise HTTPException(status_code=400, detail={"msg": "must contain an id"}) automations = AutomationManager(hass_config) automations.update(body) diff --git a/api/src/automations/db.py b/api/src/automations/db.py index d62d6be..c6b9e52 100644 --- a/api/src/automations/db.py +++ b/api/src/automations/db.py @@ -11,14 +11,18 @@ logger = get_logger(__file__) class AutomationDBConnection: def __init__(self) -> None: self.db: dict[str, ExtenededAutomation] = {} + self.missing_id_db: list[ExtenededAutomation] = [] def reset(self): self.db = {} + self.missing_id_db = [] def insert_automations(self, automations: list[ExtenededAutomation]): repeated_automations = [] for automation in automations: - if self.db.get(automation.id, None) is None: + if automation.id is None: + self.missing_id_db.append(automation) + elif self.db.get(automation.id, None) is None: self.db[automation.id] = automation else: repeated_automations.append( @@ -33,7 +37,7 @@ class AutomationDBConnection: raise DBNoAutomationFound(automation_id) def list_automations(self, offset: int, limit: int) -> Iterable[ExtenededAutomation]: - for i, auto in enumerate(self.db.values()): + for i, auto in enumerate([*self.missing_id_db, *self.db.values()]): if i >= offset and i < limit + offset: yield auto diff --git a/api/src/automations/loader.py b/api/src/automations/loader.py index 8b8b4ca..366652d 100644 --- a/api/src/automations/loader.py +++ b/api/src/automations/loader.py @@ -236,9 +236,11 @@ def load_automation_inline_ref( ) -> Iterator[ExtenededAutomation]: for auto in inline_automation.automations: try: + cleaned_automation = clean_automation(auto) + automation_id = cleaned_automation.get("id", None) yield ExtenededAutomation( - **clean_automation(auto), - tags=tag_manager.get(auto["id"], {}), + **cleaned_automation, + tags={} if automation_id is None else tag_manager.get(automation_id, {}), configuration_key=inline_automation.configuration_key, source_file=str(inline_automation.source_file.resolve().relative_to(root_path)), source_file_type="inline", @@ -286,9 +288,11 @@ def load_automation_path( if isinstance(automations, dict): try: + cleaned_automation = clean_automation(automations) + automation_id = cleaned_automation.get("id", None) yield ExtenededAutomation( - **clean_automation(automations), - tags=tag_manager.get(automations["id"], {}), + **cleaned_automation, + tags={} if automation_id is None else tag_manager.get(automation_id, {}), configuration_key=configuration_key, source_file=str(automation_path.resolve().relative_to(root_path)), source_file_type="obj", @@ -305,9 +309,11 @@ def load_automation_path( return for automation in automations: try: + cleaned_automation = clean_automation(automation) + automation_id = cleaned_automation.get("id", None) yield ExtenededAutomation( - **clean_automation(automation), - tags=tag_manager.get(automation["id"], {}), + **cleaned_automation, + tags={} if automation_id is None else tag_manager.get(automation_id, {}), configuration_key=configuration_key, source_file=str(automation_path.resolve().relative_to(root_path)), source_file_type="list", diff --git a/api/src/automations/types.py b/api/src/automations/types.py index 3d4606f..b67d6a9 100644 --- a/api/src/automations/types.py +++ b/api/src/automations/types.py @@ -32,7 +32,7 @@ AutomationExtractedIter = Iterator[ExtractedAutomation] class BaseAutomation(BaseModel): - id: str + id: Optional[str] alias: Optional[str] = None description: Optional[str] = None trigger_variables: Optional[dict[str, Union[str, int, bool, float]]] = None diff --git a/api/tests/samples/config-13/automations/ui.yaml b/api/tests/samples/config-13/automations/ui.yaml new file mode 100644 index 0000000..e125729 --- /dev/null +++ b/api/tests/samples/config-13/automations/ui.yaml @@ -0,0 +1,21 @@ +- alias: UI 1 + description: Example + mode: single + trigger: + - platform: homeassistant + event: start + condition: [] + action: + - service: counter.increment + data: {} + target: + entity_id: counter.up_times +- alias: UI 2 + description: "" + trigger: + - platform: sun + event: sunrise + offset: 0 + condition: [] + action: [] + mode: single diff --git a/api/tests/samples/config-13/configuration.yaml b/api/tests/samples/config-13/configuration.yaml new file mode 100644 index 0000000..c1b374f --- /dev/null +++ b/api/tests/samples/config-13/configuration.yaml @@ -0,0 +1,30 @@ +# Loads default set of integrations. Do not remove. +default_config: + +# Text to speech +tts: + - platform: google_translate + +# automation manual: !include_dir_list ./automations/include_dir_list +# automation ui: !include automations/ui.yaml +# automation: !include automations/base.yaml +# automation cools: !include_dir_merge_list automations/include_dir_merge_list +script: !include scripts.yaml +scene: !include scenes.yaml + +automation: + - alias: inline-auto + description: Example package auto + mode: single + trigger: + - platform: homeassistant + event: start + condition: [] + action: + - service: counter.increment + data: {} + target: + entity_id: counter.up_times + +homeassistant: + packages: !include packages.yaml diff --git a/api/tests/samples/config-13/packages.yaml b/api/tests/samples/config-13/packages.yaml new file mode 100644 index 0000000..e0fa9d5 --- /dev/null +++ b/api/tests/samples/config-13/packages.yaml @@ -0,0 +1,16 @@ +ha: !include packages/ha.yaml +sub: !include_dir_merge_named packages/sub +secret_ones: + automation: + - alias: package ha + description: Example package auto + mode: single + trigger: + - platform: homeassistant + event: start + condition: [] + action: + - service: counter.increment + data: {} + target: + entity_id: counter.up_times diff --git a/api/tests/samples/config-13/packages/ha.yaml b/api/tests/samples/config-13/packages/ha.yaml new file mode 100644 index 0000000..6bae0a9 --- /dev/null +++ b/api/tests/samples/config-13/packages/ha.yaml @@ -0,0 +1 @@ +automation: !include ../automations/ui.yaml diff --git a/api/tests/samples/config-13/packages/sub/automation.yaml b/api/tests/samples/config-13/packages/sub/automation.yaml new file mode 100644 index 0000000..3ed923b --- /dev/null +++ b/api/tests/samples/config-13/packages/sub/automation.yaml @@ -0,0 +1,13 @@ +automation: + - alias: package ha + description: Example package auto + mode: single + trigger: + - platform: homeassistant + event: start + condition: [] + action: + - service: counter.increment + data: {} + target: + entity_id: counter.up_times diff --git a/api/tests/unittests/automations/test_loader.py b/api/tests/unittests/automations/test_loader.py index 6197733..7f8fbcf 100644 --- a/api/tests/unittests/automations/test_loader.py +++ b/api/tests/unittests/automations/test_loader.py @@ -10,6 +10,7 @@ from src.automations.tags import TagManager from src.automations.types import ExtenededAutomation from src.hass_config.loader import HassConfig from tests.utils import ( + HA_CONFIG13_EXAMPLE, HA_CONFIG2_EXAMPLE, HA_CONFIG3_EXAMPLE, HA_CONFIG4_EXAMPLE, @@ -160,6 +161,7 @@ class loader_tests(TestCase): (HA_CONFIG10_EXAMPLE, 3), (HA_CONFIG11_EXAMPLE, 3), (HA_CONFIG12_EXAMPLE, 4), + (HA_CONFIG13_EXAMPLE, 4), ]: with self.subTest(config_name=ha_path.name, expected=expected): hass_config = HassConfig(ha_path) diff --git a/api/tests/utils.py b/api/tests/utils.py index 7abc870..ec58b71 100644 --- a/api/tests/utils.py +++ b/api/tests/utils.py @@ -22,6 +22,7 @@ HA_CONFIG9_EXAMPLE = SAMPLES_FOLDER / "config-9" HA_CONFIG10_EXAMPLE = SAMPLES_FOLDER / "config-10" HA_CONFIG11_EXAMPLE = SAMPLES_FOLDER / "config-11" HA_CONFIG12_EXAMPLE = SAMPLES_FOLDER / "config-12" +HA_CONFIG13_EXAMPLE = SAMPLES_FOLDER / "config-13" def get_example_automation_loader( diff --git a/webapp/src/apiService/mock.test.ts b/webapp/src/apiService/mock.test.ts index e0fffcd..daa8456 100644 --- a/webapp/src/apiService/mock.test.ts +++ b/webapp/src/apiService/mock.test.ts @@ -76,7 +76,7 @@ test("update tags from mock api", async () => { ); let data = ((await mockApi.list({ limit: 10, offset: 0 })) as any).data .data as AutomationData[]; - const autoId = data[0].id; + const autoId = data[0].id as string; expect(data[0].tags).toStrictEqual({}); mockApi.updateTags({ automation_id: autoId, diff --git a/webapp/src/components/AutomationEditor/AutoInfoBox.stories.tsx b/webapp/src/components/AutomationEditor/AutoInfoBox.stories.tsx index 6944315..8768537 100644 --- a/webapp/src/components/AutomationEditor/AutoInfoBox.stories.tsx +++ b/webapp/src/components/AutomationEditor/AutoInfoBox.stories.tsx @@ -14,7 +14,7 @@ const { componentMeta, make } = makeStory({ }) => { const [[metadata, tags], setState] = useState([args.metadata, args.tags]); const tagDB = useTagDB( - [{ id: metadata.id, tags: Object.fromEntries(tags) }], + metadata.id ? [{ id: metadata.id, tags: Object.fromEntries(tags) }] : [], (_, t) => setState([metadata, Object.entries(t)]) ); diff --git a/webapp/src/components/AutomationEditor/AutoInfoBox.tsx b/webapp/src/components/AutomationEditor/AutoInfoBox.tsx index 27443e7..9a1dbbc 100644 --- a/webapp/src/components/AutomationEditor/AutoInfoBox.tsx +++ b/webapp/src/components/AutomationEditor/AutoInfoBox.tsx @@ -104,7 +104,7 @@ export const AutoInfoBox: FC = ({
= ({ options={["parallel", "single", "queued", "restart"]} />
-

Tags

+

{langStore.get("TAGS")}

{tags.map(([tagName, tagValue], tagIndex) => (
diff --git a/webapp/src/components/AutomationEditor/index.stories.tsx b/webapp/src/components/AutomationEditor/index.stories.tsx index 8ba6c6c..a3a0aff 100644 --- a/webapp/src/components/AutomationEditor/index.stories.tsx +++ b/webapp/src/components/AutomationEditor/index.stories.tsx @@ -19,7 +19,7 @@ const { make, componentMeta } = makeStory({ {} )} automation={state} diff --git a/webapp/src/components/AutomationEditor/index.tsx b/webapp/src/components/AutomationEditor/index.tsx index f08f2c4..ad490b4 100644 --- a/webapp/src/components/AutomationEditor/index.tsx +++ b/webapp/src/components/AutomationEditor/index.tsx @@ -32,6 +32,8 @@ import { MiniFailure } from "types/validators/helper"; import { AutoInfoBox } from "./AutoInfoBox"; import { useAutomatioEditorState, EditorData } from "./state"; import { useLang } from "lang"; +import { useSnackbar } from "notistack"; +import { Alert } from "@mui/material"; interface Props { automation?: AutomationData | BareAutomationData; @@ -41,6 +43,8 @@ interface Props { onDelete: () => void; tagDB: TagDB; isNew?: boolean; + readonly: boolean; + issue?: string; } export const AutomationEditor: FC = ({ dims, @@ -49,10 +53,12 @@ export const AutomationEditor: FC = ({ onDelete, onTrigger, tagDB, - isNew, + readonly, + issue, }) => { // state const langStore = useLang(); + const snackbr = useSnackbar(); const { state, updateSequence, @@ -63,7 +69,15 @@ export const AutomationEditor: FC = ({ save, saveAndUpdate, undo, - } = useAutomatioEditorState(propsAutos, propsOnUpdate); + } = useAutomatioEditorState(propsAutos, (auto) => { + if (readonly) { + snackbr.enqueueSnackbar(langStore.get("READONLY_CANNOT_UPDATE"), { + color: "warning", + }); + } else { + propsOnUpdate(auto); + } + }); const [infoBoxOpen, setInfoBox] = useState(false); const [ { ckFlipped }, @@ -95,7 +109,13 @@ export const AutomationEditor: FC = ({ ); } return ( -
+
{state.status === "saving" && ( )} @@ -112,13 +132,15 @@ export const AutomationEditor: FC = ({ state.status, ].join(" ")} > - +
@@ -220,6 +242,11 @@ export const AutomationEditor: FC = ({ {langStore.get("SAVE")}
+ {readonly && ( + + {langStore.get("READONLY_MODE")} - {issue} + + )}
    {failures.map((f, i) => ( - <> -
  • - {f.path}: -
      - {f.message.map((m, j) => ( -
    • {m}
    • - ))} -
    -
  • - +
  • + {f.path}: +
      + {f.message.map((m, j) => ( +
    • {m}
    • + ))} +
    +
  • ))}
{langStore.get("VALIDATION_PLEASE_CORRECT_FILE")} diff --git a/webapp/src/components/AutomationManager/ListBoxGroup/index.stories.tsx b/webapp/src/components/AutomationManager/ListBoxGroup/index.stories.tsx index 1cd91b8..1920970 100644 --- a/webapp/src/components/AutomationManager/ListBoxGroup/index.stories.tsx +++ b/webapp/src/components/AutomationManager/ListBoxGroup/index.stories.tsx @@ -23,6 +23,7 @@ const { make, componentMeta } = makeStory({ id: "1", title: "Example", entityId: "automation.1", + readonly: false, description: "Amazing automation!", tags: {}, state: "on", @@ -38,6 +39,7 @@ const { make, componentMeta } = makeStory({ id: "2", title: "Example 2", entityId: "automation.2", + readonly: false, description: "Cool!", state: "on", source_file: "automation.yaml", @@ -89,6 +91,7 @@ export const Nested = make({ entityId: "automation.1", description: "", title: "Automation #1", + readonly: false, state: "on", source_file: "automation.yaml", source_file_type: "list", @@ -102,6 +105,7 @@ export const Nested = make({ { id: "2", entityId: "automation.2", + readonly: false, description: "another one", title: "Automation #2", state: "off", @@ -117,6 +121,7 @@ export const Nested = make({ { id: "3", entityId: "automation.3", + readonly: false, description: "bad one", title: "Automation #3", state: "offing", @@ -153,6 +158,7 @@ export const Nested = make({ }, data: [ { + readonly: false, id: "4", entityId: "automation.4", description: "Cool thing", diff --git a/webapp/src/components/AutomationManager/Loaded/automationDB.ts b/webapp/src/components/AutomationManager/Loaded/automationDB.ts index acf46b2..88133cb 100644 --- a/webapp/src/components/AutomationManager/Loaded/automationDB.ts +++ b/webapp/src/components/AutomationManager/Loaded/automationDB.ts @@ -52,6 +52,7 @@ export const useAutomationDB = ( const auto = defaultAutomation(String(Date.now())); setNewAuto([ { + readonly: false, id: auto.id, entityId: "", title: auto.alias ?? "", @@ -77,13 +78,18 @@ const genMapping = ( tagsDB: TagDB, langStore: LangStore ) => { - const configData: Record = configAutomations.reduce( - (all, n) => ({ - ...all, - [n.id]: n, - }), - {} - ); + const configDataNoId: AutomationData[] = []; + const configData: Record = + configAutomations.reduce((all, n) => { + if (n.id) { + return { + ...all, + [n.id]: n, + }; + } + configDataNoId.push(n); + return all; + }, {}); const automatioinMap: Record< string, [AutomationManagerAuto, AutomationData | null] @@ -91,7 +97,7 @@ const genMapping = ( Object.entries(haEntities) .filter(([entityId, _]) => entityId.toLowerCase().startsWith("automation.")) .forEach(([entityId, entityData], i) => { - const autoId = entityData.attributes.id ?? `${entityData.state}-${i}`; + const autoId = entityData.attributes.id ?? entityId.split(".")[1]; let title = entityData.attributes.friendly_name ?? ""; let description = ""; let issue: string | undefined = ["on", "off"].includes(entityData.state) @@ -127,12 +133,13 @@ const genMapping = ( source_file_type, configuration_key, issue, + readonly: found == null, }, found, ]; }); - Object.values(configData).forEach((auto) => { + Object.values(configData).forEach((auto, i) => { automatioinMap[auto.id] = [ { id: auto.id, @@ -145,10 +152,55 @@ const genMapping = ( source_file_type: auto.source_file_type, configuration_key: auto.configuration_key, issue: langStore.get("ISSUES_NO_LOAD_AUTOMATION"), + readonly: false, }, auto, ]; }); + let failedGeneration: number = 0; + configDataNoId.forEach((auto) => { + let generatedAutoId = `automation_${failedGeneration + 1}`; + if (auto.alias) { + generatedAutoId = auto.alias + .toLowerCase() + .replaceAll(/\W/g, " ") + .trim() + .replaceAll(" ", "_"); + } else { + failedGeneration += 1; + } + if (automatioinMap[generatedAutoId]) { + automatioinMap[generatedAutoId] = [ + { + ...automatioinMap[generatedAutoId][0], + issue: langStore.get("ISSUE_MUST_CONTAIN_A_UNQIUE_ID_TO_EDIT", { + sourceFile: auto.source_file, + }), + readonly: true, + }, + auto, + ]; + } else { + automatioinMap[generatedAutoId] = [ + { + id: generatedAutoId, + entityId: "", + title: auto.alias ?? "", + description: auto.description ?? "", + state: langStore.get("AUTOMATION_STATE_UNREGISTERED"), + tags: {}, + source_file: auto.source_file, + source_file_type: auto.source_file_type, + configuration_key: auto.configuration_key, + issue: langStore.get("ISSUE_MUST_CONTAIN_A_UNQIUE_ID_TO_EDIT", { + sourceFile: auto.source_file, + }), + readonly: true, + }, + auto, + ]; + } + }); return automatioinMap; }; diff --git a/webapp/src/components/AutomationManager/Loaded/index.tsx b/webapp/src/components/AutomationManager/Loaded/index.tsx index 878f2dc..6c41356 100644 --- a/webapp/src/components/AutomationManager/Loaded/index.tsx +++ b/webapp/src/components/AutomationManager/Loaded/index.tsx @@ -50,6 +50,8 @@ export const AutomationManagerLoaded: FC< {state.currentAutomationId !== null ? ( state.currentAutomation ? ( ({ id, tags, - })), + })) + .filter(({ id }) => typeof id === "string") as Array<{ + id: string; + tags: Record; + }>, onUpdateTags ); const automationDB = useAutomationDB(hassEntities, configAutomations, tagsDB); @@ -78,6 +82,14 @@ export const useAutomationManagerState = ({ get currentAutomationEntityId(): string | null { return currentAutomation === null ? null : currentAutomation[0].entityId; }, + get currentAutomationIsReadOnly(): boolean { + return currentAutomation === null ? true : currentAutomation[0].readonly; + }, + get currentAutomationIssue(): string | undefined { + return currentAutomation === null + ? undefined + : currentAutomation[0].issue; + }, async setSelectedAutomationId(i: string | null, force: boolean = false) { if (methods.currentAutomationIsNew && !force) { if (!(await stillHaveNewAreYouSure())) { @@ -94,7 +106,9 @@ export const useAutomationManagerState = ({ } } const auto = automationDB.addNew(); - methods.setSelectedAutomationId(auto.id, true); + if (auto.id) { + methods.setSelectedAutomationId(auto.id, true); + } }, editorUpdateAutomation(auto: AutomationData | BareAutomationData) { if (methods.currentAutomationIsNew) { diff --git a/webapp/src/components/AutomationManager/Sidebar/index.stories.tsx b/webapp/src/components/AutomationManager/Sidebar/index.stories.tsx index d7283b9..71ef82f 100644 --- a/webapp/src/components/AutomationManager/Sidebar/index.stories.tsx +++ b/webapp/src/components/AutomationManager/Sidebar/index.stories.tsx @@ -71,6 +71,7 @@ const story = makeStory({ source_file: "automation.yaml", source_file_type: "list", configuration_key: ["automation"], + readonly: false, }, ]); setSelAutoId(id); @@ -100,6 +101,7 @@ const makeAuto = ( source_file: "automation.yaml", source_file_type: "list", configuration_key: ["automation"], + readonly: false, ...data, }; }; diff --git a/webapp/src/components/AutomationManager/automationGrouper.test.ts b/webapp/src/components/AutomationManager/automationGrouper.test.ts index ab668cb..2e95ec0 100644 --- a/webapp/src/components/AutomationManager/automationGrouper.test.ts +++ b/webapp/src/components/AutomationManager/automationGrouper.test.ts @@ -38,10 +38,11 @@ const automations: AutomationManagerAuto[] = [ description: a.description ?? "", state: "on", tags: a.tags, - id: a.id, + id: a.id as string, isSelected: false, source_file: "automation.yaml", source_file_type: "list", + readonly: false, configuration_key: ["automation"], })); @@ -51,7 +52,8 @@ const bigList = bigMockAutoList.map((a) => ({ description: a.description ?? "", state: "on", tags: a.tags, - id: a.id, + id: a.id as string, + readonly: false, isSelected: false, source_file: "automation.yaml", source_file_type: "list", diff --git a/webapp/src/components/AutomationManager/types.ts b/webapp/src/components/AutomationManager/types.ts index 739f5da..21fdc25 100644 --- a/webapp/src/components/AutomationManager/types.ts +++ b/webapp/src/components/AutomationManager/types.ts @@ -12,6 +12,7 @@ export type AutomationManagerAuto = AutomationManagerAutoUpdatable & { source_file: string; source_file_type: string; configuration_key: string[]; + readonly: boolean; }; export type AutomationManagerAutoItem = AutomationManagerAuto & { flags: AutomationManagerItemFlags; diff --git a/webapp/src/components/ConditionEditor/editorRender.tsx b/webapp/src/components/ConditionEditor/editorRender.tsx index 869cf28..9e505a7 100644 --- a/webapp/src/components/ConditionEditor/editorRender.tsx +++ b/webapp/src/components/ConditionEditor/editorRender.tsx @@ -271,7 +271,7 @@ export const StateEditor: Editor = ({ } /> onChange({ diff --git a/webapp/src/components/Inputs/AutoComplete/InputArea.tsx b/webapp/src/components/Inputs/AutoComplete/InputArea.tsx index 3afb238..5ba11d3 100644 --- a/webapp/src/components/Inputs/AutoComplete/InputArea.tsx +++ b/webapp/src/components/Inputs/AutoComplete/InputArea.tsx @@ -4,6 +4,7 @@ import { InputAutoCompletePropsBase, } from "./InputAutoComplete"; import { useHAAreas } from "haService/AreaRegistry"; +import { useLang } from "lang"; export type InputAreaProps = InputAutoCompletePropsBase & { restrictToDomain?: string[]; @@ -11,12 +12,13 @@ export type InputAreaProps = InputAutoCompletePropsBase & { }; export const InputArea: FC = (props) => { + const lang = useLang(); // state const areas = useHAAreas(); return ( = { id: string; @@ -65,6 +66,7 @@ export function computeSimilarity( export function InputAutoComplete( props: InputAutoCompleteProps ) { + const lang = useLang(); // aliases const getID = (opt: T): string => { if (props.getID) { @@ -212,7 +214,7 @@ export function InputAutoComplete( }} variant="filled" required={props.required} - label={props.label ?? "Entity ID"} + label={props.label ?? lang.get("ENTITY_ID")} helperText={helperText} disabled={props.disabled} error={error} diff --git a/webapp/src/components/Inputs/AutoComplete/InputDevice.tsx b/webapp/src/components/Inputs/AutoComplete/InputDevice.tsx index e85ea39..0226e14 100644 --- a/webapp/src/components/Inputs/AutoComplete/InputDevice.tsx +++ b/webapp/src/components/Inputs/AutoComplete/InputDevice.tsx @@ -1,5 +1,6 @@ import { FC } from "react"; import { useHADeviceRegistry } from "haService"; +import { useLang } from "lang"; import { InputAutoComplete, InputAutoCompletePropsBase, @@ -9,11 +10,12 @@ export type InputDeviceProps = InputAutoCompletePropsBase; export const InputDevice: FC = (props) => { // state + const lang = useLang(); const dr = useHADeviceRegistry(); return ( = props => { - const services = useHAServices(); - return (typeof opt !== 'string') ? prettyName(opt.domain) : ''} +export const InputService: FC = (props) => { + const services = useHAServices(); + const lang = useLang(); + return ( + (typeof opt !== "string" ? prettyName(opt.domain) : "")} /> -} - + ); +}; diff --git a/webapp/src/components/Inputs/AutoComplete/InputTimeEntity.tsx b/webapp/src/components/Inputs/AutoComplete/InputTimeEntity.tsx index ccd5430..3c5ab08 100644 --- a/webapp/src/components/Inputs/AutoComplete/InputTimeEntity.tsx +++ b/webapp/src/components/Inputs/AutoComplete/InputTimeEntity.tsx @@ -12,6 +12,7 @@ import { } from "utils/time"; import { InputEntity } from "./InputEntities"; import { InputTime } from "components/Inputs/Base/InputTime"; +import { useLang } from "lang"; export type InputTimeEntityProps = { value: string; @@ -23,6 +24,7 @@ export const InputTimeEntity: FC = ({ onChange, className = "", }) => { + const lang = useLang(); const is24Hour = isAutomationTimeString24Hours(value); let editor: ReactNode; if (is24Hour) { @@ -39,7 +41,7 @@ export const InputTimeEntity: FC = ({ } else { editor = ( = [ @@ -29,6 +30,7 @@ export type InputTimeProps = { }; export const InputTime: FC = (props) => { // state + const lang = useLang(); const [disabled, setDisabled] = useState(!props.value); const [displayValue, setDisplayValue] = useState( convertAutomationTimeToTimeObject(props.value) @@ -127,21 +129,26 @@ export const InputTime: FC = (props) => {
{/* {props.label} */}
- {fields.map((name) => ( - updateOne(name)(e.target.value)} - /> - ))} + {fields.map((name) => { + const label = prettyName( + lang.get(`TIME_UNITS_${name}`.toUpperCase()) + ); + return ( + updateOne(name)(e.target.value)} + /> + ); + })} {!props.restrictEmpty && ( setDisabled(!disabled)} diff --git a/webapp/src/components/NodeEditor/OptionManager/ActionEvent.tsx b/webapp/src/components/NodeEditor/OptionManager/ActionEvent.tsx index d411895..58fa504 100644 --- a/webapp/src/components/NodeEditor/OptionManager/ActionEvent.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/ActionEvent.tsx @@ -10,18 +10,18 @@ export const ActionEventState: OptionManager = { event_data: {}, }), isReady: ({ alias, event }) => alias !== "" && event !== "", - renderOptionList: (state, setState) => { + Component: ({ state, setState, langStore }) => { const update = updateActionData(state, setState); return ( <> update({ event })} /> update({ event_data })} /> diff --git a/webapp/src/components/NodeEditor/OptionManager/ActionRepeat.tsx b/webapp/src/components/NodeEditor/OptionManager/ActionRepeat.tsx index b29c989..3fd7587 100644 --- a/webapp/src/components/NodeEditor/OptionManager/ActionRepeat.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/ActionRepeat.tsx @@ -15,12 +15,12 @@ export const ActionRepeatState: OptionManager = { }, }), isReady: () => true, - Component: ({ state, setState }) => { + Component: ({ state, setState, langStore }) => { const repeatType = determineType(state.repeat); return ( <> { @@ -53,7 +53,7 @@ export const ActionRepeatState: OptionManager = { /> {"count" in state.repeat && ( setState({ diff --git a/webapp/src/components/NodeEditor/OptionManager/ActionScene.tsx b/webapp/src/components/NodeEditor/OptionManager/ActionScene.tsx index f055fbc..0eae575 100644 --- a/webapp/src/components/NodeEditor/OptionManager/ActionScene.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/ActionScene.tsx @@ -7,11 +7,11 @@ export const ActionSceneState: OptionManager = { scene: "", }), isReady: () => true, - Component: ({ state, setState }) => { + Component: ({ state, setState, langStore }) => { return ( <> setState({ ...state, scene: d ?? "" })} diff --git a/webapp/src/components/NodeEditor/OptionManager/ActionStop.tsx b/webapp/src/components/NodeEditor/OptionManager/ActionStop.tsx index 5e176aa..a07097d 100644 --- a/webapp/src/components/NodeEditor/OptionManager/ActionStop.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/ActionStop.tsx @@ -8,11 +8,11 @@ export const ActionStopState: OptionManager = { stop: "", }), isReady: () => true, - Component: ({ state, setState }) => { + Component: ({ state, setState, langStore }) => { return ( <> setState({ ...state, stop: d })} /> diff --git a/webapp/src/components/NodeEditor/OptionManager/ActionWait.tsx b/webapp/src/components/NodeEditor/OptionManager/ActionWait.tsx index 0ac5c46..bf744a4 100644 --- a/webapp/src/components/NodeEditor/OptionManager/ActionWait.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/ActionWait.tsx @@ -24,7 +24,7 @@ export const ActionWaitState: OptionManager = { delay: "", }), isReady: () => true, - Component: ({ state, setState }) => { + Component: ({ state, setState, langStore }) => { // state const update = updateActionData(state, setState); const [current, _setCurrent] = useState(getCurrentOption(state)); @@ -63,7 +63,7 @@ export const ActionWaitState: OptionManager = { <> update({ delay })} /> @@ -87,7 +87,7 @@ export const ActionWaitState: OptionManager = { <> update({ wait_for_trigger })} /> @@ -100,7 +100,7 @@ export const ActionWaitState: OptionManager = {
= { {current !== "delay" ? (
= { } /> update({ continue_on_timeout }) diff --git a/webapp/src/components/NodeEditor/OptionManager/OptionManager.ts b/webapp/src/components/NodeEditor/OptionManager/OptionManager.ts index 17a9fe9..01faaa8 100644 --- a/webapp/src/components/NodeEditor/OptionManager/OptionManager.ts +++ b/webapp/src/components/NodeEditor/OptionManager/OptionManager.ts @@ -1,5 +1,6 @@ import { HAService } from "haService"; import { HAEntitiesState } from "haService/HAEntities"; +import { LangStore } from "lang"; import { FC } from "react"; import { AutomationAction } from "types/automations/actions"; import { AutomationCondition } from "types/automations/conditions"; @@ -21,6 +22,7 @@ export type OptionManager = { setState: (s: T) => void; ha: HAService; createMode: boolean; + langStore: LangStore; }>; } ); diff --git a/webapp/src/components/NodeEditor/OptionManager/TriggerTime.tsx b/webapp/src/components/NodeEditor/OptionManager/TriggerTime.tsx index 036e421..cfa7f48 100644 --- a/webapp/src/components/NodeEditor/OptionManager/TriggerTime.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/TriggerTime.tsx @@ -15,6 +15,7 @@ import { Button } from "components/Inputs/Buttons/Button"; import { useState } from "react"; import AddIcon from "@mui/icons-material/Add"; import { InputTimeEntity } from "components/Inputs/AutoComplete/InputTimeEntity"; +import { useLang } from "lang"; export const TriggerTime: OptionManager = { defaultState: () => ({ @@ -115,10 +116,11 @@ export const TriggerTimePattern: FC<{ state: AutomationTriggerTimePattern; setState: (t: AutomationTriggerTimePattern) => void; }> = ({ state, setState }) => { + const lang = useLang(); return (
setState({ @@ -128,7 +130,7 @@ export const TriggerTimePattern: FC<{ } /> setState({ @@ -138,7 +140,7 @@ export const TriggerTimePattern: FC<{ } /> setState({ diff --git a/webapp/src/components/NodeEditor/OptionManager/index.tsx b/webapp/src/components/NodeEditor/OptionManager/index.tsx index 1a14bda..8c1b12f 100644 --- a/webapp/src/components/NodeEditor/OptionManager/index.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/index.tsx @@ -158,6 +158,7 @@ export const useEditorNodeState = ( setState={setState} ha={ha} createMode={createMode} + langStore={langStore} /> )} diff --git a/webapp/src/components/NodeEditor/index.tsx b/webapp/src/components/NodeEditor/index.tsx index 9e3fbbd..2d999a5 100644 --- a/webapp/src/components/NodeEditor/index.tsx +++ b/webapp/src/components/NodeEditor/index.tsx @@ -134,7 +134,7 @@ export const NodeEditor: FC = ({ areYouSureNotReady("close", true).then((ok) => ok && onClose()) } > - Close + {langStore.get("CLOSE")}