matterbridge-hass
Version:
Matterbridge hass plugin
60 lines (59 loc) • 4.45 kB
JavaScript
import { contactSensor, smokeCoAlarm, waterFreezeDetector, waterLeakDetector } from 'matterbridge';
import { CYAN, db, debugStringify } from 'matterbridge/logger';
import { BooleanState, SmokeCoAlarm } from 'matterbridge/matter/clusters';
import { getClusterNameById } from 'matterbridge/matter/types';
import { isValidString } from 'matterbridge/utils';
import { hassDomainBinarySensorsConverter } from './converters.js';
import { getDomain } from './helpers.js';
export function addBinarySensorEntity(platform, mutableDevice, entity, state) {
let endpointName = undefined;
const domain = getDomain(entity.entity_id);
if (domain !== 'binary_sensor')
return undefined;
if (state.attributes['device_class'] === null || state.attributes['device_class'] === undefined) {
endpointName = entity.entity_id;
platform.log.debug(`+ binary_sensor device ${CYAN}${contactSensor.name}${db} cluster ${CYAN}${getClusterNameById(BooleanState.Cluster.id)}${db}`);
mutableDevice.addDeviceTypes(endpointName, contactSensor);
mutableDevice.addClusterServerIds(endpointName, BooleanState.Cluster.id);
if (isValidString(state.attributes['friendly_name']))
mutableDevice.setFriendlyName(endpointName, state.attributes['friendly_name']);
platform.log.debug(`- state ${debugStringify(state)}`);
platform.log.debug(`= contactSensor device ${CYAN}${entity.entity_id}${db} state ${CYAN}${state.state}${db}`);
mutableDevice.addClusterServerBooleanState(endpointName, state.state === 'on' ? false : true);
return endpointName;
}
hassDomainBinarySensorsConverter
.filter((d) => d.domain === domain && d.withDeviceClass === state.attributes['device_class'])
.forEach((hassDomainBinarySensor) => {
if (hassDomainBinarySensor.endpoint !== undefined) {
endpointName = hassDomainBinarySensor.endpoint;
platform.log.debug(`- binary_sensor domain ${hassDomainBinarySensor.domain} deviceClass ${hassDomainBinarySensor.withDeviceClass} endpoint '${CYAN}${endpointName}${db}' for entity ${CYAN}${entity.entity_id}${db}`);
}
else {
endpointName = entity.entity_id;
}
platform.log.debug(`+ binary_sensor device ${CYAN}${hassDomainBinarySensor.deviceType.name}${db} cluster ${CYAN}${getClusterNameById(hassDomainBinarySensor.clusterId)}${db}`);
mutableDevice.addDeviceTypes(endpointName, hassDomainBinarySensor.deviceType);
mutableDevice.addClusterServerIds(endpointName, hassDomainBinarySensor.clusterId);
if (isValidString(state.attributes['friendly_name']))
mutableDevice.setFriendlyName(endpointName, state.attributes['friendly_name']);
platform.log.debug(`- state ${debugStringify(state)}`);
if (hassDomainBinarySensor.deviceType.code === contactSensor.code) {
platform.log.debug(`= contactSensor device ${CYAN}${entity.entity_id}${db} state ${CYAN}${state.state}${db}`);
mutableDevice.addClusterServerBooleanState(endpointName, state.state === 'on' ? false : true);
}
if (hassDomainBinarySensor.deviceType.code === waterLeakDetector.code || hassDomainBinarySensor.deviceType.code === waterFreezeDetector.code) {
platform.log.debug(`= waterLeakDetector/waterFreezeDetector device ${CYAN}${entity.entity_id}${db} state ${CYAN}${state.state}${db}`);
mutableDevice.addClusterServerBooleanState(endpointName, state.state === 'on' ? true : false);
}
if (state.attributes.device_class === 'smoke' && hassDomainBinarySensor.deviceType.code === smokeCoAlarm.code) {
platform.log.debug(`= smokeCoAlarm SmokeAlarm device ${CYAN}${entity.entity_id}${db} state ${CYAN}${state.state}${db}`);
mutableDevice.addClusterServerSmokeAlarmSmokeCoAlarm(endpointName, state.state === 'on' ? SmokeCoAlarm.AlarmState.Critical : SmokeCoAlarm.AlarmState.Normal);
}
if (state.attributes.device_class === 'carbon_monoxide' && hassDomainBinarySensor.deviceType.code === smokeCoAlarm.code) {
platform.log.debug(`= smokeCoAlarm CoAlarm device ${CYAN}${entity.entity_id}${db} state ${CYAN}${state.state}${db}`);
mutableDevice.addClusterServerCoAlarmSmokeCoAlarm(endpointName, state.state === 'on' ? SmokeCoAlarm.AlarmState.Critical : SmokeCoAlarm.AlarmState.Normal);
}
});
return endpointName;
}