40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
import type { HomeAssistantEntity } from "@dpu/shared";
|
|
import { BaseClient } from "@dpu/shared/dist/fastify";
|
|
import { printNetworkError } from "@dpu/shared/dist/logger.js";
|
|
|
|
export class HomeAssistantClient extends BaseClient {
|
|
async getEntityStates(entityIds: string[]): Promise<HomeAssistantEntity[]> {
|
|
try {
|
|
const promises = entityIds.map((id) => this.getEntityState(id));
|
|
return await Promise.all(promises);
|
|
} catch (error) {
|
|
printNetworkError(error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async getEntityState(entityId: string): Promise<HomeAssistantEntity> {
|
|
try {
|
|
const response = await this.getAxios().get<HomeAssistantEntity>(
|
|
`states/${entityId}`,
|
|
);
|
|
return response.data;
|
|
} catch (error) {
|
|
printNetworkError(error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async triggerWebhook(webhookId: string): Promise<unknown> {
|
|
try {
|
|
const response = await this.getAxios().post<HomeAssistantEntity>(
|
|
`webhook/${webhookId}`,
|
|
);
|
|
return response.data;
|
|
} catch (error) {
|
|
printNetworkError(error);
|
|
throw error;
|
|
}
|
|
}
|
|
}
|