import { getCollection } from "./collection.js" ; import { HassEntities , StateChangedEvent , UnsubscribeFunc } from "./types.js" ; import { Connection } from "./connection.js" ; import { Store } from "./store.js" ; import { getStates } from "./commands.js" ; function processEvent ( store , event ) { const state = store . state ; if ( state === undefined ) return ; const { entity_id , new_state } = event . data ; if ( new_state ) { store . setState ( { [ new_state . entity_id ] : new_state } ) ; } else { const newEntities = { ... state } ; delete newEntities [ entity_id ] ; store . setState ( newEntities , true ) ; } } async function fetchEntities ( conn ) { const states = await getStates ( conn ) ; const entities = { } ; for ( let i = 0 ; i < states . length ; i ++ ) { const state = states [ i ] ; entities [ state . entity_id ] = state ; } return entities ; } const subscribeUpdates = ( conn , store ) => conn . subscribeEvents < StateChangedEvent > ( ( ev ) => processEvent ( store , ev as StateChangedEvent ) , "state_changed" ) ; export const entitiesColl = ( conn ) => getCollection ( conn , "_ent" , fetchEntities , subscribeUpdates ) ; export const subscribeEntities = ( conn , onChange ) : UnsubscribeFunc => entitiesColl ( conn ) . subscribe ( onChange ) ;