add desk text sensor and change detection

This commit is contained in:
Darius
2025-10-03 13:46:07 +02:00
parent fd420fa59a
commit 31b9ba105f
5 changed files with 52 additions and 31 deletions

View File

@@ -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;
}