added user profile
This commit is contained in:
1 parent
caad458b48
commit
95ac286b53
7 files changed
+97
-43
No files matched your search
@@ -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
|
||||
@@ -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]
|
||||
|
||||
@@ -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"
|
||||
@@ -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))
|
||||
Reference in new issue
Block a user