node-red-contrib-home-assistant-websocket
Version:
Node-RED integration with Home Assistant through websocket and REST API
51 lines (50 loc) • 3.13 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.subscribeAreaRegistry = subscribeAreaRegistry;
exports.subscribeDeviceRegistry = subscribeDeviceRegistry;
exports.subscribeEntityRegistry = subscribeEntityRegistry;
exports.subscribeFloorRegistry = subscribeFloorRegistry;
exports.subscribeLabelRegistry = subscribeLabelRegistry;
const home_assistant_js_websocket_1 = require("home-assistant-js-websocket");
const lodash_1 = require("lodash");
function subscribeAreaRegistry(conn, cb) {
const fetchAreaRegistry = (conn) => conn.sendMessagePromise({
type: 'config/area_registry/list',
});
const subscribeUpdates = (conn, store) => conn.subscribeEvents((0, lodash_1.throttle)(() => fetchAreaRegistry(conn).then((areas) => store.setState(areas, true)), 500), 'area_registry_updated');
const collection = (0, home_assistant_js_websocket_1.getCollection)(conn, '_areas', fetchAreaRegistry, subscribeUpdates);
collection.subscribe(cb);
return collection;
}
function subscribeDeviceRegistry(conn, cb) {
const fetchDeviceRegistry = (conn) => conn.sendMessagePromise({
type: 'config/device_registry/list',
});
const subscribeUpdates = (conn, store) => conn.subscribeEvents((0, lodash_1.throttle)(() => fetchDeviceRegistry(conn).then((devices) => store.setState(devices, true)), 500), 'device_registry_updated');
const collection = (0, home_assistant_js_websocket_1.getCollection)(conn, '_devices', fetchDeviceRegistry, subscribeUpdates);
collection.subscribe(cb);
}
function subscribeEntityRegistry(conn, cb) {
const fetchEntityRegistry = (conn) => conn.sendMessagePromise({
type: 'config/entity_registry/list',
});
const subscribeUpdates = (conn, store) => conn.subscribeEvents((0, lodash_1.throttle)(() => fetchEntityRegistry(conn).then((devices) => store.setState(devices, true)), 500), 'entity_registry_updated');
const collection = (0, home_assistant_js_websocket_1.getCollection)(conn, '_entity', fetchEntityRegistry, subscribeUpdates);
collection.subscribe(cb);
}
function subscribeFloorRegistry(conn, cb) {
const fetchFloorRegistry = (conn) => conn.sendMessagePromise({
type: 'config/floor_registry/list',
});
const subscribeUpdates = (conn, store) => conn.subscribeEvents((0, lodash_1.throttle)(() => fetchFloorRegistry(conn).then((floors) => store.setState(floors, true)), 500), 'floor_registry_updated');
const collection = (0, home_assistant_js_websocket_1.getCollection)(conn, '_floors', fetchFloorRegistry, subscribeUpdates);
collection.subscribe(cb);
}
function subscribeLabelRegistry(conn, cb) {
const fetchLabelRegistry = (conn) => conn.sendMessagePromise({
type: 'config/label_registry/list',
});
const subscribeUpdates = (conn, store) => conn.subscribeEvents((0, lodash_1.throttle)(() => fetchLabelRegistry(conn).then((labels) => store.setState(labels, true)), 500), 'label_registry_updated');
const collection = (0, home_assistant_js_websocket_1.getCollection)(conn, '_labels', fetchLabelRegistry, subscribeUpdates);
collection.subscribe(cb);
}
;