add seconds to standing/sitting time

This commit is contained in:
Darius
2026-02-05 22:53:56 +01:00
parent 7b6a056141
commit 07719bbc1f
3 changed files with 94 additions and 90 deletions

View File

@@ -4,101 +4,104 @@ import { z } from "zod";
import type { HomeAssistantService } from "../homeassistant/service.js";
export async function homeAssistantRoutes(
fastify: FastifyInstance,
{
haService,
verifyAPIKey,
}: {
haService: HomeAssistantService;
verifyAPIKey: (
request: FastifyRequest,
reply: FastifyReply,
) => Promise<void>;
},
fastify: FastifyInstance,
{
haService,
verifyAPIKey,
}: {
haService: HomeAssistantService;
verifyAPIKey: (
request: FastifyRequest,
reply: FastifyReply,
) => Promise<void>;
},
) {
fastify.get(
"/homeassistant/desk/position",
{
schema: {
description: "Get current desk position",
tags: ["homeassistant"],
response: {
200: z.object({
position: z.string(),
is_standing: z.boolean(),
last_changed: z.string(),
}),
418: z.object({
error: z.string(),
}),
},
},
},
async (_request, reply) => {
const service_result = await haService.getDeskPosition();
fastify.get(
"/homeassistant/desk/position",
{
schema: {
description: "Get current desk position",
tags: ["homeassistant"],
response: {
200: z.object({
position: z.string(),
is_standing: z.boolean(),
last_changed: z.string(),
last_changed_seconds: z.number(),
}),
418: z.object({
error: z.string(),
}),
},
},
},
async (_request, reply) => {
const service_result = await haService.getDeskPosition();
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
return haService.convertPosResultToApiAnswer(service_result.result as HomeAssistantDeskPositionResult);
},
);
return haService.convertPosResultToApiAnswer(
service_result.result as HomeAssistantDeskPositionResult,
);
},
);
fastify.post(
"/homeassistant/desk/stand",
{
preHandler: verifyAPIKey,
schema: {
description: "Trigger standing desk automation",
tags: ["homeassistant"],
response: {
200: z.unknown(),
401: z.object({
error: z.literal("Invalid API key"),
}),
418: z.object({
error: z.string(),
}),
},
},
},
async (_request, reply) => {
const service_result = await haService.startStandingAutomation();
fastify.post(
"/homeassistant/desk/stand",
{
preHandler: verifyAPIKey,
schema: {
description: "Trigger standing desk automation",
tags: ["homeassistant"],
response: {
200: z.unknown(),
401: z.object({
error: z.literal("Invalid API key"),
}),
418: z.object({
error: z.string(),
}),
},
},
},
async (_request, reply) => {
const service_result = await haService.startStandingAutomation();
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
return service_result.result;
},
);
return service_result.result;
},
);
fastify.get(
"/homeassistant/temperature",
{
schema: {
description: "Get current room temperature",
tags: ["homeassistant"],
response: {
200: z.string(),
418: z.object({
error: z.string(),
}),
},
},
},
async (_request, reply) => {
const service_result = await haService.getTemperatureText();
fastify.get(
"/homeassistant/temperature",
{
schema: {
description: "Get current room temperature",
tags: ["homeassistant"],
response: {
200: z.string(),
418: z.object({
error: z.string(),
}),
},
},
},
async (_request, reply) => {
const service_result = await haService.getTemperatureText();
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
return service_result.result;
},
);
return service_result.result;
},
);
}

View File

@@ -82,6 +82,7 @@ export class HomeAssistantService extends BaseService<HomeAssistantClient> {
position: position.as_text(),
is_standing: position.as_boolean,
last_changed: position.last_changed.toReadable(true),
last_changed_seconds: position.last_changed.seconds,
};
}