124 lines
4.6 KiB
TypeScript
124 lines
4.6 KiB
TypeScript
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
|
import { TradfriMapper } from '../../ts/integrations/tradfri/index.js';
|
|
import type { ITradfriSnapshot } from '../../ts/integrations/tradfri/index.js';
|
|
|
|
const snapshot: ITradfriSnapshot = {
|
|
host: 'tradfri-gateway.local',
|
|
port: 5684,
|
|
connected: true,
|
|
gateway: {
|
|
id: 'gw-001',
|
|
name: 'Tradfri Gateway',
|
|
firmwareVersion: '1.19.32',
|
|
},
|
|
devices: [
|
|
{
|
|
id: 65537,
|
|
name: 'Kitchen Bulb',
|
|
reachable: 1,
|
|
deviceInfo: {
|
|
manufacturer: 'IKEA of Sweden',
|
|
modelNumber: 'TRADFRI bulb E27 WS opal 980lm',
|
|
firmwareVersion: '2.3.087',
|
|
},
|
|
lightControl: [{ state: 1, dimmer: 127, colorTemp: 370 }],
|
|
},
|
|
{
|
|
id: 65538,
|
|
name: 'Coffee Outlet',
|
|
reachable: true,
|
|
deviceInfo: { manufacturer: 'IKEA of Sweden', modelNumber: 'TRADFRI control outlet' },
|
|
socketControl: [{ state: 0 }],
|
|
},
|
|
{
|
|
id: 65539,
|
|
name: 'Bedroom Blind',
|
|
reachable: true,
|
|
deviceInfo: { manufacturer: 'IKEA of Sweden', modelNumber: 'FYRTUR block-out roller blind', batteryLevel: 88 },
|
|
blindControl: [{ currentCoverPosition: 40 }],
|
|
},
|
|
{
|
|
id: 65540,
|
|
name: 'Hall Motion',
|
|
reachable: true,
|
|
deviceInfo: { manufacturer: 'IKEA of Sweden', modelNumber: 'TRADFRI motion sensor', batteryLevel: 76 },
|
|
sensors: [{ name: 'Motion', deviceClass: 'motion', value: true, binary: true }],
|
|
},
|
|
{
|
|
id: 65541,
|
|
name: 'Air Purifier',
|
|
reachable: true,
|
|
deviceInfo: { manufacturer: 'IKEA of Sweden', modelNumber: 'STARKVIND air purifier' },
|
|
airPurifierControl: [{ mode: 1, fanSpeed: 20, airQuality: 12, filterLifetimeRemaining: 1200 }],
|
|
},
|
|
],
|
|
groups: [
|
|
{
|
|
id: 131073,
|
|
name: 'Kitchen Group',
|
|
state: 1,
|
|
dimmer: 200,
|
|
memberIds: [65537, 65538],
|
|
},
|
|
],
|
|
};
|
|
|
|
tap.test('maps Tradfri snapshot to canonical devices and entities', async () => {
|
|
const devices = TradfriMapper.toDevices(snapshot);
|
|
const entities = TradfriMapper.toEntities(snapshot);
|
|
expect(devices.some((deviceArg) => deviceArg.id === 'tradfri.gateway.gw_001')).toBeTrue();
|
|
expect(devices.some((deviceArg) => deviceArg.id === 'tradfri.device.gw_001.65539')).toBeTrue();
|
|
expect(entities.some((entityArg) => entityArg.id === 'light.kitchen_bulb' && entityArg.state === 'on')).toBeTrue();
|
|
expect(entities.some((entityArg) => entityArg.id === 'switch.coffee_outlet' && entityArg.state === 'off')).toBeTrue();
|
|
expect(entities.some((entityArg) => entityArg.id === 'cover.bedroom_blind' && entityArg.state === 60)).toBeTrue();
|
|
expect(entities.some((entityArg) => entityArg.id === 'sensor.bedroom_blind_battery' && entityArg.state === 88)).toBeTrue();
|
|
expect(entities.some((entityArg) => entityArg.id === 'binary_sensor.hall_motion_motion' && entityArg.state === 'on')).toBeTrue();
|
|
expect(entities.some((entityArg) => entityArg.id === 'fan.air_purifier' && entityArg.state === 'on')).toBeTrue();
|
|
expect(entities.some((entityArg) => entityArg.id === 'light.kitchen_group')).toBeTrue();
|
|
});
|
|
|
|
tap.test('maps service calls to Tradfri command payloads', async () => {
|
|
const lightCommand = TradfriMapper.commandForService(snapshot, {
|
|
domain: 'light',
|
|
service: 'turn_on',
|
|
target: { entityId: 'light.kitchen_bulb' },
|
|
data: { brightness: 200 },
|
|
});
|
|
const coverCommand = TradfriMapper.commandForService(snapshot, {
|
|
domain: 'cover',
|
|
service: 'set_position',
|
|
target: { entityId: 'cover.bedroom_blind' },
|
|
data: { position: 70 },
|
|
});
|
|
const setValueCommand = TradfriMapper.commandForService(snapshot, {
|
|
domain: 'cover',
|
|
service: 'set_value',
|
|
target: { entityId: 'cover.bedroom_blind' },
|
|
data: { value: 25 },
|
|
});
|
|
const fanCommand = TradfriMapper.commandForService(snapshot, {
|
|
domain: 'fan',
|
|
service: 'set_percentage',
|
|
target: { entityId: 'fan.air_purifier' },
|
|
data: { percentage: 50 },
|
|
});
|
|
const openCommand = TradfriMapper.commandForService(snapshot, {
|
|
domain: 'cover',
|
|
service: 'open_cover',
|
|
target: { entityId: 'cover.bedroom_blind' },
|
|
});
|
|
const closeCommand = TradfriMapper.commandForService(snapshot, {
|
|
domain: 'cover',
|
|
service: 'close_cover',
|
|
target: { entityId: 'cover.bedroom_blind' },
|
|
});
|
|
expect(lightCommand?.coap.payload?.['3311']).toEqual([{ '5850': 1, '5851': 200 }]);
|
|
expect(coverCommand?.payload.rawPosition).toEqual(30);
|
|
expect(setValueCommand?.payload.position).toEqual(25);
|
|
expect(fanCommand?.payload.mode).toEqual(26);
|
|
expect(openCommand?.payload.rawPosition).toEqual(0);
|
|
expect(closeCommand?.payload.rawPosition).toEqual(100);
|
|
});
|
|
|
|
export default tap.start();
|