@heathbar/home-assistant-rxjs
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

home-assistant-rxjs

A simple service that exposes Home Assistant events as an RxJs stream (with TypeScript support).

Usage

Subscribe to all changes of all entities

const hass = new HomeAssistant('http://192.168.0.101:8123');

hass.entities$.subscribe(entities => console.log(entities));
hass.config$.subscribe(entities => console.log(entities));
hass.services$.subscribe(entities => console.log(entities));

callService

Call any Home Assistant service

await hass.callService('light', 'turn_on', { entity_id: 'light.my_light', brightness_pct: 50, color_name: 'blue' });

await hass.callService('media_player', 'media_play_pause', { entity_id: 'media_player.my_music_player'});

await hass.callService('script', 'party_time');

selectEntity

Filters out all events except for the specified entity

hass.entities$.pipe(
    selectEntity('sensor.temperature')
).subscribe(temperatureEntity => console.log(temperatureEntity));

// Emits HassEntity instances 

{
    attributes: { 
        device_class: "temperature"
        friendly_name: "Outdoor Temperature"
        icon: "mdi:thermometer"
        unit_of_measurement: "°F"
    },
    context: { ... },
    entity_id: 'sensor.temperature',
    last_changed: "2021-09-25T05:18:10.850272+00:00"
    last_updated: "2021-09-25T05:18:10.850272+00:00",
    state: '47.4'
}

selectEntityState

Filters out all events except for the specified entity and emit only the current state of the given entity

hass.entities$.pipe(
    selectEntityState('sensor.temperature')
).subscribe(temperatureState => console.log(temperatureState));

// Emits strings

'47.4'

Additional Information

This library was designed with the browser in mind, although it would not be much work to make it work in node.

This library relies on the official home-assistant-js-websocket library to provide OAuth authorization to your Home Assistant instance.

On first launch, the library will forward the browser to the specified Home Assistant host to initiate an OAuth session. On success, the library will store the auth token it receives in sessionStorage.

autoConnect

By default, the library will initiate a connection when you create a new HomeAssistant instance. This can be disabled as follows

const hass = new HomeAssistant('http://192.168.0.101:8123', { autoConnect: false });

// and later...

await hass.connect()

tokenKey

By default, the library will store its auth token in session storage under the key hass-token. This can be changed as follows

const hass = new HomeAssistant('http://192.168.0.101:8123', { tokenKey: 'some-other-key' });

Readme

Keywords

none

Package Sidebar

Install

npm i @heathbar/home-assistant-rxjs

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

11.4 kB

Total Files

4

Last publish

Collaborators

  • heathbar