UNPKG

matterbridge-hass

Version:
66 lines (65 loc) 3.22 kB
import { onOffMountedSwitch, onOffOutlet } from 'matterbridge'; import { OnOff } from 'matterbridge/matter/clusters'; import { CYAN, db } from 'node-ansi-logger'; import { getDomain } from './helpers.js'; export function addHelperEntity(platform, mutableDevice, entity, state, individualOrSplitEntity) { const endpointName = entity.entity_id; const domain = getDomain(entity.entity_id); platform.log.debug(`- helper domain "${domain}" platform "${entity.platform}" endpoint "${endpointName}" for entity ${CYAN}${entity.entity_id}${db}`); if (domain === 'automation') { if (individualOrSplitEntity) { mutableDevice.setComposedType(`Hass Automation`); mutableDevice.setConfigUrl(`${platform.config.host.replace('ws://', 'http://').replace('wss://', 'https://')}/config/automation/dashboard`); } } else if (domain === 'scene') { if (individualOrSplitEntity) { mutableDevice.setComposedType(`Hass Scene`); mutableDevice.setConfigUrl(`${platform.config.host.replace('ws://', 'http://').replace('wss://', 'https://')}/config/scene/dashboard`); } } else if (domain === 'script') { if (individualOrSplitEntity) { mutableDevice.setComposedType(`Hass Script`); mutableDevice.setConfigUrl(`${platform.config.host.replace('ws://', 'http://').replace('wss://', 'https://')}/config/script/dashboard`); } } else if (domain === 'input_boolean') { if (individualOrSplitEntity) { mutableDevice.setComposedType(`Hass Boolean`); mutableDevice.setConfigUrl(`${platform.config.host.replace('ws://', 'http://').replace('wss://', 'https://')}/config/helpers`); } } else if (domain === 'input_button') { if (individualOrSplitEntity) { mutableDevice.setComposedType(`Hass Button`); mutableDevice.setConfigUrl(`${platform.config.host.replace('ws://', 'http://').replace('wss://', 'https://')}/config/helpers`); } } else { return undefined; } mutableDevice.addDeviceTypes(endpointName, onOffMountedSwitch, onOffOutlet); mutableDevice.addCommandHandler(endpointName, 'on', async (data) => { if (domain === 'automation') { await platform.ha.callService(domain, 'trigger', entity.entity_id); } else if (domain === 'input_button') { await platform.ha.callService(domain, 'press', entity.entity_id); } else { await platform.ha.callService(domain, 'turn_on', entity.entity_id); } if (domain !== 'input_boolean') { setTimeout(() => { void data.endpoint.setAttribute(OnOff.Cluster, 'onOff', false, data.endpoint.log).catch(() => { }); }, 500).unref(); } }); mutableDevice.addCommandHandler(endpointName, 'off', async () => { if (domain === 'input_boolean') await platform.ha.callService(domain, 'turn_off', entity.entity_id); }); platform.log.debug(`+ helper domain "${domain}" platform "${entity.platform}" endpoint "${endpointName}" for entity ${CYAN}${entity.entity_id}${db}`); return endpointName; }