Add native local device integrations
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||
import { AndroidIpWebcamClient } from '../../ts/integrations/android_ip_webcam/index.js';
|
||||
|
||||
tap.test('fetches live snapshots and only reports command success after Ok response', async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
const requests: string[] = [];
|
||||
globalThis.fetch = (async (inputArg: RequestInfo | URL) => {
|
||||
const url = typeof inputArg === 'string' ? inputArg : inputArg instanceof URL ? inputArg.toString() : inputArg.url;
|
||||
requests.push(url);
|
||||
if (url.endsWith('/status.json?show_avail=1')) {
|
||||
return new Response(JSON.stringify({
|
||||
curvals: { torch: 'off', motion_detect: 'on' },
|
||||
avail: { torch: ['on', 'off'] },
|
||||
audio_connections: 1,
|
||||
video_connections: 0,
|
||||
}), { headers: { 'content-type': 'application/json' } });
|
||||
}
|
||||
if (url.endsWith('/sensors.json')) {
|
||||
return new Response(JSON.stringify({
|
||||
battery_level: { unit: '%', data: [[ [87, 123] ]] },
|
||||
motion_active: { data: [[ [1, 123] ]] },
|
||||
}), { headers: { 'content-type': 'application/json' } });
|
||||
}
|
||||
if (url.endsWith('/enabletorch')) {
|
||||
return new Response('Ok');
|
||||
}
|
||||
return new Response('Not Found', { status: 404 });
|
||||
}) as typeof fetch;
|
||||
|
||||
try {
|
||||
const client = new AndroidIpWebcamClient({ host: '192.168.1.20', port: 8080 });
|
||||
const snapshot = await client.getSnapshot();
|
||||
expect(snapshot.connected).toBeTrue();
|
||||
expect(snapshot.sensors.find((sensorArg) => sensorArg.key === 'battery_level')?.value).toEqual(87);
|
||||
expect(snapshot.binarySensors[0].isOn).toBeTrue();
|
||||
expect(snapshot.switches.find((switchArg) => switchArg.key === 'torch')?.isOn).toEqual(false);
|
||||
|
||||
const result = await client.execute({ type: 'torch', service: 'set_torch', activate: true });
|
||||
expect(result).toEqual({ ok: true, key: 'torch', value: true });
|
||||
expect(requests.some((requestArg) => requestArg.endsWith('/enabletorch'))).toBeTrue();
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('does not pretend live commands succeeded without a live endpoint', async () => {
|
||||
const client = new AndroidIpWebcamClient({});
|
||||
let error = '';
|
||||
try {
|
||||
await client.execute({ type: 'torch', service: 'set_torch', activate: true });
|
||||
} catch (errorArg) {
|
||||
error = errorArg instanceof Error ? errorArg.message : String(errorArg);
|
||||
}
|
||||
expect(error.includes('requires config.host or config.url')).toBeTrue();
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
@@ -0,0 +1,35 @@
|
||||
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||
import { AndroidIpWebcamConfigFlow, createAndroidIpWebcamDiscoveryDescriptor } from '../../ts/integrations/android_ip_webcam/index.js';
|
||||
|
||||
tap.test('matches manual Android IP Webcam URL entries', async () => {
|
||||
const descriptor = createAndroidIpWebcamDiscoveryDescriptor();
|
||||
const matcher = descriptor.getMatchers()[0];
|
||||
const match = await matcher.matches({ url: 'http://192.168.1.20:8080', name: 'Kitchen Phone' }, {});
|
||||
|
||||
expect(match.matched).toBeTrue();
|
||||
expect(match.candidate?.integrationDomain).toEqual('android_ip_webcam');
|
||||
expect(match.candidate?.host).toEqual('192.168.1.20');
|
||||
expect(match.candidate?.port).toEqual(8080);
|
||||
expect(match.candidate?.metadata?.url).toEqual('http://192.168.1.20:8080');
|
||||
|
||||
const validation = await descriptor.getValidators()[0].validate(match.candidate!, {});
|
||||
expect(validation.matched).toBeTrue();
|
||||
});
|
||||
|
||||
tap.test('matches manual Android IP Webcam host entries and configures flow', async () => {
|
||||
const descriptor = createAndroidIpWebcamDiscoveryDescriptor();
|
||||
const matcher = descriptor.getMatchers()[0];
|
||||
const match = await matcher.matches({ host: '192.168.1.21', name: 'Desk Phone' }, {});
|
||||
expect(match.matched).toBeTrue();
|
||||
expect(match.candidate?.port).toEqual(8080);
|
||||
|
||||
const flow = new AndroidIpWebcamConfigFlow();
|
||||
const step = await flow.start(match.candidate!, {});
|
||||
const done = await step.submit!({ username: 'user', password: 'pass' });
|
||||
expect(done.kind).toEqual('done');
|
||||
expect(done.config?.host).toEqual('192.168.1.21');
|
||||
expect(done.config?.port).toEqual(8080);
|
||||
expect(done.config?.username).toEqual('user');
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
@@ -0,0 +1,85 @@
|
||||
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||
import { AndroidIpWebcamMapper, type IAndroidIpWebcamSnapshot } from '../../ts/integrations/android_ip_webcam/index.js';
|
||||
|
||||
const snapshot: IAndroidIpWebcamSnapshot = {
|
||||
deviceInfo: {
|
||||
id: 'kitchen-phone',
|
||||
name: 'Kitchen Phone',
|
||||
manufacturer: 'Android IP Webcam',
|
||||
host: '192.168.1.20',
|
||||
port: 8080,
|
||||
protocol: 'http',
|
||||
online: true,
|
||||
},
|
||||
camera: {
|
||||
id: 'camera',
|
||||
name: 'Kitchen Phone Camera',
|
||||
mjpegUrl: 'http://192.168.1.20:8080/video',
|
||||
imageUrl: 'http://192.168.1.20:8080/shot.jpg',
|
||||
rtspUrl: 'rtsp://192.168.1.20:8080/h264_aac.sdp',
|
||||
supportedFeatures: ['stream'],
|
||||
available: true,
|
||||
},
|
||||
sensors: [
|
||||
{ key: 'audio_connections', name: 'Audio connections', value: 1, stateClass: 'total', entityCategory: 'diagnostic', available: true },
|
||||
{ key: 'battery_level', name: 'Battery level', value: 87, unit: '%', deviceClass: 'battery', stateClass: 'measurement', entityCategory: 'diagnostic', available: true },
|
||||
],
|
||||
binarySensors: [{ key: 'motion_active', name: 'Motion active', isOn: true, deviceClass: 'motion', available: true }],
|
||||
switches: [
|
||||
{ key: 'torch', name: 'Torch', isOn: false, command: 'torch', entityCategory: 'config', available: true },
|
||||
{ key: 'motion_detect', name: 'Motion detection', isOn: true, command: 'setting', entityCategory: 'config', available: true },
|
||||
],
|
||||
statusData: { audio_connections: 1, curvals: { torch: 'off', motion_detect: 'on' } },
|
||||
sensorData: { battery_level: { unit: '%', data: [[ [87, 123] ]] }, motion_active: { data: [[ [1, 123] ]] } },
|
||||
currentSettings: { torch: false, motion_detect: true },
|
||||
enabledSensors: ['battery_level', 'motion_active'],
|
||||
enabledSettings: ['torch', 'motion_detect'],
|
||||
availableSettings: {},
|
||||
connected: true,
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
};
|
||||
|
||||
tap.test('maps Android IP Webcam camera, sensors, binary sensor, and switches', async () => {
|
||||
const devices = AndroidIpWebcamMapper.toDevices(snapshot);
|
||||
const entities = AndroidIpWebcamMapper.toEntities(snapshot);
|
||||
|
||||
expect(devices.length).toEqual(1);
|
||||
expect(devices[0].features.some((featureArg) => featureArg.capability === 'camera')).toBeTrue();
|
||||
expect(entities.find((entityArg) => entityArg.id === 'camera.kitchen_phone_camera')?.attributes?.mjpegUrl).toEqual('http://192.168.1.20:8080/video');
|
||||
expect(entities.find((entityArg) => entityArg.id === 'sensor.battery_level')?.state).toEqual(87);
|
||||
expect(entities.find((entityArg) => entityArg.id === 'binary_sensor.motion_active')?.state).toEqual('on');
|
||||
expect(entities.find((entityArg) => entityArg.id === 'switch.torch')?.state).toEqual('off');
|
||||
});
|
||||
|
||||
tap.test('models camera and setting services as explicit commands', async () => {
|
||||
const streamCommand = AndroidIpWebcamMapper.commandForService(snapshot, {
|
||||
domain: 'camera',
|
||||
service: 'stream_source',
|
||||
target: { entityId: 'camera.kitchen_phone_camera' },
|
||||
});
|
||||
const snapshotCommand = AndroidIpWebcamMapper.commandForService(snapshot, {
|
||||
domain: 'camera',
|
||||
service: 'snapshot',
|
||||
target: { entityId: 'camera.kitchen_phone_camera' },
|
||||
});
|
||||
const torchCommand = AndroidIpWebcamMapper.commandForService(snapshot, {
|
||||
domain: 'switch',
|
||||
service: 'turn_on',
|
||||
target: { entityId: 'switch.torch' },
|
||||
});
|
||||
const zoomCommand = AndroidIpWebcamMapper.commandForService(snapshot, {
|
||||
domain: 'android_ip_webcam',
|
||||
service: 'set_zoom',
|
||||
target: {},
|
||||
data: { zoom: '42' },
|
||||
});
|
||||
|
||||
expect(streamCommand?.type).toEqual('stream_source');
|
||||
expect(snapshotCommand?.type).toEqual('snapshot_image');
|
||||
expect(torchCommand?.type).toEqual('torch');
|
||||
expect(torchCommand?.activate).toBeTrue();
|
||||
expect(zoomCommand?.type).toEqual('set_zoom');
|
||||
expect(zoomCommand?.zoom).toEqual(42);
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
Reference in New Issue
Block a user