Skip to content

Commit

Permalink
fixed return values from backend
Browse files Browse the repository at this point in the history
  • Loading branch information
asosnovsky committed Sep 2, 2022
1 parent c461640 commit 7526b57
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion api/src/api/routes/automations.py
Expand Up @@ -11,7 +11,7 @@
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:
Expand Down
24 changes: 18 additions & 6 deletions api/src/automations/types.py
Expand Up @@ -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"]
Expand Down
8 changes: 1 addition & 7 deletions webapp/src/apiService/base.ts
Expand Up @@ -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}`,
};
}
},
Expand Down

0 comments on commit 7526b57

Please sign in to comment.