TypeScript/RXJS Node.js api for HomeAssistant
npm install tsnode-homeassistantIt's simple reactive way of access and control HomeAssistant using the
websocket api.
This is very first versions of the library and some functionality is missing...
Please, do not hasitate to create an issues if you will find any bug or missing functionality.
``bash`
$ npm i --save tsnode-homeassistant
Or use seed project
https://github.com/troyanskiy/tsnode-homeassistant-seed
`typescript
import { HomeAssistant, IHAEvent } from 'tsnode-homeassistant';
import { IButtonEventData } from './data';
import { CONFIG } from './config';
import { map } from 'rxjs/operators';
// Create the HomeAssistant instance and connect to the HA server
const ha = new HomeAssistant(CONFIG);
ha
.events // Events object
.select('deconz_event') // Select HA events
.pipe(
map(($event: IHAEvent
)
.subscribe(data => { // Subscribe to events
console.log('The event', data);
});
ha
.states
.onChange
.subscribe(state => console.log(state));
`
connectionStatus$: Observable
connectionStatus: HAConnectionStatus
ready$: Observable
haVersion: string
events: HomeAssistantEvents
Details below
service: HomeAssistantService
Details below
entities: HomeAssistantEntities
Details below
Available as object on HomeAssistant instance
`typescript`
*.events.select(eventType: string): Observable
`typescript
export interface IHAEvent
data: T; // event data
event_type: string; // event type or name
time_fired: string; // time of event fire
origin: string; // event origin
context?: IHAEventContext;
}
export interface IHAEventContext {
id: string;
parent_id: string | null;
user_id: string | null;
}
`
#### Example
Above
Available as object on HomeAssistant instance
`typescript`
*.service.call(domain: HADomain, service: HAServiceActionType, serviceDataOrEntity?: any): Observable
Will return an observable with service execution result
#### Examples
`typescript`
ha
.service
.call(
HADomain.Light,
HAServiceType.TurnOn,
'light.EntityId1'
)
`typescript`
ha
.service
.call(
HADomain.Light,
HAServiceType.TurnOn,
'EntityId1'
)
`typescript`
ha
.service
.call(
HADomain.Switch,
HAServiceType.TurnOff,
['EntityId1', 'switch.EntityId2']
)
`typescript`
ha
.service
.call(
HADomain.Switch,
HAServiceType.Toggle,
{
entity_id: ['EntityId1', 'EntityId2']
}
)
`typescript`
toggle(domain: HADomain, entities: string[] | string, force?: boolean): Observable
Will return an observable with service execution result
If
* force === true it will call TurnOn serviceforce === false
* it will call TurnOff serviceforce
* is missing will call Toggle service
#### Example
`typescript`
ha
.service
.toggle(
HADomain.Switch,
['EntityId1', 'EntityId2']
)
Available as object on HomeAssistant instance
All the entities/states are loaded and saved in the memory every time
when Node.js instance is connected to the HomeAssistant server
`typescript`
*.entities.onChange: Subject
#### Examples
`typescript`
ha
.entities
.onChange
.pipe(
filter(entity => entity.id === 'MyEntityId')
)
.subscribe(entity => {
// do something with new state
console.log('Received new state of entity', entity);
})
Will call HA to get all the states, updates them in the memory and returns as observable resolution
`typescript`
*.entities.fetchEntities(): Observable
Will return entity from memory or null if not found
`typescript``
*.entities.getState(entityId: string): HAEntity | null