Add native local infrastructure integrations
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||
import { HomeAssistantFritzIntegration, type IFritzCommand, type IFritzConfig } from '../../ts/integrations/fritz/index.js';
|
||||
|
||||
const config: IFritzConfig = {
|
||||
host: '192.168.178.1',
|
||||
snapshot: {
|
||||
connected: true,
|
||||
router: {
|
||||
name: 'Home Fritz',
|
||||
host: '192.168.178.1',
|
||||
serialNumber: 'AABBCCDDEEFF',
|
||||
actions: ['reboot'],
|
||||
},
|
||||
devices: [],
|
||||
interfaces: [],
|
||||
connection: {},
|
||||
sensors: {},
|
||||
wifiNetworks: [],
|
||||
portForwards: [],
|
||||
callDeflections: [],
|
||||
},
|
||||
};
|
||||
|
||||
tap.test('does not fake FRITZ TR-064 command success without injected executor', async () => {
|
||||
const runtime = await new HomeAssistantFritzIntegration().setup(config, {});
|
||||
const result = await runtime.callService!({ domain: 'fritz', service: 'reboot', target: {} });
|
||||
|
||||
expect(result.success).toBeFalse();
|
||||
expect(result.error || '').toInclude('not faked');
|
||||
await runtime.destroy();
|
||||
});
|
||||
|
||||
tap.test('executes explicit FRITZ commands through injected executor', async () => {
|
||||
let command: IFritzCommand | undefined;
|
||||
const runtime = await new HomeAssistantFritzIntegration().setup({
|
||||
...config,
|
||||
commandExecutor: async (commandArg) => {
|
||||
command = commandArg;
|
||||
return { success: true, data: { accepted: true } };
|
||||
},
|
||||
}, {});
|
||||
const result = await runtime.callService!({ domain: 'fritz', service: 'reboot', target: {} });
|
||||
|
||||
expect(result.success).toBeTrue();
|
||||
expect(command?.type).toEqual('router.action');
|
||||
expect(command?.action).toEqual('reboot');
|
||||
await runtime.destroy();
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
@@ -0,0 +1,94 @@
|
||||
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||
import { FritzConfigFlow, createFritzDiscoveryDescriptor } from '../../ts/integrations/fritz/index.js';
|
||||
|
||||
tap.test('matches and validates manual FRITZ entries', async () => {
|
||||
const descriptor = createFritzDiscoveryDescriptor();
|
||||
const matcher = descriptor.getMatchers()[0];
|
||||
const result = await matcher.matches({
|
||||
host: '192.168.178.1',
|
||||
name: 'Home Fritz',
|
||||
model: 'FRITZ!Box 7590',
|
||||
serialNumber: 'AABBCCDDEEFF',
|
||||
}, {});
|
||||
|
||||
expect(result.matched).toBeTrue();
|
||||
expect(result.candidate?.integrationDomain).toEqual('fritz');
|
||||
expect(result.candidate?.port).toEqual(49000);
|
||||
expect(result.normalizedDeviceId).toEqual('AABBCCDDEEFF');
|
||||
|
||||
const validator = descriptor.getValidators()[0];
|
||||
const validation = await validator.validate(result.candidate!, {});
|
||||
expect(validation.matched).toBeTrue();
|
||||
expect(validation.metadata?.liveTr064Implemented).toBeFalse();
|
||||
});
|
||||
|
||||
tap.test('accepts snapshot-only setup and rejects unrelated manual entries', async () => {
|
||||
const descriptor = createFritzDiscoveryDescriptor();
|
||||
const matcher = descriptor.getMatchers()[0];
|
||||
const snapshotResult = await matcher.matches({
|
||||
snapshot: {
|
||||
connected: true,
|
||||
router: { name: 'Snapshot Fritz', serialNumber: 'AABBCCDDEEFF', model: 'FRITZ!Box 7530' },
|
||||
devices: [],
|
||||
interfaces: [],
|
||||
connection: {},
|
||||
sensors: {},
|
||||
wifiNetworks: [],
|
||||
portForwards: [],
|
||||
callDeflections: [],
|
||||
},
|
||||
}, {});
|
||||
const unrelated = await matcher.matches({ name: 'Generic Switch', model: 'GS108' }, {});
|
||||
|
||||
expect(snapshotResult.matched).toBeTrue();
|
||||
expect(snapshotResult.confidence).toEqual('certain');
|
||||
expect(unrelated.matched).toBeFalse();
|
||||
});
|
||||
|
||||
tap.test('matches Home Assistant supported FRITZ SSDP candidates', async () => {
|
||||
const descriptor = createFritzDiscoveryDescriptor();
|
||||
const matcher = descriptor.getMatchers()[1];
|
||||
const result = await matcher.matches({
|
||||
st: 'urn:schemas-upnp-org:device:fritzbox:1',
|
||||
ssdpLocation: 'http://192.168.178.1:49000/rootDesc.xml',
|
||||
upnp: {
|
||||
friendlyName: 'FRITZ!Box 7590',
|
||||
modelName: 'FRITZ!Box 7590',
|
||||
UDN: 'uuid:abcdef01-2345-6789-abcd-ef0123456789',
|
||||
},
|
||||
}, {});
|
||||
|
||||
expect(result.matched).toBeTrue();
|
||||
expect(result.confidence).toEqual('certain');
|
||||
expect(result.candidate?.source).toEqual('ssdp');
|
||||
expect(result.candidate?.host).toEqual('192.168.178.1');
|
||||
expect(result.candidate?.metadata?.upstreamSupportsZeroconf).toBeFalse();
|
||||
});
|
||||
|
||||
tap.test('matches supplied FRITZ zeroconf candidates without claiming upstream zeroconf manifest support', async () => {
|
||||
const descriptor = createFritzDiscoveryDescriptor();
|
||||
const matcher = descriptor.getMatchers()[2];
|
||||
const result = await matcher.matches({
|
||||
host: 'fritz.box',
|
||||
name: 'FRITZ!Box 7530',
|
||||
model: 'FRITZ!Box 7530',
|
||||
serviceType: '_http._tcp.local',
|
||||
}, {});
|
||||
|
||||
expect(result.matched).toBeTrue();
|
||||
expect(result.candidate?.source).toEqual('mdns');
|
||||
expect(result.candidate?.metadata?.upstreamSupportsZeroconf).toBeFalse();
|
||||
});
|
||||
|
||||
tap.test('builds FRITZ config flow without claiming live TR-064 validation', async () => {
|
||||
const flow = new FritzConfigFlow();
|
||||
const step = await flow.start({ source: 'ssdp', host: '192.168.178.1', metadata: { ssl: true } }, {});
|
||||
const done = await step.submit!({ host: '192.168.178.1', ssl: true, username: 'homeassistant', password: 'secret', featureDeviceTracking: true });
|
||||
|
||||
expect(done.kind).toEqual('done');
|
||||
expect(done.config?.port).toEqual(49443);
|
||||
expect(done.config?.ssl).toBeTrue();
|
||||
expect(done.config?.metadata?.liveTr064Implemented).toBeFalse();
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
@@ -0,0 +1,138 @@
|
||||
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||
import { FritzMapper, type IFritzSnapshot } from '../../ts/integrations/fritz/index.js';
|
||||
|
||||
const snapshot: IFritzSnapshot = {
|
||||
connected: true,
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
router: {
|
||||
host: '192.168.178.1',
|
||||
port: 49000,
|
||||
ssl: false,
|
||||
name: 'Home Fritz',
|
||||
model: 'FRITZ!Box 7590',
|
||||
serialNumber: 'AABBCCDDEEFF',
|
||||
macAddress: 'AA:BB:CC:DD:EE:FF',
|
||||
firmware: '8.02',
|
||||
latestFirmware: '8.03',
|
||||
updateAvailable: true,
|
||||
actions: ['reboot', 'reconnect', 'firmware_update'],
|
||||
},
|
||||
devices: [
|
||||
{
|
||||
mac: '11:22:33:44:55:66',
|
||||
name: 'Kitchen Phone',
|
||||
ipAddress: '192.168.178.45',
|
||||
connected: true,
|
||||
connectedTo: 'Repeater',
|
||||
connectionType: 'LAN',
|
||||
ssid: 'Home WiFi',
|
||||
wanAccess: true,
|
||||
},
|
||||
],
|
||||
interfaces: [
|
||||
{
|
||||
name: 'wan',
|
||||
label: 'WAN',
|
||||
connected: true,
|
||||
rxBytes: 2_000_000_000,
|
||||
txBytes: 1_000_000_000,
|
||||
rxRateKbps: 125.5,
|
||||
txRateKbps: 42.5,
|
||||
},
|
||||
],
|
||||
connection: {
|
||||
connection: 'dsl',
|
||||
wanEnabled: true,
|
||||
ipv6Active: true,
|
||||
isConnected: true,
|
||||
isLinked: true,
|
||||
externalIp: '203.0.113.10',
|
||||
externalIpv6: '2001:db8::1',
|
||||
transmissionRate: [42_500, 125_500],
|
||||
maxBitRate: [50_000_000, 250_000_000],
|
||||
maxLinkedBitRate: [60_000_000, 300_000_000],
|
||||
noiseMargin: [80, 90],
|
||||
attenuation: [120, 130],
|
||||
bytesSent: 1_000_000_000,
|
||||
bytesReceived: 2_000_000_000,
|
||||
cpuTemperature: 56,
|
||||
},
|
||||
sensors: {},
|
||||
wifiNetworks: [
|
||||
{ index: 1, switchName: 'Main 2.4Ghz', ssid: 'Home WiFi', enabled: true, band: '2.4Ghz' },
|
||||
],
|
||||
portForwards: [
|
||||
{ index: 0, description: 'SSH', enabled: true, internalClient: '192.168.178.2', internalPort: 22, externalPort: 22, protocol: 'TCP', connectionType: 'WANIPConnection' },
|
||||
],
|
||||
callDeflections: [
|
||||
{ id: 1, enabled: false, type: 'fromNumber', number: '123', deflectionToNumber: '456', mode: 'eImmediately' },
|
||||
],
|
||||
update: {
|
||||
installedVersion: '8.02',
|
||||
latestVersion: '8.03',
|
||||
updateAvailable: true,
|
||||
releaseUrl: 'https://example.invalid/fritzos',
|
||||
},
|
||||
actions: [
|
||||
{ target: 'service', action: 'set_guest_wifi_password' },
|
||||
{ target: 'service', action: 'dial' },
|
||||
],
|
||||
};
|
||||
|
||||
tap.test('maps FRITZ router, tracker equivalents, interfaces, traffic sensors, and controls', async () => {
|
||||
const normalized = FritzMapper.toSnapshot({ snapshot });
|
||||
const devices = FritzMapper.toDevices(normalized);
|
||||
const entities = FritzMapper.toEntities(normalized);
|
||||
|
||||
expect(devices.some((deviceArg) => deviceArg.id === 'fritz.router.aa_bb_cc_dd_ee_ff')).toBeTrue();
|
||||
expect(devices.some((deviceArg) => deviceArg.id === 'fritz.client.11_22_33_44_55_66')).toBeTrue();
|
||||
expect(entities.find((entityArg) => entityArg.id === 'sensor.home_fritz_gb_received')?.state).toEqual(2);
|
||||
expect(entities.find((entityArg) => entityArg.id === 'sensor.home_fritz_download_throughput')?.state).toEqual(125.5);
|
||||
expect(entities.find((entityArg) => entityArg.id === 'sensor.home_fritz_wan_download')?.state).toEqual(2);
|
||||
expect(entities.find((entityArg) => entityArg.id === 'binary_sensor.kitchen_phone_connected')?.attributes?.nativePlatform).toEqual('device_tracker');
|
||||
expect(entities.find((entityArg) => entityArg.id === 'switch.home_fritz_wi_fi_main_2_4ghz')?.state).toEqual('on');
|
||||
expect(entities.find((entityArg) => entityArg.id === 'switch.kitchen_phone_internet_access')?.state).toEqual('on');
|
||||
expect(entities.find((entityArg) => entityArg.id === 'button.kitchen_phone_wake_on_lan')?.available).toBeTrue();
|
||||
expect(entities.find((entityArg) => entityArg.id === 'update.home_fritz_fritz_os')?.attributes?.latestVersion).toEqual('8.03');
|
||||
});
|
||||
|
||||
tap.test('models only represented FRITZ commands safely', async () => {
|
||||
const normalized = FritzMapper.toSnapshot({ snapshot });
|
||||
const rebootCommand = FritzMapper.commandForService(normalized, {
|
||||
domain: 'fritz',
|
||||
service: 'reboot',
|
||||
target: {},
|
||||
});
|
||||
const wifiCommand = FritzMapper.commandForService(normalized, {
|
||||
domain: 'switch',
|
||||
service: 'turn_off',
|
||||
target: { entityId: 'switch.home_fritz_wi_fi_main_2_4ghz' },
|
||||
});
|
||||
const wolCommand = FritzMapper.commandForService(normalized, {
|
||||
domain: 'button',
|
||||
service: 'press',
|
||||
target: { entityId: 'button.kitchen_phone_wake_on_lan' },
|
||||
});
|
||||
const serviceCommand = FritzMapper.commandForService(normalized, {
|
||||
domain: 'fritz',
|
||||
service: 'set_guest_wifi_password',
|
||||
target: {},
|
||||
data: { password: 'safe-passphrase' },
|
||||
});
|
||||
const invalidGuestPassword = FritzMapper.commandForService(normalized, {
|
||||
domain: 'fritz',
|
||||
service: 'set_guest_wifi_password',
|
||||
target: {},
|
||||
data: { password: 'short' },
|
||||
});
|
||||
|
||||
expect(rebootCommand?.type).toEqual('router.action');
|
||||
expect(wifiCommand?.action).toEqual('set_wifi_enabled');
|
||||
expect(wifiCommand?.payload?.enabled).toBeFalse();
|
||||
expect(wolCommand?.type).toEqual('client.action');
|
||||
expect(wolCommand?.mac).toEqual('11:22:33:44:55:66');
|
||||
expect(serviceCommand?.type).toEqual('service.action');
|
||||
expect(invalidGuestPassword).toBeUndefined();
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
Reference in New Issue
Block a user