Files
integrations/test/emulated_kasa/test.emulated_kasa.node.ts
T

75 lines
3.4 KiB
TypeScript

import { expect, tap } from '@git.zone/tstest/tapbundle';
import { EmulatedKasaClient, EmulatedKasaConfigFlow, EmulatedKasaIntegration, EmulatedKasaMapper, HomeAssistantEmulatedKasaIntegration, createEmulatedKasaDiscoveryDescriptor, emulatedKasaProfile, type IEmulatedKasaSnapshot } from '../../ts/integrations/emulated_kasa/index.js';
const rawData = {
entities: {
'switch.coffee_maker': {
name: 'Coffee Maker',
unique_id: 'switch.kasa.coffee_maker',
state: 'on',
power: 42.5,
},
'sensor.fridge_power': {
name: 'Fridge Power',
domain: 'sensor',
state: '18.2',
},
},
};
tap.test('matches manual Emulated Kasa candidates and creates config flow output', async () => {
const descriptor = createEmulatedKasaDiscoveryDescriptor();
const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'emulated_kasa-manual-match');
const result = await matcher!.matches({ host: 'kasa-emulator.local', name: 'Emulated Kasa', metadata: { rawData } }, {});
expect(result.matched).toBeTrue();
expect(result.candidate?.integrationDomain).toEqual('emulated_kasa');
const validation = await descriptor.getValidators()[0].validate(result.candidate!, {});
expect(validation.matched).toBeTrue();
const done = await (await new EmulatedKasaConfigFlow().start(result.candidate!, {})).submit!({});
expect(done.kind).toEqual('done');
expect(done.config?.host).toEqual('kasa-emulator.local');
expect(done.config?.port).toEqual(9999);
});
tap.test('maps Emulated Kasa raw snapshots to devices and entities', async () => {
const client = new EmulatedKasaClient({ name: 'Kasa Bridge', rawData });
const snapshot = await client.getSnapshot();
const devices = EmulatedKasaMapper.toDevices(snapshot);
const entities = EmulatedKasaMapper.toEntities(snapshot);
expect(snapshot.online).toBeTrue();
expect(snapshot.source).toEqual('manual');
expect(devices[0].integrationDomain).toEqual('emulated_kasa');
expect(devices[0].manufacturer).toEqual('Home Assistant');
expect(entities.length).toEqual(3);
expect(entities.some((entityArg) => entityArg.platform === 'sensor' && entityArg.state === 42.5)).toBeTrue();
expect(entities.some((entityArg) => entityArg.platform === 'binary_sensor' && entityArg.state === true)).toBeTrue();
});
tap.test('exposes Emulated Kasa read-only runtime, HA alias, and unsupported control', async () => {
const integration = new EmulatedKasaIntegration();
const alias = new HomeAssistantEmulatedKasaIntegration();
expect(alias.domain).toEqual('emulated_kasa');
expect(integration.status).toEqual('read-only-runtime');
expect(emulatedKasaProfile.metadata.configFlow).toEqual(false);
expect(emulatedKasaProfile.metadata.requirements).toEqual(['sense-energy==0.14.1']);
const runtime = await integration.setup({ name: 'Kasa Runtime', rawData }, {});
const status = await runtime.callService!({ domain: 'emulated_kasa', service: 'status', target: {} });
const snapshot = status.data as IEmulatedKasaSnapshot;
expect(status.success).toBeTrue();
expect(snapshot.online).toBeTrue();
expect((await runtime.devices())[0].name).toEqual('Kasa Runtime');
const command = await runtime.callService!({ domain: 'emulated_kasa', service: 'turn_on', target: {} });
expect(command.success).toBeFalse();
expect(command.error?.includes('requires an injected client.execute() or commandExecutor')).toBeTrue();
await runtime.destroy();
});
export default tap.start();