diff --git a/api/src/api/routes/details.py b/api/src/api/routes/details.py index 3de9069..ff24270 100644 --- a/api/src/api/routes/details.py +++ b/api/src/api/routes/details.py @@ -3,6 +3,7 @@ from fastapi import APIRouter from src.automations.loader import extract_automation_refs from src.env import BUILD_VERSION, HASSIO_TOKEN, HASSIO_WS from src.hass_config import HassConfig +from src.user_profile import UserProfile def make_details_router(hass_config: HassConfig) -> APIRouter: @@ -16,10 +17,17 @@ def make_details_router(hass_config: HassConfig) -> APIRouter: "hassio_url": HASSIO_WS, "config": hass_config.get_configuration_path(), "tags": hass_config.get_automation_tags_path(), - "autos": { - config_key: auto_file - for config_key, auto_file in extract_automation_refs(hass_config) - }, + "profile": hass_config.get_profile_path(), + "autos": list(map(repr, extract_automation_refs(hass_config))), } + @router.get("/profile") + def read_profile(): + return UserProfile.from_hass_config(hass_config) + + @router.put("/profile") + def save_profile(user_profile: UserProfile): + user_profile.save(hass_config) + return {"saved": hass_config.get_profile_path()} + return router diff --git a/api/src/automations/types.py b/api/src/automations/types.py index 3b6662d..3d4606f 100644 --- a/api/src/automations/types.py +++ b/api/src/automations/types.py @@ -15,11 +15,17 @@ class InlineAutomation(NamedTuple): source_file: Path automations: list[dict] + def __repr__(self) -> str: + return f"InlineAutomation(configuration_key='{self.configuration_key}', source_file='{self.source_file}')" + class IncludedAutoamtion(NamedTuple): configuration_key: ConfigurationKey ref: IncludedYamlDir + def __repr__(self) -> str: + return f"InlineAutomation(configuration_key='{self.configuration_key}', ref='{self.ref}')" + ExtractedAutomation = InlineAutomation | IncludedAutoamtion AutomationExtractedIter = Iterator[ExtractedAutomation] diff --git a/api/src/hass_config/loader.py b/api/src/hass_config/loader.py index 7c339b5..6947d50 100644 --- a/api/src/hass_config/loader.py +++ b/api/src/hass_config/loader.py @@ -60,5 +60,8 @@ class HassConfig: def get_automation_tags_path(self) -> Path: return self.get_shortumations_path() / "tags.yaml" + def get_profile_path(self) -> Path: + return self.get_shortumations_path() / "profile.yaml" + def get_configuration_path(self) -> Path: return self.root_path / "configuration.yaml" diff --git a/api/src/user_profile.py b/api/src/user_profile.py new file mode 100644 index 0000000..d84f789 --- /dev/null +++ b/api/src/user_profile.py @@ -0,0 +1,23 @@ +from typing import Literal +from pydantic import BaseModel + +from src.hass_config import HassConfig +from src.yaml_serializer import load_yaml, dump_yaml + + +class UserProfile(BaseModel): + theme: Literal["dark", "light"] = "dark" + lang: str = "eng" + + @classmethod + def from_hass_config(cls, hass_config: HassConfig): + path = hass_config.get_profile_path() + if path.exists(): + with hass_config.get_profile_path().open("r") as fp: + return cls(**load_yaml(fp, hass_config.root_path)) + else: + return cls() + + def save(self, hass_config: HassConfig): + with hass_config.get_profile_path().open("w") as fp: + fp.write(dump_yaml(self.dict(), hass_config.root_path)) diff --git a/api/tests/unittests/api/test_automations.py b/api/tests/unittests/api/test_automations.py index 57d0902..ada40fa 100644 --- a/api/tests/unittests/api/test_automations.py +++ b/api/tests/unittests/api/test_automations.py @@ -1,27 +1,13 @@ -from pathlib import Path -from unittest import TestCase - import yaml -from fastapi.testclient import TestClient -from src.api.app import make_app from src.automations.types import Automation, ExtenededAutomation from tests.utils import ( HA_CONFIG2_EXAMPLE, HA_CONFIG6_EXAMPLE, HA_CONFIG10_EXAMPLE, - get_example_automation_loader, ) - -class BaseTestCase(TestCase): - def setUp(self) -> None: - ( - self.config_folder, - self.hass_config, - self.automation_loader, - ) = get_example_automation_loader() - self.client = TestClient(make_app(self.automation_loader)) +from .util import BaseTestCase class automation_pagination_tests(BaseTestCase): @@ -90,20 +76,13 @@ class automation_pagination_tests(BaseTestCase): class automation_pagination_tests_2(automation_pagination_tests): + source_config_path = HA_CONFIG2_EXAMPLE test_params = { "total": 1, "subtest2_found_items": 0, "subtest1_found_items": 1, } - def setUp(self) -> None: - ( - self.config_folder, - self.hass_config, - self.automation_loader, - ) = get_example_automation_loader(HA_CONFIG2_EXAMPLE) - self.client = TestClient(make_app(self.automation_loader)) - class automation_update_tests(BaseTestCase): test_params = { @@ -350,6 +329,7 @@ class automation_update_tests(BaseTestCase): class automation_update_split_config_tests(automation_update_tests): + source_config_path = HA_CONFIG6_EXAMPLE test_params = { "total": 12, "modify_item": 3, @@ -378,30 +358,15 @@ class automation_update_split_config_tests(automation_update_tests): }, } - def setUp(self) -> None: - ( - self.config_folder, - self.hass_config, - self.automation_loader, - ) = get_example_automation_loader(HA_CONFIG6_EXAMPLE) - self.client = TestClient(make_app(self.automation_loader)) - class automation_update_split_config_and_inline_config_tests(automation_update_tests): + source_config_path = HA_CONFIG10_EXAMPLE test_params = { "total": 4, "modify_item": 0, "consitency_tests": {}, } - def setUp(self) -> None: - ( - self.config_folder, - self.hass_config, - self.automation_loader, - ) = get_example_automation_loader(HA_CONFIG10_EXAMPLE) - self.client = TestClient(make_app(self.automation_loader)) - class automation_delete_tests(BaseTestCase): def test_delete_some(self): diff --git a/api/tests/unittests/api/test_profile.py b/api/tests/unittests/api/test_profile.py new file mode 100644 index 0000000..166a4e7 --- /dev/null +++ b/api/tests/unittests/api/test_profile.py @@ -0,0 +1,27 @@ +from tests.unittests.api.util import BaseTestCase + + +class user_profile_tests(BaseTestCase): + def test_read_empty_profile(self): + resp = self.client.get("/details/profile") + self.assertEqual(resp.status_code, 200) + self.assertEqual(resp.json()["theme"], "dark") + self.assertEqual(resp.json()["lang"], "eng") + + def test_update_profile(self): + resp = self.client.put("/details/profile", json={"theme": "light", "lang": "fra"}) + self.assertEqual(resp.status_code, 200) + + resp = self.client.get("/details/profile") + self.assertEqual(resp.status_code, 200) + self.assertEqual(resp.json()["theme"], "light") + self.assertEqual(resp.json()["lang"], "fra") + + def test_update_invalid_profile(self): + resp = self.client.put("/details/profile", json={"theme": "creamy", "lang": "fra"}) + self.assertEqual(resp.status_code, 422) + + resp = self.client.get("/details/profile") + self.assertEqual(resp.status_code, 200) + self.assertEqual(resp.json()["theme"], "dark") + self.assertEqual(resp.json()["lang"], "eng") diff --git a/api/tests/unittests/api/util.py b/api/tests/unittests/api/util.py new file mode 100644 index 0000000..aa28ffb --- /dev/null +++ b/api/tests/unittests/api/util.py @@ -0,0 +1,22 @@ +from unittest import TestCase + +from fastapi.testclient import TestClient + +from src.api.app import make_app +from tests.utils import ( + HA_CONFIG_EXAMPLE, + get_example_automation_loader, +) + + +class BaseTestCase(TestCase): + + source_config_path = HA_CONFIG_EXAMPLE + + def setUp(self) -> None: + ( + self.config_folder, + self.hass_config, + self.automation_loader, + ) = get_example_automation_loader(config_to_copy=self.source_config_path) + self.client = TestClient(make_app(self.automation_loader))