Skip to content

Commit

Permalink
refactor types
Browse files Browse the repository at this point in the history
  • Loading branch information
mdvanes committed Sep 13, 2024
1 parent 0c34a02 commit 0ba7c5f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
31 changes: 19 additions & 12 deletions apps/server/src/smart-entities/smart-entities.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
GetHaStatesResponse,
PostServicesDomainServiceBody,
PostServicesDomainServiceResponse,
State,
type SmartEntitiesTypes,
} from "@homeremote/types";
Expand Down Expand Up @@ -67,26 +69,28 @@ export class SmartEntitiesController {
);

try {
const switchIds: GetHaStatesResponse = await this.fetchHa(
const listOfIdsResponse: GetHaStatesResponse = await this.fetchHa(
`/api/states/${this.haApiConfig.entityId}`
);
console.log("switchIds", switchIds);
console.log("switchIds", listOfIdsResponse);

if ("message" in switchIds && switchIds.message) {
throw new Error(switchIds.message);
if ("message" in listOfIdsResponse && listOfIdsResponse.message) {
throw new Error(listOfIdsResponse.message);
}

if ("attributes" in switchIds) {
if ("attributes" in listOfIdsResponse) {
const allHaStates = (await this.fetchHa(
`/api/states`
)) as State[];
const switchStates = allHaStates.filter((state) =>
switchIds.attributes.entity_id.includes(state.entity_id)
const entities = allHaStates.filter((state) =>
listOfIdsResponse.attributes.entity_id.includes(
state.entity_id
)
);

return {
switches: switchStates,
} as SmartEntitiesTypes.GetSmartEntitiesResponse;
entities,
};
}

throw new Error("no attributes property");
Expand All @@ -113,8 +117,8 @@ export class SmartEntitiesController {
try {
const [entityType] = entityId.split(".");
const pathType = entityType === "light" ? "light" : "switch";
// TODO use helper and add Arg/Response types
await fetch(
const body: PostServicesDomainServiceBody = { entity_id: entityId };
const response = await fetch(
`${
this.haApiConfig.baseUrl
}/api/services/${pathType}/turn_${args.state.toLowerCase()}`,
Expand All @@ -123,9 +127,12 @@ export class SmartEntitiesController {
headers: {
Authorization: `Bearer ${this.haApiConfig.token}`,
},
body: JSON.stringify({ entity_id: entityId }),
body: JSON.stringify(body),
}
);
const data: PostServicesDomainServiceResponse =
await response.json();
console.log(data);
return "received";
} catch (err) {
this.logger.error(`[${req.user.name}] ${err}`);
Expand Down
7 changes: 4 additions & 3 deletions libs/types/definitions/external/homeAssistant.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,11 @@ paths:
$ref: "#/components/schemas/PostServicesDomainServiceBody"
responses:
"200":
# TODO what will the response be?
description: HaStates
description: Update State Response
content:
application/json:
schema:
$ref: "#/components/schemas/State"
$ref: "#/components/schemas/ServiceResponse"
"400":
description: Bad request.
content:
Expand Down Expand Up @@ -283,3 +282,5 @@ components:
entity_id:
type: string
example: light.favorites
ServiceResponse:
type: array
7 changes: 4 additions & 3 deletions libs/types/src/lib/external/generated/homeAssistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ export interface components {
/** @example light.favorites */
entity_id?: string;
};
ServiceResponse: unknown[];
};
responses: never;
parameters: never;
Expand Down Expand Up @@ -284,7 +285,7 @@ export interface operations {
};
requestBody?: never;
responses: {
/** @description HaStates */
/** @description State */
200: {
headers: {
[name: string]: unknown;
Expand Down Expand Up @@ -331,13 +332,13 @@ export interface operations {
};
};
responses: {
/** @description HaStates */
/** @description Update State Response */
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["State"];
"application/json": components["schemas"]["ServiceResponse"];
};
};
/** @description Bad request. */
Expand Down
7 changes: 7 additions & 0 deletions libs/types/src/lib/external/homeassistant.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ export type GetHaStatesResponse =
| operations["getHaStates"]["responses"]["400"]["content"]["application/json"];

export type State = components["schemas"]["State"];

export type PostServicesDomainServiceBody =
components["schemas"]["PostServicesDomainServiceBody"];

export type PostServicesDomainServiceResponse =
| operations["postServicesDomainService"]["responses"]["200"]["content"]["application/json"]
| operations["postServicesDomainService"]["responses"]["400"]["content"]["application/json"];

0 comments on commit 0ba7c5f

Please sign in to comment.