Add native media and network integrations
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||
import { createUnifiDiscoveryDescriptor } from '../../ts/integrations/unifi/index.js';
|
||||
|
||||
tap.test('matches UniFi mDNS, SSDP, manual, and discovery records', async () => {
|
||||
const descriptor = createUnifiDiscoveryDescriptor();
|
||||
const mdnsMatcher = descriptor.getMatchers()[0];
|
||||
const ssdpMatcher = descriptor.getMatchers()[1];
|
||||
const manualMatcher = descriptor.getMatchers()[2];
|
||||
const discoveryMatcher = descriptor.getMatchers()[3];
|
||||
|
||||
const mdnsResult = await mdnsMatcher.matches({
|
||||
type: '_unifi._tcp.local.',
|
||||
name: 'UniFi Network._unifi._tcp.local.',
|
||||
host: 'unifi.local',
|
||||
txt: {
|
||||
mac: 'b4fbe4123456',
|
||||
model: 'UniFi Dream Machine',
|
||||
controller_uuid: 'controller-1',
|
||||
},
|
||||
}, {});
|
||||
const ssdpResult = await ssdpMatcher.matches({
|
||||
location: 'https://192.168.1.1:443/description.xml',
|
||||
manufacturer: 'Ubiquiti Networks',
|
||||
modelDescription: 'UniFi Dream Machine',
|
||||
usn: 'uuid:udm-1',
|
||||
}, {});
|
||||
const manualResult = await manualMatcher.matches({
|
||||
host: '192.168.1.2',
|
||||
site: 'default',
|
||||
model: 'UniFi Network',
|
||||
}, {});
|
||||
const discoveryResult = await discoveryMatcher.matches({
|
||||
source_ip: '192.168.1.1',
|
||||
hw_addr: 'B4:FB:E4:12:34:56',
|
||||
services: { network: true },
|
||||
}, {});
|
||||
|
||||
expect(mdnsResult.matched).toBeTrue();
|
||||
expect(mdnsResult.normalizedDeviceId).toEqual('controller-1');
|
||||
expect(ssdpResult.matched).toBeTrue();
|
||||
expect(ssdpResult.candidate?.host).toEqual('192.168.1.1');
|
||||
expect(manualResult.matched).toBeTrue();
|
||||
expect(manualResult.candidate?.port).toEqual(443);
|
||||
expect(discoveryResult.normalizedDeviceId).toEqual('b4:fb:e4:12:34:56');
|
||||
});
|
||||
|
||||
tap.test('validates UniFi candidates', async () => {
|
||||
const validator = createUnifiDiscoveryDescriptor().getValidators()[0];
|
||||
const result = await validator.validate({
|
||||
source: 'ssdp',
|
||||
host: '192.168.1.1',
|
||||
manufacturer: 'Ubiquiti Networks',
|
||||
model: 'UniFi Dream Machine SE',
|
||||
}, {});
|
||||
expect(result.matched).toBeTrue();
|
||||
expect(result.confidence).toEqual('high');
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
@@ -0,0 +1,113 @@
|
||||
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||
import { UnifiMapper, type IUnifiSnapshot } from '../../ts/integrations/unifi/index.js';
|
||||
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
|
||||
const snapshot: IUnifiSnapshot = {
|
||||
connected: true,
|
||||
host: '192.168.1.1',
|
||||
port: 443,
|
||||
site: 'default',
|
||||
controller: {
|
||||
id: 'controller-1',
|
||||
name: 'UniFi Network',
|
||||
version: '9.0.0',
|
||||
connected: true,
|
||||
},
|
||||
sites: [{ id: 'site-1', name: 'default', description: 'Default' }],
|
||||
clients: [
|
||||
{
|
||||
mac: 'aa:bb:cc:dd:ee:ff',
|
||||
name: 'Kitchen Phone',
|
||||
ip: '192.168.1.55',
|
||||
essid: 'Guest WiFi',
|
||||
is_wired: false,
|
||||
blocked: false,
|
||||
last_seen: now,
|
||||
ap_mac: 'b4:fb:e4:12:34:56',
|
||||
'rx_bytes-r': 2000000,
|
||||
'tx_bytes-r': 1000000,
|
||||
rssi: -55,
|
||||
},
|
||||
],
|
||||
devices: [
|
||||
{
|
||||
mac: 'b4:fb:e4:12:34:56',
|
||||
name: 'Switch 24',
|
||||
model: 'USW-24-PoE',
|
||||
version: '6.6.1',
|
||||
ip: '192.168.1.10',
|
||||
state: 1,
|
||||
uptime: 3600,
|
||||
general_temperature: 42,
|
||||
'system-stats': { cpu: '12.5', mem: '31.1' },
|
||||
port_table: [
|
||||
{
|
||||
deviceMac: 'b4:fb:e4:12:34:56',
|
||||
port_idx: 1,
|
||||
name: 'Office AP',
|
||||
enable: true,
|
||||
up: true,
|
||||
port_poe: true,
|
||||
poe_mode: 'auto',
|
||||
poe_power: '7.2',
|
||||
speed: 1000,
|
||||
'rx_bytes-r': 1024,
|
||||
'tx_bytes-r': 2048,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
wlans: [
|
||||
{
|
||||
_id: 'wlan-1',
|
||||
name: 'Guest WiFi',
|
||||
enabled: true,
|
||||
security: 'wpapsk',
|
||||
is_guest: true,
|
||||
},
|
||||
],
|
||||
ports: [],
|
||||
events: [],
|
||||
};
|
||||
|
||||
tap.test('maps UniFi clients, devices, WLANs, and ports', async () => {
|
||||
const devices = UnifiMapper.toDevices(snapshot);
|
||||
const entities = UnifiMapper.toEntities(snapshot);
|
||||
|
||||
expect(devices.some((deviceArg) => deviceArg.id === 'unifi.client.aa_bb_cc_dd_ee_ff')).toBeTrue();
|
||||
expect(devices.some((deviceArg) => deviceArg.id === 'unifi.device.b4_fb_e4_12_34_56')).toBeTrue();
|
||||
expect(devices.some((deviceArg) => deviceArg.id === 'unifi.wlan.wlan_1')).toBeTrue();
|
||||
expect(entities.find((entityArg) => entityArg.id === 'binary_sensor.kitchen_phone_connected')?.state).toEqual('on');
|
||||
expect(entities.find((entityArg) => entityArg.id === 'switch.guest_wifi')?.state).toEqual('on');
|
||||
expect(entities.find((entityArg) => entityArg.id === 'switch.switch_24_office_ap_poe')?.state).toEqual('on');
|
||||
expect(entities.find((entityArg) => entityArg.id === 'sensor.switch_24_office_ap_poe_power')?.state).toEqual(7.2);
|
||||
});
|
||||
|
||||
tap.test('maps services to UniFi commands', async () => {
|
||||
const blockCommand = UnifiMapper.commandForService(snapshot, {
|
||||
domain: 'unifi',
|
||||
service: 'block_client',
|
||||
target: {},
|
||||
data: { mac: 'aa:bb:cc:dd:ee:ff' },
|
||||
});
|
||||
const wlanCommand = UnifiMapper.commandForService(snapshot, {
|
||||
domain: 'switch',
|
||||
service: 'turn_off',
|
||||
target: { entityId: 'switch.guest_wifi' },
|
||||
});
|
||||
const poeCommand = UnifiMapper.commandForService(snapshot, {
|
||||
domain: 'switch',
|
||||
service: 'turn_off',
|
||||
target: { entityId: 'switch.switch_24_office_ap_poe' },
|
||||
});
|
||||
|
||||
expect(blockCommand?.type).toEqual('blockClient');
|
||||
expect(blockCommand?.block).toBeTrue();
|
||||
expect(wlanCommand?.type).toEqual('setWlanEnabled');
|
||||
expect(wlanCommand?.enabled).toBeFalse();
|
||||
expect(poeCommand?.type).toEqual('setPoePortEnabled');
|
||||
expect(poeCommand?.portIdx).toEqual('1');
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
Reference in New Issue
Block a user