feat: initialize project with Home Assistant integration
- 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.
This commit is contained in:
parent
8f79c438b5
commit
b73dfc84dd
6 changed files with 3926 additions and 0 deletions
25
index.ts
Normal file
25
index.ts
Normal file
|
@ -0,0 +1,25 @@
|
|||
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 };
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue