- Added index.ts for connecting to Home Assistant and subscribing to entities. - Implemented function to calculate sleep time from sensor data. - Created package.json with dependencies for TypeScript and Home Assistant WebSocket. - Configured tsconfig.json for TypeScript with strict settings and ESNext features.
25 lines
No EOL
788 B
TypeScript
25 lines
No EOL
788 B
TypeScript
import {
|
|
Auth,
|
|
createConnection,
|
|
subscribeEntities,
|
|
createLongLivedTokenAuth,
|
|
} from "home-assistant-js-websocket";
|
|
|
|
const auth = createLongLivedTokenAuth(
|
|
"http://172.16.206.123:8123",
|
|
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiI5MGYyNDk3MDVkMTQ0ZTNjODQ0MDZlMWFkMjQ5NTgwZSIsImlhdCI6MTc0NzExNzM0NiwiZXhwIjoyMDYyNDc3MzQ2fQ.zbbyX-RsgtNV8TOIboc0Kk1YZvWMi5LQMwrf9rkv_SM",
|
|
);
|
|
|
|
const connection = await createConnection({ auth });
|
|
|
|
subscribeEntities(connection, (entities) => {
|
|
const fatherSleep = getTimeAsleep(parseFloat(entities["sensor.pixel_9_pro_xl_sleep_segment"]?.state || "0"));
|
|
});
|
|
|
|
const getTimeAsleep = (ms: number) => {
|
|
|
|
const minutes = ((ms / 1000 / 60) % 60);
|
|
const hours = ((ms / 1000 / 60 / 60) % 24);
|
|
|
|
return { minutes, hours };
|
|
}; |