diff --git a/api/src/api/routes/automations.py b/api/src/api/routes/automations.py index ea06087..39f346f 100644 --- a/api/src/api/routes/automations.py +++ b/api/src/api/routes/automations.py @@ -11,7 +11,7 @@ from src.hass_config.loader import HassConfig def make_automation_router(hass_config: HassConfig) -> APIRouter: router = APIRouter() - @router.post("/list") + @router.post("/list", response_model_exclude_unset=True, response_model_exclude_none=True) def list_autos(body: ListParams) -> ListData[ExtenededAutomation]: automations = AutomationManager(hass_config) try: diff --git a/api/src/automations/types.py b/api/src/automations/types.py index b67d6a9..53266f1 100644 --- a/api/src/automations/types.py +++ b/api/src/automations/types.py @@ -41,27 +41,39 @@ class BaseAutomation(BaseModel): condition: list[dict] = [] action: list[dict] = [] - def to_primitive(self): - return self.dict(exclude_unset=True, exclude_none=True) + def to_primitive(self, exclude_unset=True, exclude_none=True, **kwrgs): + return BaseModel.dict(self, exclude_unset=exclude_unset, exclude_none=exclude_none, **kwrgs) + + def dict(self, **kwrgs): + kwrgs["exclude_unset"] = True + kwrgs["exclude_none"] = True + return self.to_primitive( + **kwrgs, + ) class Automation(BaseAutomation): tags: dict[str, str] = Field(default_factory=dict) - def to_primitive(self, include_tags: bool = False): - out = super().to_primitive() + def to_primitive(self, include_tags: bool = False, **kwrgs): + out = super().to_primitive(**kwrgs) if not include_tags and out.get("tags", None) is not None: del out["tags"] + if include_tags and out.get("tags", None) is None: + out["tags"] = {} return out + def dict(self, **kwrgs): + return BaseAutomation.dict(self, **kwrgs, include_tags=True) + class ExtenededAutomation(Automation): source_file: str source_file_type: Literal["list", "obj", "inline"] configuration_key: ConfigurationKey - def to_primitive(self, include_tags: bool = False): - out = super().to_primitive(include_tags=include_tags) + def to_primitive(self, include_tags: bool = False, **kwrgs): + out = super().to_primitive(include_tags=include_tags, **kwrgs) del out["source_file"] del out["source_file_type"] del out["configuration_key"] diff --git a/webapp/src/apiService/base.ts b/webapp/src/apiService/base.ts index e87047e..3ca4905 100644 --- a/webapp/src/apiService/base.ts +++ b/webapp/src/apiService/base.ts @@ -34,15 +34,9 @@ export const makeRemoteAPI = (baseURL: string): API => ({ return response; } catch (err) { console.error(err); - let error = String(err); - - if (err instanceof Error) { - error += ` ${err.stack}`; - } - return { ok: false, - error, + error: `Server error: ${method} ${path}`, }; } },