Add native local NAS and network service integrations

This commit is contained in:
2026-05-05 19:37:20 +00:00
parent a144ef687c
commit ae901a3308
69 changed files with 13245 additions and 183 deletions
@@ -0,0 +1,59 @@
import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HomeAssistantSynologyDsmIntegration, type ISynologyDsmCommand, type ISynologyDsmConfig } from '../../ts/integrations/synology_dsm/index.js';
const config: ISynologyDsmConfig = {
host: '192.168.1.20',
snapshot: {
connected: true,
system: {
serial: 'SYN123',
name: 'DiskStation',
host: '192.168.1.20',
model: 'DS920+',
versionString: 'DSM 7.2.2-72806',
},
utilization: {},
storage: { volumes: [], disks: [] },
network: {},
cameras: [],
switches: [],
actions: [],
},
};
tap.test('does not fake Synology DSM command success without injected executor', async () => {
const runtime = await new HomeAssistantSynologyDsmIntegration().setup(config, {});
const result = await runtime.callService!({ domain: 'synology_dsm', service: 'reboot', target: {} });
expect(result.success).toBeFalse();
expect(result.error || '').toContain('not faked');
await runtime.destroy();
});
tap.test('executes explicit Synology DSM commands through injected executor', async () => {
let command: ISynologyDsmCommand | undefined;
const runtime = await new HomeAssistantSynologyDsmIntegration().setup({
...config,
commandExecutor: async (commandArg) => {
command = commandArg;
return { success: true, data: { accepted: true } };
},
}, {});
const result = await runtime.callService!({ domain: 'synology_dsm', service: 'shutdown', target: {} });
expect(result.success).toBeTrue();
expect(command?.type).toEqual('system.action');
expect(command?.action).toEqual('shutdown');
await runtime.destroy();
});
tap.test('reports offline refresh without snapshot, provider, or native client', async () => {
const runtime = await new HomeAssistantSynologyDsmIntegration().setup({ host: '192.168.1.20' }, {});
const result = await runtime.callService!({ domain: 'synology_dsm', service: 'refresh', target: {} });
expect(result.success).toBeFalse();
expect(result.error || '').toContain('nativeClient');
await runtime.destroy();
});
export default tap.start();
@@ -0,0 +1,87 @@
import { expect, tap } from '@git.zone/tstest/tapbundle';
import { SynologyDsmConfigFlow, createSynologyDsmDiscoveryDescriptor, type ISynologyDsmSnapshot } from '../../ts/integrations/synology_dsm/index.js';
const snapshot: ISynologyDsmSnapshot = {
connected: true,
system: {
serial: 'SYN123',
name: 'DiskStation',
host: '192.168.1.20',
port: 5001,
ssl: true,
model: 'DS920+',
},
utilization: {},
storage: { volumes: [], disks: [] },
network: {},
cameras: [],
switches: [],
actions: [],
};
tap.test('matches and validates manual Synology DSM entries', async () => {
const descriptor = createSynologyDsmDiscoveryDescriptor();
const matcher = descriptor.getMatchers()[0];
const result = await matcher.matches({ host: '192.168.1.20', name: 'NAS' }, {});
expect(result.matched).toBeTrue();
expect(result.candidate?.integrationDomain).toEqual('synology_dsm');
expect(result.candidate?.port).toEqual(5001);
const validation = await descriptor.getValidators()[0].validate(result.candidate!, {});
expect(validation.matched).toBeTrue();
expect(validation.normalizedDeviceId).toEqual('192.168.1.20:5001');
});
tap.test('matches Synology HTTP, SSDP, and mDNS local candidates', async () => {
const descriptor = createSynologyDsmDiscoveryDescriptor();
const httpResult = await descriptor.getMatchers()[1].matches({ url: 'https://diskstation.local:5001/webapi/query.cgi' }, {});
const ssdpResult = await descriptor.getMatchers()[2].matches({
location: 'http://192.168.1.20:5000/description.xml',
manufacturer: 'Synology',
upnp: {
friendlyName: 'DiskStation (DS920+)',
modelName: 'DS920+',
manufacturer: 'Synology',
serialNumber: 'AABBCCDDEEFF',
},
}, {});
const mdnsResult = await descriptor.getMatchers()[3].matches({
type: '_http._tcp.local.',
name: 'DiskStation._http._tcp.local.',
host: 'diskstation.local',
properties: {
vendor: 'synology',
mac_address: 'AA:BB:CC:DD:EE:FF',
},
}, {});
expect(httpResult.matched).toBeTrue();
expect(httpResult.candidate?.host).toEqual('diskstation.local');
expect(ssdpResult.matched).toBeTrue();
expect(ssdpResult.normalizedDeviceId).toEqual('aa:bb:cc:dd:ee:ff');
expect(mdnsResult.matched).toBeTrue();
expect(mdnsResult.normalizedDeviceId).toEqual('aa:bb:cc:dd:ee:ff');
});
tap.test('creates config flow output from discovery and snapshot JSON', async () => {
const step = await new SynologyDsmConfigFlow().start({ source: 'manual', host: '192.168.1.20', id: 'SYN123', name: 'DiskStation' }, {});
const done = await step.submit!({ username: 'admin', password: 'secret', snapshotJson: JSON.stringify(snapshot), snapshotQuality: '2' });
expect(done.kind).toEqual('done');
expect(done.config?.host).toEqual('192.168.1.20');
expect(done.config?.port).toEqual(5001);
expect(done.config?.snapshot?.system.serial).toEqual('SYN123');
expect(done.config?.snapshotQuality).toEqual(2);
});
tap.test('rejects candidates without Synology DSM hints or usable data', async () => {
const descriptor = createSynologyDsmDiscoveryDescriptor();
const httpResult = await descriptor.getMatchers()[1].matches({ url: 'http://example.local/status' }, {});
expect(httpResult.matched).toBeFalse();
const validation = await descriptor.getValidators()[0].validate({ source: 'manual', name: 'Synology DSM' }, {});
expect(validation.matched).toBeFalse();
});
export default tap.start();
@@ -0,0 +1,114 @@
import { expect, tap } from '@git.zone/tstest/tapbundle';
import { SynologyDsmMapper, type ISynologyDsmSnapshot } from '../../ts/integrations/synology_dsm/index.js';
const snapshot: ISynologyDsmSnapshot = {
connected: true,
updatedAt: '2026-01-01T00:00:00.000Z',
system: {
serial: 'SYN123',
name: 'DiskStation',
hostname: 'diskstation',
host: '192.168.1.20',
port: 5001,
ssl: true,
model: 'DS920+',
versionString: 'DSM 7.2.2-72806',
temperature: 42,
uptimeSeconds: 3600,
macs: ['AA:BB:CC:DD:EE:FF'],
},
utilization: {
cpuUserLoad: 12,
cpuSystemLoad: 5,
cpuTotalLoad: 17,
cpu5MinLoad: 23,
memoryRealUsage: 64,
memorySize: 8_589_934_592,
networkUp: 1024,
networkDown: 2048,
},
storage: {
volumes: [
{ id: 'volume_1', name: 'Volume 1', status: 'normal', sizeTotal: 4_000, sizeUsed: 2_000, percentageUsed: 50, diskTempAvg: 38, diskTempMax: 41, deviceType: 'shr' },
],
disks: [
{ id: 'disk_1', name: 'Drive 1', vendor: 'Seagate', model: 'IronWolf', status: 'normal', smartStatus: 'normal', temperature: 36, exceedBadSectorThreshold: false, belowRemainLifeThreshold: false },
],
},
network: {
hostname: 'diskstation',
macs: ['AA:BB:CC:DD:EE:FF'],
uploadRate: 1024,
downloadRate: 2048,
},
cameras: [
{ id: '1', name: 'Front Door', model: 'BC500', enabled: true, recording: true, motionDetectionEnabled: true, rtsp: 'rtsp://nas/camera/1' },
],
update: {
installedVersion: 'DSM 7.2.2-72806',
latestVersion: 'DSM 7.3-73000',
updateAvailable: true,
releaseUrl: 'http://update.synology.com/autoupdate/whatsnew.php?model=DS920%2B&update_version=73000',
},
switches: [
{ key: 'home_mode', name: 'Home mode', enabled: true, type: 'home_mode' },
],
security: {
status: 'safe',
statusByCheck: { malware: 'safe' },
},
actions: [],
};
tap.test('maps Synology DSM system, storage, network, camera, switch, and update snapshot data', async () => {
const normalized = SynologyDsmMapper.toSnapshot({ snapshot });
const devices = SynologyDsmMapper.toDevices(normalized);
const entities = SynologyDsmMapper.toEntities(normalized);
expect(devices.some((deviceArg) => deviceArg.id === 'synology_dsm.nas.syn123')).toBeTrue();
expect(devices.some((deviceArg) => deviceArg.id === 'synology_dsm.volume.syn123.volume_1')).toBeTrue();
expect(devices.some((deviceArg) => deviceArg.id === 'synology_dsm.disk.syn123.disk_1')).toBeTrue();
expect(devices.some((deviceArg) => deviceArg.id === 'synology_dsm.camera.syn123.1')).toBeTrue();
expect(entities.find((entityArg) => entityArg.attributes?.nativeKey === 'cpu_user_load')?.state).toEqual(12);
expect(entities.find((entityArg) => entityArg.attributes?.nativeKey === 'cpu_5min_load')?.state).toEqual(0.23);
expect(entities.find((entityArg) => entityArg.attributes?.nativeKey === 'volume_percentage_used')?.state).toEqual(50);
expect(entities.find((entityArg) => entityArg.attributes?.nativeKey === 'disk_temp')?.state).toEqual(36);
expect(entities.find((entityArg) => entityArg.attributes?.nativeKey === 'network_down')?.state).toEqual(2048);
expect(entities.find((entityArg) => entityArg.id === 'binary_sensor.front_door_motion_detection')?.state).toEqual('on');
expect(entities.find((entityArg) => entityArg.id === 'switch.diskstation_surveillance_station_home_mode')?.state).toEqual('on');
expect(entities.find((entityArg) => entityArg.id === 'update.diskstation_dsm_update')?.attributes?.latestVersion).toEqual('DSM 7.3-73000');
});
tap.test('models represented Synology DSM commands without executing them', async () => {
const normalized = SynologyDsmMapper.toSnapshot({ snapshot });
const rebootCommand = SynologyDsmMapper.commandForService(normalized, {
domain: 'synology_dsm',
service: 'reboot',
target: {},
});
const homeModeCommand = SynologyDsmMapper.commandForService(normalized, {
domain: 'switch',
service: 'turn_off',
target: { entityId: 'switch.diskstation_surveillance_station_home_mode' },
});
const shutdownCommand = SynologyDsmMapper.commandForService(normalized, {
domain: 'button',
service: 'press',
target: { entityId: 'button.diskstation_shutdown' },
});
const cameraCommand = SynologyDsmMapper.commandForService(normalized, {
domain: 'camera',
service: 'disable_motion_detection',
target: { deviceId: 'synology_dsm.camera.syn123.1' },
});
expect(rebootCommand?.type).toEqual('system.action');
expect(rebootCommand?.action).toEqual('reboot');
expect(homeModeCommand?.type).toEqual('switch.set');
expect(homeModeCommand?.payload?.enabled).toBeFalse();
expect(shutdownCommand?.action).toEqual('shutdown');
expect(cameraCommand?.type).toEqual('camera.action');
expect(cameraCommand?.cameraId).toEqual('1');
});
export default tap.start();