diff --git a/lib/commands.ts b/lib/commands.ts index 43baeec7..c5e3f9f8 100644 --- a/lib/commands.ts +++ b/lib/commands.ts @@ -1,7 +1,7 @@ import { Connection } from "./connection.js"; import * as messages from "./messages.js"; import { - HassEntity, + HassEntityState, HassServices, HassConfig, HassUser, @@ -9,7 +9,7 @@ import { } from "./types.js"; export const getStates = (connection: Connection) => - connection.sendMessagePromise(messages.states()); + connection.sendMessagePromise(messages.states()); export const getServices = (connection: Connection) => connection.sendMessagePromise(messages.services()); diff --git a/lib/index.ts b/lib/index.ts index 38366e3c..4c2b154d 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -7,7 +7,7 @@ export * from "./collection.js"; export * from "./connection.js"; export * from "./config.js"; export * from "./services.js"; -export * from "./entities.js"; +export * from "./states.js"; export * from "./errors.js"; export * from "./socket.js"; export * from "./types.js"; diff --git a/lib/entities.ts b/lib/states.ts similarity index 82% rename from lib/entities.ts rename to lib/states.ts index 26f872ff..de6772fc 100644 --- a/lib/entities.ts +++ b/lib/states.ts @@ -1,7 +1,7 @@ import { getCollection } from "./collection.js"; import { Context, - HassEntities, + HassEntityStates, StateChangedEvent, UnsubscribeFunc, } from "./types.js"; @@ -44,7 +44,7 @@ interface StatesUpdates { c: Record; } -function processEvent(store: Store, updates: StatesUpdates) { +function processEvent(store: Store, updates: StatesUpdates) { const state = { ...store.state }; if (updates.a) { @@ -127,13 +127,13 @@ function processEvent(store: Store, updates: StatesUpdates) { store.setState(state, true); } -const subscribeUpdates = (conn: Connection, store: Store) => +const subscribeUpdates = (conn: Connection, store: Store) => conn.subscribeMessage((ev) => processEvent(store, ev), { type: "subscribe_entities", }); function legacyProcessEvent( - store: Store, + store: Store, event: StateChangedEvent ) { const state = store.state; @@ -143,34 +143,44 @@ function legacyProcessEvent( if (new_state) { store.setState({ [new_state.entity_id]: new_state }); } else { - const newEntities = { ...state }; - delete newEntities[entity_id]; - store.setState(newEntities, true); + const newStates = { ...state }; + delete newStates[entity_id]; + store.setState(newStates, true); } } -async function legacyFetchEntities(conn: Connection): Promise { +async function legacyFetchEntityStates( + conn: Connection +): Promise { const states = await getStates(conn); - const entities: HassEntities = {}; + const entityStates: HassEntityStates = {}; for (let i = 0; i < states.length; i++) { const state = states[i]; - entities[state.entity_id] = state; + entityStates[state.entity_id] = state; } - return entities; + return entityStates; } -const legacySubscribeUpdates = (conn: Connection, store: Store) => +const legacySubscribeUpdates = ( + conn: Connection, + store: Store +) => conn.subscribeEvents( (ev) => legacyProcessEvent(store, ev as StateChangedEvent), "state_changed" ); -export const entitiesColl = (conn: Connection) => +export const entityStatesColl = (conn: Connection) => atLeastHaVersion(conn.haVersion, 2022, 4, 0) ? getCollection(conn, "_ent", undefined, subscribeUpdates) - : getCollection(conn, "_ent", legacyFetchEntities, legacySubscribeUpdates); - -export const subscribeEntities = ( + : getCollection( + conn, + "_ent", + legacyFetchEntityStates, + legacySubscribeUpdates + ); + +export const subscribeEntityStates = ( conn: Connection, - onChange: (state: HassEntities) => void -): UnsubscribeFunc => entitiesColl(conn).subscribe(onChange); + onChange: (state: HassEntityStates) => void +): UnsubscribeFunc => entityStatesColl(conn).subscribe(onChange); diff --git a/lib/types.ts b/lib/types.ts index b73d1a4a..70666f64 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -32,8 +32,8 @@ export type StateChangedEvent = HassEventBase & { event_type: "state_changed"; data: { entity_id: string; - new_state: HassEntity | null; - old_state: HassEntity | null; + new_state: HassEntityState | null; + old_state: HassEntityState | null; }; }; @@ -67,16 +67,16 @@ export type HassConfig = { language: string; }; -export type HassEntityBase = { +export type HassEntityStateBase = { entity_id: string; state: string; last_changed: string; last_updated: string; - attributes: HassEntityAttributeBase; + attributes: HassEntityStateAttributeBase; context: Context; }; -export type HassEntityAttributeBase = { +export type HassEntityStateAttributeBase = { friendly_name?: string; unit_of_measurement?: string; icon?: string; @@ -89,11 +89,11 @@ export type HassEntityAttributeBase = { restored?: boolean; }; -export type HassEntity = HassEntityBase & { +export type HassEntityState = HassEntityStateBase & { attributes: { [key: string]: any }; }; -export type HassEntities = { [entity_id: string]: HassEntity }; +export type HassEntityStates = { [entity_id: string]: HassEntityState }; export type HassService = { name?: string; diff --git a/test/entities.spec.ts b/test/states.spec.ts similarity index 90% rename from test/entities.spec.ts rename to test/states.spec.ts index a75f7cc8..83aeab4d 100644 --- a/test/entities.spec.ts +++ b/test/states.spec.ts @@ -1,6 +1,6 @@ import * as assert from "assert"; -import { subscribeEntities } from "../dist/entities"; +import { subscribeEntityStates } from "../dist/states"; import { MockConnection, AwaitableEvent } from "./util"; const MOCK_LIGHT = { @@ -28,7 +28,7 @@ describe("subscribeEntities legacy", () => { it("should load initial entities", async () => { awaitableEvent.prime(); - subscribeEntities(conn, awaitableEvent.set); + subscribeEntityStates(conn, awaitableEvent.set); const entities = await awaitableEvent.wait(); assert.deepStrictEqual(entities, { @@ -38,7 +38,7 @@ describe("subscribeEntities legacy", () => { }); it("should handle state changed with updated state", async () => { - subscribeEntities(conn, awaitableEvent.set); + subscribeEntityStates(conn, awaitableEvent.set); await 0; await 0; @@ -68,7 +68,7 @@ describe("subscribeEntities legacy", () => { }); it("should handle state changed with new state", async () => { - subscribeEntities(conn, awaitableEvent.set); + subscribeEntityStates(conn, awaitableEvent.set); await 0; await 0; @@ -99,7 +99,7 @@ describe("subscribeEntities legacy", () => { }); it("should handle state changed with removed state", async () => { - subscribeEntities(conn, awaitableEvent.set); + subscribeEntityStates(conn, awaitableEvent.set); await 0; await 0;