diff --git a/apps/server/src/smart-entities/smart-entities.controller.ts b/apps/server/src/smart-entities/smart-entities.controller.ts index bf36c22..3cac728 100644 --- a/apps/server/src/smart-entities/smart-entities.controller.ts +++ b/apps/server/src/smart-entities/smart-entities.controller.ts @@ -1,5 +1,7 @@ import { GetHaStatesResponse, + PostServicesDomainServiceBody, + PostServicesDomainServiceResponse, State, type SmartEntitiesTypes, } from "@homeremote/types"; @@ -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"); @@ -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()}`, @@ -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}`); diff --git a/libs/types/definitions/external/homeAssistant.yml b/libs/types/definitions/external/homeAssistant.yml index 6d55365..d0ece62 100644 --- a/libs/types/definitions/external/homeAssistant.yml +++ b/libs/types/definitions/external/homeAssistant.yml @@ -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: @@ -283,3 +282,5 @@ components: entity_id: type: string example: light.favorites + ServiceResponse: + type: array diff --git a/libs/types/src/lib/external/generated/homeAssistant.ts b/libs/types/src/lib/external/generated/homeAssistant.ts index 874d52d..20535d2 100644 --- a/libs/types/src/lib/external/generated/homeAssistant.ts +++ b/libs/types/src/lib/external/generated/homeAssistant.ts @@ -225,6 +225,7 @@ export interface components { /** @example light.favorites */ entity_id?: string; }; + ServiceResponse: unknown[]; }; responses: never; parameters: never; @@ -284,7 +285,7 @@ export interface operations { }; requestBody?: never; responses: { - /** @description HaStates */ + /** @description State */ 200: { headers: { [name: string]: unknown; @@ -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. */ diff --git a/libs/types/src/lib/external/homeassistant.types.ts b/libs/types/src/lib/external/homeassistant.types.ts index 785e4d7..bc2a2cd 100644 --- a/libs/types/src/lib/external/homeassistant.types.ts +++ b/libs/types/src/lib/external/homeassistant.types.ts @@ -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"];