change from poll to update from hooks
This commit is contained in:
@@ -5,7 +5,6 @@ import {
|
||||
type HomeAssistantEntity,
|
||||
type ServiceResult,
|
||||
} from "@dpu/shared";
|
||||
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";
|
||||
@@ -37,9 +36,7 @@ export class HomeAssistantService extends BaseService<HomeAssistantClient> {
|
||||
|
||||
return this.getSuccessfulResult(result);
|
||||
} catch (error) {
|
||||
const error_message = `error starting stand automation. ${error instanceof Error ? error.message : error}`;
|
||||
logWarning(error_message);
|
||||
return this.getErrorResult(error_message);
|
||||
return this.getErrorResult("error starting stand automation.", error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,25 +48,27 @@ export class HomeAssistantService extends BaseService<HomeAssistantClient> {
|
||||
Config.homeassistant.id_sensor_desk_binary,
|
||||
);
|
||||
|
||||
const position = Number(raw.state);
|
||||
|
||||
const result = {
|
||||
raw,
|
||||
as_boolean: position === 1,
|
||||
last_changed: calculateSecondsBetween(
|
||||
new Date(raw.last_changed).getTime(),
|
||||
Date.now(),
|
||||
),
|
||||
};
|
||||
|
||||
return this.getSuccessfulResult(result);
|
||||
return this.getSuccessfulResult(this.convertHaEntityToPosResult(raw));
|
||||
} catch (error) {
|
||||
const error_message = "error getting desk position";
|
||||
logWarning(error_message, error);
|
||||
return this.getErrorResult(error_message);
|
||||
return this.getErrorResult("error getting desk position.", error);
|
||||
}
|
||||
}
|
||||
|
||||
convertHaEntityToPosResult(
|
||||
raw: HomeAssistantEntity,
|
||||
): HomeAssistantDeskPositionResult {
|
||||
const position = Number(raw.state);
|
||||
|
||||
return {
|
||||
raw,
|
||||
as_boolean: position === 1,
|
||||
last_changed: calculateSecondsBetween(
|
||||
new Date(raw.last_changed).getTime(),
|
||||
Date.now(),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
convertPosResultToApiAnswer(
|
||||
position: HomeAssistantDeskPositionResult,
|
||||
): API_HA_DeskPosition {
|
||||
@@ -80,33 +79,15 @@ export class HomeAssistantService extends BaseService<HomeAssistantClient> {
|
||||
};
|
||||
}
|
||||
|
||||
async getTemperatureText(): Promise<ServiceResult<string>> {
|
||||
async getTemperature(): Promise<ServiceResult<string>> {
|
||||
try {
|
||||
const entities = await this.getTemperatures();
|
||||
const values = entities
|
||||
.map((entity) => parseFloat(entity.state))
|
||||
.filter((value) => !Number.isNaN(value));
|
||||
const average =
|
||||
values.length > 0
|
||||
? values.reduce((sum, value) => sum + value, 0) / values.length
|
||||
: 0;
|
||||
const result = average.toFixed(2);
|
||||
return this.getSuccessfulResult(result);
|
||||
} catch (error) {
|
||||
const error_message = "error getting temperature as text";
|
||||
logWarning(error_message, error);
|
||||
return this.getErrorResult(error_message);
|
||||
}
|
||||
}
|
||||
|
||||
private async getTemperatures(): Promise<HomeAssistantEntity[]> {
|
||||
try {
|
||||
return await this.getClient().getEntityStates(
|
||||
Config.homeassistant.id_sensors_roomtemp,
|
||||
const entity = await this.getClient().getEntityState(
|
||||
Config.homeassistant.id_sensor_roomtemp,
|
||||
);
|
||||
|
||||
return this.getSuccessfulResult(entity.state);
|
||||
} catch (error) {
|
||||
logWarning("error getting temperatures:", error);
|
||||
return [];
|
||||
return this.getErrorResult("error getting temperature.", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user