add desk text sensor and change detection
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import type { ChatMessage } from "@twurple/chat";
|
||||
import { getDeskHeight } from "../../util/api-homeassistant.ts";
|
||||
import { getDeskPositionText } from "../../util/api-homeassistant.ts";
|
||||
import { calculateSecondsBetween } from "../../util/general.ts";
|
||||
import { logSuccess } from "../../util/logger.ts";
|
||||
import { BaseCommand } from "../base-command.ts";
|
||||
import type { ICommandRequirements } from "../interface.ts";
|
||||
@@ -21,28 +22,23 @@ export class PositionCommand extends BaseCommand {
|
||||
msg: ChatMessage,
|
||||
) => {
|
||||
logSuccess(`${channel} ${user} position command triggered`);
|
||||
const position = await getPosition();
|
||||
this.chatClient.say(channel, `darius is ${position} right now`, {
|
||||
const position = await getDeskPositionText();
|
||||
if (position) {
|
||||
const time = calculateSecondsBetween(
|
||||
new Date(position.last_changed).getTime(),
|
||||
Date.now(),
|
||||
).toReadable(true);
|
||||
this.chatClient.say(
|
||||
channel,
|
||||
`darius has been ${position.state} for ${time}`,
|
||||
{
|
||||
replyTo: msg,
|
||||
},
|
||||
);
|
||||
return;
|
||||
}
|
||||
this.chatClient.say(channel, `darius' position is unkown right now`, {
|
||||
replyTo: msg,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
async function getPosition(): Promise<string> {
|
||||
const heightFromHA = await getDeskHeight();
|
||||
if (!heightFromHA) {
|
||||
return "unkown";
|
||||
}
|
||||
const height = convertHeightToCentimeters(heightFromHA.state);
|
||||
if (height > 95) {
|
||||
return "standing";
|
||||
} else {
|
||||
return "sitting";
|
||||
}
|
||||
}
|
||||
|
||||
function convertHeightToCentimeters(height: string): number {
|
||||
const meters = parseFloat(height);
|
||||
const centimeters = Math.round(meters * 100);
|
||||
return centimeters;
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ export const Config = {
|
||||
api_token: process.env.HA_API_TOKEN || "",
|
||||
|
||||
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(",") || [],
|
||||
},
|
||||
} as const;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { HellFreezesOverError, type UserIdResolvable } from "@twurple/api";
|
||||
import type { UserIdResolvable } from "@twurple/api";
|
||||
import { Config } from "../config/config.ts";
|
||||
import { apiClient } from "../core/client.ts";
|
||||
import { getUserId } from "./general.ts";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import axios from "axios";
|
||||
import { Config } from "../config/config.ts";
|
||||
import { logWarning } from "./logger.ts";
|
||||
import { logInfo, logWarning } from "./logger.ts";
|
||||
|
||||
type HomeAssistantEntity = {
|
||||
entity_id: string;
|
||||
@@ -22,6 +22,18 @@ type HomeAssistantEntity = {
|
||||
};
|
||||
};
|
||||
|
||||
export async function getDeskPositionText(): Promise<HomeAssistantEntity | null> {
|
||||
try {
|
||||
const position = await sendRequestToHomeAssistant(
|
||||
Config.homeassistant.id_desk_sensor_text,
|
||||
);
|
||||
return position;
|
||||
} catch {
|
||||
logWarning("error getting hoehe text from homeassistant");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getDeskHeight(): Promise<HomeAssistantEntity | null> {
|
||||
try {
|
||||
return await sendRequestToHomeAssistant(
|
||||
|
||||
@@ -30,19 +30,25 @@ export async function promptForInput(prompt: string): Promise<string> {
|
||||
export function calculateSecondsBetween(
|
||||
start: number,
|
||||
end: number,
|
||||
): { seconds: number; toReadable: () => string } {
|
||||
): { seconds: number; toReadable: (roundToMinutes?: boolean) => string } {
|
||||
const seconds = (end - start) / 1000;
|
||||
return {
|
||||
seconds,
|
||||
toReadable: () => secondsToReadable(seconds),
|
||||
toReadable: (roundToMinutes?: boolean) =>
|
||||
secondsToReadable(seconds, roundToMinutes),
|
||||
};
|
||||
}
|
||||
|
||||
export function secondsToReadable(secs: number): string {
|
||||
var days = Math.floor(secs / (3600 * 24));
|
||||
var hours = Math.floor((secs % (3600 * 24)) / 3600);
|
||||
var minutes = Math.floor((secs % 3600) / 60);
|
||||
var seconds = Math.floor(secs % 60);
|
||||
export function secondsToReadable(
|
||||
secs: number,
|
||||
roundToMinutes: boolean = false,
|
||||
): string {
|
||||
const totalSeconds = roundToMinutes ? Math.round(secs / 60) * 60 : secs;
|
||||
|
||||
var days = Math.floor(totalSeconds / (3600 * 24));
|
||||
var hours = Math.floor((totalSeconds % (3600 * 24)) / 3600);
|
||||
var minutes = Math.floor((totalSeconds % 3600) / 60);
|
||||
var seconds = Math.floor(totalSeconds % 60);
|
||||
|
||||
var dayDisplay = days > 0 ? days + (days === 1 ? " day, " : " days, ") : "";
|
||||
var hourDisplay =
|
||||
@@ -56,3 +62,9 @@ export function secondsToReadable(secs: number): string {
|
||||
"",
|
||||
);
|
||||
}
|
||||
|
||||
function meterToCentimeters(length: string): number {
|
||||
const meters = parseFloat(length);
|
||||
const centimeters = Math.round(meters * 100);
|
||||
return centimeters;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user