UNPKG

node-red-contrib-home-assistant-websocket

Version:
59 lines (58 loc) 2.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAreaNameByEntityId = getAreaNameByEntityId; exports.getRegistryData = getRegistryData; const globals_1 = require("../globals"); function getAreaNameByEntityId(entityId, areas, devices, entities) { const areaId = getAreaIdByEntityId(entityId, areas, devices, entities); return getAreaNameByAreaId(areaId, areas); } function getAreaNameByAreaId(areaId, areas) { if (areaId && (areas === null || areas === void 0 ? void 0 : areas.length)) { const area = areas.find((a) => a.area_id === areaId); if (area) { return area.name; } } return globals_1.RED._('ha-device.ui.no_area'); } function getAreaIdByEntityId(entityId, areas, devices, entities) { const entity = getEntityById(entityId, entities); if (entity === null || entity === void 0 ? void 0 : entity.area_id) { return entity.area_id; } if (areas === null || areas === void 0 ? void 0 : areas.length) { const area = areas.find((area) => { const device = getDeviceByEntityId(entityId, devices, entities); return (device === null || device === void 0 ? void 0 : device.area_id) === area.area_id; }); if (area) { return area.area_id; } } return null; } function getDeviceByEntityId(entityId, devices, entities) { const entity = getEntityById(entityId, entities); if ((entity === null || entity === void 0 ? void 0 : entity.device_id) && (devices === null || devices === void 0 ? void 0 : devices.length)) { const device = devices.find((device) => entity.device_id === device.id); if (device) { return device; } } return null; } function getEntityById(entityId, entities) { var _a; if (entityId && entities) { return (_a = entities.find((e) => e.entity_id === entityId)) !== null && _a !== void 0 ? _a : null; } return null; } function getRegistryData(HassWS) { return { areas: HassWS.getAreas(), devices: HassWS.getDevices(), entities: HassWS.getEntities(), }; }