stand impl part 1
This commit is contained in:
@@ -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
|
||||
|
||||
40
src/commands/impl/stand.ts
Normal file
40
src/commands/impl/stand.ts
Normal file
@@ -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,
|
||||
});
|
||||
};
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -22,9 +22,24 @@ type HomeAssistantEntity = {
|
||||
};
|
||||
};
|
||||
|
||||
// ACTIONS
|
||||
export async function startStandingAutomation(): Promise<unknown | null> {
|
||||
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<HomeAssistantEntity | null> {
|
||||
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<HomeAssistantEntity | null>
|
||||
|
||||
export async function getDeskHeight(): Promise<HomeAssistantEntity | null> {
|
||||
try {
|
||||
return await sendRequestToHomeAssistant(
|
||||
return await sendRequestToHomeAssistantStates(
|
||||
Config.homeassistant.id_desk_sensor,
|
||||
);
|
||||
} catch {
|
||||
@@ -51,7 +66,7 @@ export async function getTemperatures(): Promise<Array<HomeAssistantEntity>> {
|
||||
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<Array<HomeAssistantEntity>> {
|
||||
}
|
||||
}
|
||||
|
||||
async function sendRequestToHomeAssistant(
|
||||
// UTILITY
|
||||
|
||||
async function sendRequestToHomeAssistantStates(
|
||||
entity_id: string,
|
||||
): Promise<HomeAssistantEntity> {
|
||||
const response = await axios.get<HomeAssistantEntity>(
|
||||
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<unknown> {
|
||||
const response = await axios.post<HomeAssistantEntity>(
|
||||
`${Config.homeassistant.api_url}webhook/"${webhook_id}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${Config.homeassistant.api_token}`,
|
||||
|
||||
Reference in New Issue
Block a user