rewrite typeysation
This commit is contained in:
@@ -2,26 +2,23 @@ import type {
|
||||
HomeAssistantDeskPositionResult,
|
||||
HomeAssistantEntity,
|
||||
} from "@dpu/shared";
|
||||
import { BaseService, type ServiceResult } from "@dpu/shared/dist/fastify.js";
|
||||
import { logWarning } from "@dpu/shared/dist/logger.js";
|
||||
import { calculateSecondsBetween } from "@dpu/shared/dist/timehelper.js";
|
||||
import { Config } from "../config.js";
|
||||
import type { HomeAssistantClient } from "./client.js";
|
||||
|
||||
export class HomeAssistantService {
|
||||
private client: HomeAssistantClient;
|
||||
|
||||
constructor(client: HomeAssistantClient) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
async startStandingAutomation(): Promise<unknown | null> {
|
||||
export class HomeAssistantService extends BaseService<HomeAssistantClient> {
|
||||
async startStandingAutomation(): Promise<ServiceResult<unknown | string>> {
|
||||
try {
|
||||
const position = await this.getDeskPosition();
|
||||
const positionResult = await this.getDeskPosition();
|
||||
|
||||
if (position === null) {
|
||||
throw Error("error accessing desk api");
|
||||
if (!positionResult.successful) {
|
||||
throw Error(positionResult.result as string);
|
||||
}
|
||||
|
||||
const position = positionResult.result as HomeAssistantDeskPositionResult;
|
||||
|
||||
if (position.as_boolean) {
|
||||
throw Error("desk is already in standing position");
|
||||
}
|
||||
@@ -30,24 +27,29 @@ export class HomeAssistantService {
|
||||
throw Error("desk has moved too recently");
|
||||
}
|
||||
|
||||
return await this.client.triggerWebhook(
|
||||
const result = await this.getClient().triggerWebhook(
|
||||
Config.homeassistant.id_webhook_stand,
|
||||
);
|
||||
|
||||
return this.getSuccessfulResult(result);
|
||||
} catch (error) {
|
||||
logWarning("Error starting stand automation:", error);
|
||||
return null;
|
||||
const error_message = "error starting stand automation";
|
||||
logWarning(error_message, error);
|
||||
return this.getErrorResult(error_message);
|
||||
}
|
||||
}
|
||||
|
||||
async getDeskPosition(): Promise<HomeAssistantDeskPositionResult | null> {
|
||||
async getDeskPosition(): Promise<
|
||||
ServiceResult<HomeAssistantDeskPositionResult | string>
|
||||
> {
|
||||
try {
|
||||
const raw = await this.client.getEntityState(
|
||||
const raw = await this.getClient().getEntityState(
|
||||
Config.homeassistant.id_desk_sensor_binary,
|
||||
);
|
||||
|
||||
const position = Number(raw.state);
|
||||
|
||||
return {
|
||||
const result = {
|
||||
raw,
|
||||
as_boolean: position === 1,
|
||||
as_text: () => {
|
||||
@@ -60,13 +62,16 @@ export class HomeAssistantService {
|
||||
Date.now(),
|
||||
),
|
||||
};
|
||||
|
||||
return this.getSuccessfulResult(result);
|
||||
} catch (error) {
|
||||
logWarning("Error getting desk position:", error);
|
||||
return null;
|
||||
const error_message = "error getting desk position";
|
||||
logWarning(error_message, error);
|
||||
return this.getErrorResult(error_message);
|
||||
}
|
||||
}
|
||||
|
||||
async getTemperatureText(): Promise<string | null> {
|
||||
async getTemperatureText(): Promise<ServiceResult<string>> {
|
||||
try {
|
||||
const entities = await this.getTemperatures();
|
||||
const values = entities
|
||||
@@ -76,30 +81,23 @@ export class HomeAssistantService {
|
||||
values.length > 0
|
||||
? values.reduce((sum, value) => sum + value, 0) / values.length
|
||||
: 0;
|
||||
return average.toFixed(2);
|
||||
const result = average.toFixed(2);
|
||||
return this.getSuccessfulResult(result);
|
||||
} catch (error) {
|
||||
logWarning(`Error getting temperature as text`, error);
|
||||
return null;
|
||||
const error_message = "error getting temperature as text";
|
||||
logWarning(error_message, error);
|
||||
return this.getErrorResult(error_message);
|
||||
}
|
||||
}
|
||||
|
||||
async getTemperatures(): Promise<HomeAssistantEntity[]> {
|
||||
private async getTemperatures(): Promise<HomeAssistantEntity[]> {
|
||||
try {
|
||||
return await this.client.getEntityStates(
|
||||
return await this.getClient().getEntityStates(
|
||||
Config.homeassistant.id_room_sensors,
|
||||
);
|
||||
} catch (error) {
|
||||
logWarning("Error getting temperatures:", error);
|
||||
logWarning("error getting temperatures:", error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async getTemperature(entityId: string): Promise<HomeAssistantEntity | null> {
|
||||
try {
|
||||
return await this.client.getEntityState(entityId);
|
||||
} catch (error) {
|
||||
logWarning(`Error getting temperature for ${entityId}:`, error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user