implement for desk control
This commit is contained in:
@@ -11,7 +11,6 @@ TIDAL_PORT=47836
|
|||||||
NIGHT_TIME=01:00-08:00
|
NIGHT_TIME=01:00-08:00
|
||||||
HA_API_URL=http://homeassistant.com/api/states/
|
HA_API_URL=http://homeassistant.com/api/states/
|
||||||
HA_API_TOKEN=Nina hätte hier jetzt ihr Token ausversehen stehen hihi
|
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_ROOMTEMP_SENSOR_IDS=entityId(,separated)
|
||||||
HA_STANDING_WEBHOOK=webhookId
|
HA_STANDING_WEBHOOK=webhookId
|
||||||
|
HA_DESK_SENSOR_BINARY=entityId
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import type { ChatMessage } from "@twurple/chat";
|
import type { ChatMessage } from "@twurple/chat";
|
||||||
import { getDeskPositionText } from "../../util/api-homeassistant.js";
|
import { getDeskPosition } from "../../util/api-homeassistant.js";
|
||||||
import { calculateSecondsBetween } from "../../util/general.js";
|
import { calculateSecondsBetween } from "../../util/general.js";
|
||||||
import { logSuccess } from "../../util/logger.js";
|
import { logSuccess } from "../../util/logger.js";
|
||||||
import { BaseCommand } from "../base-command.js";
|
import { BaseCommand } from "../base-command.js";
|
||||||
@@ -22,15 +22,15 @@ export class PositionCommand extends BaseCommand {
|
|||||||
msg: ChatMessage,
|
msg: ChatMessage,
|
||||||
) => {
|
) => {
|
||||||
logSuccess(`${channel} ${user} position command triggered`);
|
logSuccess(`${channel} ${user} position command triggered`);
|
||||||
const position = await getDeskPositionText();
|
const position = await getDeskPosition();
|
||||||
if (position) {
|
if (position) {
|
||||||
const time = calculateSecondsBetween(
|
const time = calculateSecondsBetween(
|
||||||
new Date(position.last_changed).getTime(),
|
new Date(position.raw.last_changed).getTime(),
|
||||||
Date.now(),
|
Date.now(),
|
||||||
).toReadable(true);
|
).toReadable(true);
|
||||||
this.chatClient.say(
|
this.chatClient.say(
|
||||||
channel,
|
channel,
|
||||||
`darius has been ${position.state} for ${time}`,
|
`darius has been ${position.asText()} for ${time}`,
|
||||||
{
|
{
|
||||||
replyTo: msg,
|
replyTo: msg,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
import type { ChatMessage } from "@twurple/chat";
|
import type { ChatMessage } from "@twurple/chat";
|
||||||
import { getDeskHeight, startStandingAutomation } from "../../util/api-homeassistant.js";
|
import { getDeskPosition, startStandingAutomation } from "../../util/api-homeassistant.js";
|
||||||
import { logInfo, logSuccess } from "../../util/logger.js";
|
import { logInfo, logSuccess } from "../../util/logger.js";
|
||||||
import { BaseCommand } from "../base-command.js";
|
import { BaseCommand } from "../base-command.js";
|
||||||
import type { ICommandRequirements } from "../interface.ts";
|
import type { ICommandRequirements } from "../interface.ts";
|
||||||
import { Config } from "../../config/config.js";
|
import { Config } from "../../config/config.js";
|
||||||
|
import { calculateSecondsBetween } from "../../util/general.js";
|
||||||
|
|
||||||
export class StandCommand extends BaseCommand {
|
export class StandCommand extends BaseCommand {
|
||||||
name = "stand";
|
name = "stand";
|
||||||
cooldown = 60;
|
cooldown = 300;
|
||||||
enabled = true;
|
enabled = true;
|
||||||
|
|
||||||
requirements: ICommandRequirements = {
|
requirements: ICommandRequirements = {
|
||||||
@@ -23,11 +24,33 @@ export class StandCommand extends BaseCommand {
|
|||||||
) => {
|
) => {
|
||||||
logSuccess(`${channel} ${user} position command triggered`);
|
logSuccess(`${channel} ${user} position command triggered`);
|
||||||
|
|
||||||
// const position = await getDeskHeight();
|
const position = await getDeskPosition();
|
||||||
|
|
||||||
// if (position?.state > 100) {
|
if (!position) {
|
||||||
|
this.chatClient.say(channel, `error: aborting operation`, {
|
||||||
|
replyTo: msg,
|
||||||
|
});
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// }
|
if (position.asBoolean) {
|
||||||
|
this.chatClient.say(channel, `desk already is in standing mode`, {
|
||||||
|
replyTo: msg,
|
||||||
|
});
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const lastMoved = calculateSecondsBetween(
|
||||||
|
new Date(position.raw.last_changed).getTime(),
|
||||||
|
Date.now(),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (60 > lastMoved.seconds) {
|
||||||
|
this.chatClient.say(channel, `desk has moved too recently. please wait`, {
|
||||||
|
replyTo: msg,
|
||||||
|
});
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (Config.night_time.contains()) {
|
if (Config.night_time.contains()) {
|
||||||
this.chatClient.say(channel, `command disabled during nighttime`, {
|
this.chatClient.say(channel, `command disabled during nighttime`, {
|
||||||
@@ -39,7 +62,7 @@ export class StandCommand extends BaseCommand {
|
|||||||
const response = await startStandingAutomation();
|
const response = await startStandingAutomation();
|
||||||
logInfo(response)
|
logInfo(response)
|
||||||
|
|
||||||
this.chatClient.say(channel, `blabla`, {
|
this.chatClient.say(channel, `desk is now moving into standing position`, {
|
||||||
replyTo: msg,
|
replyTo: msg,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import dotenv from "dotenv";
|
import dotenv from "dotenv";
|
||||||
|
import { TimeSpan } from "../util/timespan";
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
@@ -20,8 +21,7 @@ export const Config = {
|
|||||||
api_url: process.env.HA_API_URL || "",
|
api_url: process.env.HA_API_URL || "",
|
||||||
api_token: process.env.HA_API_TOKEN || "",
|
api_token: process.env.HA_API_TOKEN || "",
|
||||||
|
|
||||||
id_desk_sensor: process.env.HA_DESK_SENSOR_ID || "",
|
id_desk_sensor_binary: process.env.HA_DESK_SENSOR_BINARY || "",
|
||||||
id_desk_sensor_text: process.env.HA_DESK_SENSOR_TEXT || "",
|
|
||||||
id_room_sensors: process.env.HA_ROOMTEMP_SENSOR_IDS?.split(",") || [],
|
id_room_sensors: process.env.HA_ROOMTEMP_SENSOR_IDS?.split(",") || [],
|
||||||
|
|
||||||
id_webhook_stand: process.env.HA_STANDING_WEBHOOK || ""
|
id_webhook_stand: process.env.HA_STANDING_WEBHOOK || ""
|
||||||
|
|||||||
@@ -22,6 +22,12 @@ type HomeAssistantEntity = {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
interface DeskPositionResult {
|
||||||
|
raw: HomeAssistantEntity;
|
||||||
|
asBoolean: boolean;
|
||||||
|
asText: () => string;
|
||||||
|
}
|
||||||
|
|
||||||
// ACTIONS
|
// ACTIONS
|
||||||
export async function startStandingAutomation(): Promise<unknown | null> {
|
export async function startStandingAutomation(): Promise<unknown | null> {
|
||||||
try {
|
try {
|
||||||
@@ -36,26 +42,26 @@ export async function startStandingAutomation(): Promise<unknown | null> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GETTER
|
// GETTER
|
||||||
|
export async function getDeskPosition(): Promise<DeskPositionResult | null> {
|
||||||
export async function getDeskPositionText(): Promise<HomeAssistantEntity | null> {
|
|
||||||
try {
|
try {
|
||||||
const position = await sendRequestToHomeAssistantStates(
|
const raw = await sendRequestToHomeAssistantStates(
|
||||||
Config.homeassistant.id_desk_sensor_text,
|
Config.homeassistant.id_desk_sensor_binary,
|
||||||
);
|
);
|
||||||
return position;
|
if (raw) {
|
||||||
} catch {
|
const position = Number(raw.state);
|
||||||
logWarning("error getting hoehe text from homeassistant");
|
return {
|
||||||
|
raw,
|
||||||
|
asBoolean: position === 1,
|
||||||
|
asText: () => {
|
||||||
|
if (position === 1) return 'standing';
|
||||||
|
if (position === 0) return 'sitting';
|
||||||
|
return 'unknown';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getDeskHeight(): Promise<HomeAssistantEntity | null> {
|
|
||||||
try {
|
|
||||||
return await sendRequestToHomeAssistantStates(
|
|
||||||
Config.homeassistant.id_desk_sensor,
|
|
||||||
);
|
|
||||||
} catch {
|
} catch {
|
||||||
logWarning("error getting hoehe from homeassistant");
|
logWarning("error getting position from homeassistant");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
class TimeSpan {
|
export class TimeSpan {
|
||||||
private start: { hours: number; minutes: number };
|
private start: { hours: number; minutes: number };
|
||||||
private end: { hours: number; minutes: number };
|
private end: { hours: number; minutes: number };
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user