matterbridge-hass
Version:
Matterbridge hass plugin
68 lines (67 loc) • 3.54 kB
JavaScript
import fs from 'node:fs';
import path from 'node:path';
import { isHidden, isIndividualEntity, isSplitEntity } from './helpers.js';
function getLongNameSuffix(name) {
return (name ?? '').length > 32 ? ' LONGNAME' : '';
}
function getFilterValue(name, id) {
return name ? `${name} >>> ${id}` : 'None';
}
export function createReport(platform) {
const areaId = Array.from(platform.ha.hassAreas.values()).find((area) => area.name === platform.config.filterByArea)?.area_id;
const labelId = Array.from(platform.ha.hassLabels.values()).find((label) => label.name === platform.config.filterByLabel)?.label_id;
const entities = Array.from(platform.ha.hassEntities.values());
const lines = [
'Home Assistant Devices and Entities Report',
'',
`Filter by area: ${getFilterValue(platform.config.filterByArea, areaId)}`,
'',
`Filter by label: ${getFilterValue(platform.config.filterByLabel, labelId)}`,
'',
'Device Entities',
'',
];
for (const device of platform.ha.hassDevices.values()) {
const deviceName = device.name_by_user ?? device.name;
lines.push(`Device: "${deviceName}"` +
`${getLongNameSuffix(deviceName)}` +
`${device.entry_type === 'service' ? ' SERVICE' : ''}` +
`${areaId && device.area_id === areaId ? ' AREA' : ''}` +
`${labelId && device.labels?.includes(labelId) ? ' LABEL' : ''}`);
for (const entity of entities.filter((entry) => entry.device_id === device.id)) {
const state = platform.ha.hassStates.get(entity.entity_id);
const entityName = entity.name ?? entity.original_name;
const reportName = state?.attributes?.friendly_name ?? entity.name ?? entity.original_name;
lines.push(`- Entity: ${entity.entity_id} "${state?.attributes?.friendly_name}" - "${entityName}"` +
`${getLongNameSuffix(reportName)}` +
`${areaId && entity.area_id === areaId ? ' AREA' : ''}` +
`${labelId && entity.labels?.includes(labelId) ? ' LABEL' : ''}` +
`${isHidden(entity) ? ' HIDDEN' : ''}` +
`${isSplitEntity(platform, entity) ? ' SPLIT' : ''}`);
}
}
lines.push('', 'Individual Entities', '');
for (const entity of entities.filter(isIndividualEntity)) {
const state = platform.ha.hassStates.get(entity.entity_id);
const entityName = entity.name ?? entity.original_name;
const reportName = state?.attributes?.friendly_name ?? entity.name ?? entity.original_name;
lines.push(`Individual Entity: ${entity.entity_id} "${state?.attributes?.friendly_name}" - "${entityName}"` +
`${getLongNameSuffix(reportName)}` +
`${areaId && entity.area_id === areaId ? ' AREA' : ''}` +
`${labelId && entity.labels?.includes(labelId) ? ' LABEL' : ''}` +
`${isHidden(entity) ? ' HIDDEN' : ''}`);
}
return `${lines.join('\n')}\n`;
}
export async function writeReport(platform) {
const pluginDirectory = platform.matterbridge.matterbridgePluginDirectory;
const reportPath = path.join(pluginDirectory, 'matterbridge-hass', 'report.log');
try {
await fs.promises.writeFile(reportPath, createReport(platform));
platform.log.debug(`Home Assistant report successfully written to ${reportPath}`);
}
catch (error) {
platform.log.error(`Error writing Home Assistant report to ${reportPath}: ${error}`);
}
return reportPath;
}