initial
This commit is contained in:
45
src/homeassistant/client.ts
Normal file
45
src/homeassistant/client.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import type { HomeAssistantEntity } from "@dpu/shared";
|
||||
import { printNetworkError } from "@dpu/shared/dist/utility";
|
||||
import type { AxiosInstance } from "axios";
|
||||
|
||||
export class HomeAssistantClient {
|
||||
private axiosInstance: AxiosInstance;
|
||||
|
||||
constructor(axiosInstance: AxiosInstance) {
|
||||
this.axiosInstance = axiosInstance;
|
||||
}
|
||||
|
||||
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.axiosInstance.get<HomeAssistantEntity>(
|
||||
`states/${entityId}`,
|
||||
);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
printNetworkError(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async triggerWebhook(webhookId: string): Promise<unknown> {
|
||||
try {
|
||||
const response = await this.axiosInstance.post<HomeAssistantEntity>(
|
||||
`webhook/${webhookId}`,
|
||||
);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
printNetworkError(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user