Add native hub protocol integrations

This commit is contained in:
2026-05-05 14:57:06 +00:00
parent 2823a1c718
commit 1eebd71e7d
102 changed files with 16316 additions and 330 deletions
@@ -0,0 +1,37 @@
import { expect, tap } from '@git.zone/tstest/tapbundle';
import { createTradfriDiscoveryDescriptor } from '../../ts/integrations/tradfri/index.js';
tap.test('matches IKEA TRADFRI HomeKit mDNS records', async () => {
const descriptor = createTradfriDiscoveryDescriptor();
const matcher = descriptor.getMatchers()[0];
const result = await matcher.matches({
type: '_hap._tcp.local.',
name: 'TRADFRI-Gateway._hap._tcp.local.',
host: 'tradfri-gateway.local',
port: 8080,
txt: {
md: 'TRADFRI',
id: 'AA:BB:CC:DD:EE:FF',
},
}, {});
expect(result.matched).toBeTrue();
expect(result.normalizedDeviceId).toEqual('AA:BB:CC:DD:EE:FF');
expect(result.candidate?.host).toEqual('tradfri-gateway.local');
expect(result.candidate?.port).toEqual(5684);
});
tap.test('matches and validates manual host/security-code entries', async () => {
const descriptor = createTradfriDiscoveryDescriptor();
const manualMatcher = descriptor.getMatchers()[1];
const validator = descriptor.getValidators()[0];
const result = await manualMatcher.matches({
host: '192.168.1.23',
securityCode: 'abc123',
}, {});
expect(result.matched).toBeTrue();
expect(result.candidate?.metadata?.securityCode).toEqual('abc123');
const validation = await validator.validate(result.candidate!, {});
expect(validation.matched).toBeTrue();
});
export default tap.start();
+123
View File
@@ -0,0 +1,123 @@
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();