privatization
This commit is contained in:
106
src/homeassistant/private-routes.ts
Normal file
106
src/homeassistant/private-routes.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
import type { HomeAssistantDeskPositionResult } from "@dpu/shared";
|
||||
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
||||
import { z } from "zod";
|
||||
import type { HomeAssistantService } from "./service.js";
|
||||
|
||||
export async function privateHomeAssistantRoutes(
|
||||
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({
|
||||
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 };
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
if (!service_result.successful) {
|
||||
reply.code(418);
|
||||
return { error: 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.getTemperature();
|
||||
|
||||
if (!service_result.successful) {
|
||||
reply.code(418);
|
||||
return { error: service_result.result };
|
||||
}
|
||||
|
||||
return service_result.result;
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user