diff --git a/package-lock.json b/package-lock.json index 679daca..f2c467e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,8 +26,8 @@ } }, "node_modules/@dpu/shared": { - "version": "1.6.2", - "resolved": "git+https://git.dariusbag.dev/DarDarBinks/dpu-shared.git#ceadd4e5a2db3e94234a455d66338fb94fccea40", + "version": "1.6.4", + "resolved": "git+https://git.dariusbag.dev/DarDarBinks/dpu-shared.git#60bcd23f5b77034777a792bbe8f33a62e92ba246", "dependencies": { "axios": "^1.7.9", "chalk": "^5.6.2", diff --git a/src/homeassistant/routes.ts b/src/homeassistant/routes.ts index d768dd2..b850e33 100644 --- a/src/homeassistant/routes.ts +++ b/src/homeassistant/routes.ts @@ -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; - }, + fastify: FastifyInstance, + { + haService, + verifyAPIKey, + }: { + haService: HomeAssistantService; + verifyAPIKey: ( + request: FastifyRequest, + reply: FastifyReply, + ) => Promise; + }, ) { - 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; + }, + ); } diff --git a/src/homeassistant/service.ts b/src/homeassistant/service.ts index 8e9cc6b..9a934c8 100644 --- a/src/homeassistant/service.ts +++ b/src/homeassistant/service.ts @@ -82,6 +82,7 @@ export class HomeAssistantService extends BaseService { position: position.as_text(), is_standing: position.as_boolean, last_changed: position.last_changed.toReadable(true), + last_changed_seconds: position.last_changed.seconds, }; }