From ade7ae722d5803c38017d6a2fb681058da298e40 Mon Sep 17 00:00:00 2001 From: Darius Date: Sun, 16 Nov 2025 20:18:10 +0100 Subject: [PATCH] stand impl part 1 --- .env.example | 1 + src/commands/impl/stand.ts | 40 +++++++++++++++++++++++++++++++++ src/config/config.ts | 2 ++ src/util/api-homeassistant.ts | 42 ++++++++++++++++++++++++++++++----- 4 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 src/commands/impl/stand.ts diff --git a/.env.example b/.env.example index a1a8e3b..7fa9c63 100644 --- a/.env.example +++ b/.env.example @@ -13,3 +13,4 @@ HA_API_TOKEN=Nina hätte hier jetzt ihr Token ausversehen stehen hihi HA_DESK_SENSOR_ID=entityId HA_DESK_SENSOR_TEXT=entityId HA_ROOMTEMP_SENSOR_IDS=entityId(,separated) +HA_STANDING_WEBHOOK=webhookId diff --git a/src/commands/impl/stand.ts b/src/commands/impl/stand.ts new file mode 100644 index 0000000..7eddcf9 --- /dev/null +++ b/src/commands/impl/stand.ts @@ -0,0 +1,40 @@ +import type { ChatMessage } from "@twurple/chat"; +import { getDeskHeight, startStandingAutomation } from "../../util/api-homeassistant.js"; +import { logInfo, logSuccess } from "../../util/logger.js"; +import { BaseCommand } from "../base-command.js"; +import type { ICommandRequirements } from "../interface.ts"; + +export class PositionCommand extends BaseCommand { + name = "stand"; + cooldown = 60; + enabled = true; + + requirements: ICommandRequirements = { + developer: true, + mod: false, + }; + + triggered = async ( + channel: string, + user: string, + _text: string, + msg: ChatMessage, + ) => { + logSuccess(`${channel} ${user} position command triggered`); + + // const position = await getDeskHeight(); + + // if (position?.state > 100) { + + // } + + const response = await startStandingAutomation(); + logInfo(response) + + + + this.chatClient.say(channel, `blabla`, { + replyTo: msg, + }); + }; +} diff --git a/src/config/config.ts b/src/config/config.ts index 2efce82..4313254 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -22,5 +22,7 @@ export const Config = { id_desk_sensor: process.env.HA_DESK_SENSOR_ID || "", id_desk_sensor_text: process.env.HA_DESK_SENSOR_TEXT || "", id_room_sensors: process.env.HA_ROOMTEMP_SENSOR_IDS?.split(",") || [], + + id_webhook_stand: process.env.HA_STANDING_WEBHOOK || "" }, } as const; diff --git a/src/util/api-homeassistant.ts b/src/util/api-homeassistant.ts index 2b7b3d9..fc7cf78 100644 --- a/src/util/api-homeassistant.ts +++ b/src/util/api-homeassistant.ts @@ -22,9 +22,24 @@ type HomeAssistantEntity = { }; }; +// ACTIONS +export async function startStandingAutomation(): Promise { + try { + const position = await sendRequestToHomeAssistantWebhook( + Config.homeassistant.id_webhook_stand, + ); + return position; + } catch { + logWarning("error starting stand automation"); + return null; + } +} + +// GETTER + export async function getDeskPositionText(): Promise { try { - const position = await sendRequestToHomeAssistant( + const position = await sendRequestToHomeAssistantStates( Config.homeassistant.id_desk_sensor_text, ); return position; @@ -36,7 +51,7 @@ export async function getDeskPositionText(): Promise export async function getDeskHeight(): Promise { try { - return await sendRequestToHomeAssistant( + return await sendRequestToHomeAssistantStates( Config.homeassistant.id_desk_sensor, ); } catch { @@ -51,7 +66,7 @@ export async function getTemperatures(): Promise> { const results = []; for (const sensor_id of sensors) { - const res = await sendRequestToHomeAssistant(sensor_id); + const res = await sendRequestToHomeAssistantStates(sensor_id); results.push(res); } @@ -62,11 +77,28 @@ export async function getTemperatures(): Promise> { } } -async function sendRequestToHomeAssistant( +// UTILITY + +async function sendRequestToHomeAssistantStates( entity_id: string, ): Promise { const response = await axios.get( - Config.homeassistant.api_url + entity_id, + `${Config.homeassistant.api_url}states/"${entity_id}`, + { + headers: { + Authorization: `Bearer ${Config.homeassistant.api_token}`, + }, + }, + ); + + return response.data; +} + +async function sendRequestToHomeAssistantWebhook( + webhook_id: string, +): Promise { + const response = await axios.post( + `${Config.homeassistant.api_url}webhook/"${webhook_id}`, { headers: { Authorization: `Bearer ${Config.homeassistant.api_token}`,