feat: Add experimental reolink media support (#1694)
* feat: Add experimental rich reolink support. * Formatting fix
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { BrowseMediaMetadata } from '../../../../src/camera-manager/browse-media/types';
|
||||
import { getViewMediaFromBrowseMediaArray } from '../../../../src/camera-manager/browse-media/utils/browse-media-to-view-media';
|
||||
import {
|
||||
MEDIA_CLASS_IMAGE,
|
||||
MEDIA_CLASS_VIDEO,
|
||||
RichBrowseMedia,
|
||||
} from '../../../../src/utils/ha/browse-media/types';
|
||||
import { createBrowseMedia } from './test-utils';
|
||||
|
||||
const createBrowseMediaChildren = (
|
||||
overrides: Partial<RichBrowseMedia<BrowseMediaMetadata>>[],
|
||||
) => {
|
||||
return overrides.map((override) => createBrowseMedia(override));
|
||||
};
|
||||
|
||||
describe('getViewMediaFromBrowseMediaArray', () => {
|
||||
it('should ignore absent metadata', () => {
|
||||
const children = createBrowseMediaChildren([{ _metadata: undefined }]);
|
||||
expect(getViewMediaFromBrowseMediaArray(children)).toEqual([]);
|
||||
});
|
||||
|
||||
it('should ignore unknown media class', () => {
|
||||
const children = createBrowseMediaChildren([{ media_class: 'UNKNOWN' }]);
|
||||
expect(getViewMediaFromBrowseMediaArray(children)).toEqual([]);
|
||||
});
|
||||
|
||||
it('should generate clip view media', () => {
|
||||
const children = createBrowseMediaChildren([{ media_class: MEDIA_CLASS_VIDEO }]);
|
||||
const viewMedia = getViewMediaFromBrowseMediaArray(children);
|
||||
expect(viewMedia).toHaveLength(1);
|
||||
expect(viewMedia?.[0].getMediaType()).toBe('clip');
|
||||
expect(viewMedia?.[0].getID()).toBe('camera.test/2024-11-19 07:23:00');
|
||||
});
|
||||
|
||||
it('should generate snapshot view media', () => {
|
||||
const children = createBrowseMediaChildren([{ media_class: MEDIA_CLASS_IMAGE }]);
|
||||
const viewMedia = getViewMediaFromBrowseMediaArray(children);
|
||||
expect(viewMedia).toHaveLength(1);
|
||||
expect(viewMedia?.[0].getMediaType()).toBe('snapshot');
|
||||
expect(viewMedia?.[0].getID()).toBe('camera.test/2024-11-19 07:23:00');
|
||||
});
|
||||
|
||||
it('should de-duplicate snapshot/clip duplicates', () => {
|
||||
const children = createBrowseMediaChildren([
|
||||
{ media_class: MEDIA_CLASS_IMAGE },
|
||||
{ media_class: MEDIA_CLASS_IMAGE },
|
||||
{ media_class: MEDIA_CLASS_VIDEO },
|
||||
]);
|
||||
const viewMedia = getViewMediaFromBrowseMediaArray(children);
|
||||
expect(viewMedia).toHaveLength(1);
|
||||
expect(viewMedia?.[0].getMediaType()).toBe('clip');
|
||||
expect(viewMedia?.[0].getID()).toBe('camera.test/2024-11-19 07:23:00');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
import { BrowseMediaMetadata } from '../../../../src/camera-manager/browse-media/types';
|
||||
import { RichBrowseMedia } from '../../../../src/utils/ha/browse-media/types';
|
||||
|
||||
export const createBrowseMedia = (
|
||||
media?: Partial<RichBrowseMedia<BrowseMediaMetadata>>,
|
||||
): RichBrowseMedia<BrowseMediaMetadata> => {
|
||||
return {
|
||||
title: 'Test Media',
|
||||
media_class: 'video',
|
||||
media_content_type: 'video/mp4',
|
||||
media_content_id: 'test',
|
||||
can_play: true,
|
||||
can_expand: false,
|
||||
thumbnail: null,
|
||||
children: null,
|
||||
_metadata: {
|
||||
cameraID: 'camera.test',
|
||||
startDate: new Date('2024-11-19T07:23:00'),
|
||||
endDate: new Date('2025-11-19T07:24:00'),
|
||||
},
|
||||
...media,
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,40 @@
|
||||
import { sub } from 'date-fns';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { isMediaWithinDates } from '../../../../src/camera-manager/browse-media/utils/within-dates';
|
||||
import { createBrowseMedia } from './test-utils';
|
||||
|
||||
describe('isMediaWithinDates', () => {
|
||||
const rangeStart = new Date('2024-11-19T07:00:00');
|
||||
const rangeEnd = new Date('2024-11-19T08:00:00');
|
||||
|
||||
it('should never match media without metadata', () => {
|
||||
const media = createBrowseMedia({ _metadata: undefined });
|
||||
expect(isMediaWithinDates(media, rangeStart, rangeEnd)).toBe(false);
|
||||
});
|
||||
|
||||
it('should always match media without start or end date', () => {
|
||||
expect(isMediaWithinDates(createBrowseMedia(), undefined, undefined)).toBe(true);
|
||||
});
|
||||
|
||||
it('should match without a start date in the range', () => {
|
||||
expect(isMediaWithinDates(createBrowseMedia(), undefined, rangeEnd)).toBe(true);
|
||||
});
|
||||
|
||||
it('should match without an end date in the range', () => {
|
||||
expect(isMediaWithinDates(createBrowseMedia(), rangeStart, undefined)).toBe(true);
|
||||
});
|
||||
|
||||
it('should match when ranges overlap', () => {
|
||||
expect(isMediaWithinDates(createBrowseMedia(), rangeStart, rangeEnd)).toBe(true);
|
||||
});
|
||||
|
||||
it('should not match when ranges do not overlap', () => {
|
||||
expect(
|
||||
isMediaWithinDates(
|
||||
createBrowseMedia(),
|
||||
sub(rangeStart, { days: 1 }),
|
||||
sub(rangeEnd, { days: 1 }),
|
||||
),
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -2,7 +2,9 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { mock } from 'vitest-mock-extended';
|
||||
import { Camera } from '../../src/camera-manager/camera.js';
|
||||
import { GenericCameraManagerEngine } from '../../src/camera-manager/generic/engine-generic.js';
|
||||
import { CameraProxyConfig } from '../../src/camera-manager/types.js';
|
||||
import { StateWatcherSubscriptionInterface } from '../../src/card-controller/hass/state-watcher.js';
|
||||
import { ProxyConfig } from '../../src/config/types.js';
|
||||
import {
|
||||
callStateWatcherCallback,
|
||||
createCameraConfig,
|
||||
@@ -137,4 +139,124 @@ describe('Camera', () => {
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe('should get proxy config', () => {
|
||||
it.each([
|
||||
[
|
||||
'when unspecified',
|
||||
{},
|
||||
{
|
||||
dynamic: true,
|
||||
media: false,
|
||||
ssl_verification: true,
|
||||
ssl_ciphers: 'default' as const,
|
||||
},
|
||||
],
|
||||
[
|
||||
'when media set to true',
|
||||
{ media: true },
|
||||
{
|
||||
dynamic: true,
|
||||
media: true,
|
||||
ssl_verification: true,
|
||||
ssl_ciphers: 'default' as const,
|
||||
},
|
||||
],
|
||||
[
|
||||
'when media set to false',
|
||||
{ media: false as const },
|
||||
{
|
||||
dynamic: true,
|
||||
media: false,
|
||||
ssl_verification: true,
|
||||
ssl_ciphers: 'default' as const,
|
||||
},
|
||||
],
|
||||
[
|
||||
'when media set to auto',
|
||||
{ media: 'auto' as const },
|
||||
{
|
||||
dynamic: true,
|
||||
media: false,
|
||||
ssl_verification: true,
|
||||
ssl_ciphers: 'default' as const,
|
||||
},
|
||||
],
|
||||
[
|
||||
'when ssl_verification is set to auto',
|
||||
{ ssl_verification: 'auto' as const },
|
||||
{
|
||||
dynamic: true,
|
||||
media: false,
|
||||
ssl_verification: true,
|
||||
ssl_ciphers: 'default' as const,
|
||||
},
|
||||
],
|
||||
[
|
||||
'when ssl_verification is set to true',
|
||||
{ ssl_verification: true },
|
||||
{
|
||||
dynamic: true,
|
||||
media: false,
|
||||
ssl_verification: true,
|
||||
ssl_ciphers: 'default' as const,
|
||||
},
|
||||
],
|
||||
[
|
||||
'when ssl_verification is set to false',
|
||||
{ ssl_verification: false },
|
||||
{
|
||||
dynamic: true,
|
||||
media: false,
|
||||
ssl_verification: false,
|
||||
ssl_ciphers: 'default' as const,
|
||||
},
|
||||
],
|
||||
[
|
||||
'when ssl_ciphers is set to auto',
|
||||
{ ssl_ciphers: 'auto' as const },
|
||||
{
|
||||
dynamic: true,
|
||||
media: false,
|
||||
ssl_verification: true,
|
||||
ssl_ciphers: 'default' as const,
|
||||
},
|
||||
],
|
||||
[
|
||||
'when ssl_ciphers is set to modern',
|
||||
{ ssl_ciphers: 'modern' as const },
|
||||
{
|
||||
dynamic: true,
|
||||
media: false,
|
||||
ssl_verification: true,
|
||||
ssl_ciphers: 'modern' as const,
|
||||
},
|
||||
],
|
||||
[
|
||||
'when dynamic is set to false',
|
||||
{ dynamic: false },
|
||||
{
|
||||
dynamic: false,
|
||||
media: false,
|
||||
ssl_verification: true,
|
||||
ssl_ciphers: 'default' as const,
|
||||
},
|
||||
],
|
||||
])(
|
||||
'%s',
|
||||
(
|
||||
_name: string,
|
||||
proxyConfig: Partial<ProxyConfig>,
|
||||
expectedResult: CameraProxyConfig,
|
||||
) => {
|
||||
const camera = new Camera(
|
||||
createCameraConfig({
|
||||
proxy: proxyConfig,
|
||||
}),
|
||||
new GenericCameraManagerEngine(mock<StateWatcherSubscriptionInterface>()),
|
||||
);
|
||||
expect(camera.getProxyConfig()).toEqual(expectedResult);
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
createRegistryEntity,
|
||||
createStateEntity,
|
||||
} from '../test-utils';
|
||||
import { ReolinkCameraManagerEngine } from '../../src/camera-manager/reolink/engine-reolink.js';
|
||||
|
||||
vi.mock('../../src/utils/ha/entity-registry');
|
||||
vi.mock('../../src/utils/ha/entity-registry/cache');
|
||||
@@ -70,7 +71,7 @@ describe('getEngineForCamera()', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('should get a motionEye camera', () => {
|
||||
describe('should get a motioneye camera', () => {
|
||||
it('from manually set engine', async () => {
|
||||
const config = createCameraConfig({ engine: 'motioneye' });
|
||||
expect(await createFactory().getEngineForCamera(createHASS(), config)).toBe(
|
||||
@@ -98,6 +99,34 @@ describe('getEngineForCamera()', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('should get a reolink camera', () => {
|
||||
it('from manually set engine', async () => {
|
||||
const config = createCameraConfig({ engine: 'reolink' });
|
||||
expect(await createFactory().getEngineForCamera(createHASS(), config)).toBe(
|
||||
Engine.Reolink,
|
||||
);
|
||||
});
|
||||
|
||||
it('from auto detection', async () => {
|
||||
const config = createCameraConfig({ engine: 'auto', camera_entity: 'camera.foo' });
|
||||
const entityRegistryManager = new EntityRegistryManager(
|
||||
createEntityRegistryCache(),
|
||||
);
|
||||
|
||||
entityRegistryManager.getEntity = vi
|
||||
.fn()
|
||||
.mockResolvedValue(
|
||||
createRegistryEntity({ entity_id: 'camera.foo', platform: 'reolink' }),
|
||||
);
|
||||
|
||||
expect(
|
||||
await createFactory({
|
||||
entityRegistryManager: entityRegistryManager,
|
||||
}).getEngineForCamera(createHASS(), config),
|
||||
).toBe(Engine.Reolink);
|
||||
});
|
||||
});
|
||||
|
||||
describe('should get a generic camera', () => {
|
||||
it('from manually set engine', async () => {
|
||||
const config = createCameraConfig({ engine: 'generic' });
|
||||
@@ -233,4 +262,12 @@ describe('createEngine()', () => {
|
||||
}),
|
||||
).toBeInstanceOf(MotionEyeCameraManagerEngine);
|
||||
});
|
||||
it('should create reolink engine', async () => {
|
||||
expect(
|
||||
await createFactory().createEngine(Engine.Reolink, {
|
||||
stateWatcher: mock<StateWatcherSubscriptionInterface>(),
|
||||
resolvedMediaCache: mock<ResolvedMediaCache>(),
|
||||
}),
|
||||
).toBeInstanceOf(ReolinkCameraManagerEngine);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -18,10 +18,10 @@ import {
|
||||
retainResultSchema,
|
||||
} from '../../../src/camera-manager/frigate/types';
|
||||
import { RecordingSegment } from '../../../src/camera-manager/types';
|
||||
import { homeAssistantWSRequest } from '../../../src/utils/ha';
|
||||
import { homeAssistantWSRequest } from '../../../src/utils/ha/ws-request';
|
||||
import { createFrigateEvent, createHASS } from '../../test-utils';
|
||||
|
||||
vi.mock('../../../src/utils/ha');
|
||||
vi.mock('../../../src/utils/ha/ws-request');
|
||||
|
||||
describe('frigate requests', () => {
|
||||
afterEach(() => {
|
||||
|
||||
@@ -0,0 +1,216 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { mock } from 'vitest-mock-extended';
|
||||
import { CameraManagerEngine } from '../../../src/camera-manager/engine';
|
||||
import { ReolinkCamera } from '../../../src/camera-manager/reolink/camera';
|
||||
import { CameraProxyConfig } from '../../../src/camera-manager/types';
|
||||
import { StateWatcher } from '../../../src/card-controller/hass/state-watcher';
|
||||
import { ProxyConfig } from '../../../src/config/types';
|
||||
import { EntityRegistryManager } from '../../../src/utils/ha/registry/entity';
|
||||
import { createCameraConfig, createHASS, createRegistryEntity } from '../../test-utils';
|
||||
|
||||
describe('ReolinkCamera', () => {
|
||||
describe('should initialize config', () => {
|
||||
describe('should detect channel', () => {
|
||||
it('without a camera_entity', async () => {
|
||||
const config = createCameraConfig();
|
||||
const camera = new ReolinkCamera(config, mock<CameraManagerEngine>());
|
||||
|
||||
expect(
|
||||
async () =>
|
||||
await camera.initialize({
|
||||
hass: createHASS(),
|
||||
entityRegistryManager: mock<EntityRegistryManager>(),
|
||||
stateWatcher: mock<StateWatcher>(),
|
||||
}),
|
||||
).rejects.toThrowError('Could not find camera entity');
|
||||
});
|
||||
|
||||
it('without a unique_id', async () => {
|
||||
const config = createCameraConfig({
|
||||
camera_entity: 'camera.office_reolink',
|
||||
});
|
||||
const camera = new ReolinkCamera(config, mock<CameraManagerEngine>());
|
||||
|
||||
const entityRegistryManager = mock<EntityRegistryManager>();
|
||||
const entity = createRegistryEntity({
|
||||
platform: 'reolink',
|
||||
});
|
||||
entityRegistryManager.getEntity.mockResolvedValue(entity);
|
||||
|
||||
expect(
|
||||
async () =>
|
||||
await camera.initialize({
|
||||
hass: createHASS(),
|
||||
entityRegistryManager: entityRegistryManager,
|
||||
stateWatcher: mock<StateWatcher>(),
|
||||
}),
|
||||
).rejects.toThrowError('Could not initialize Reolink camera');
|
||||
});
|
||||
|
||||
it('without a channel in the unique_id', async () => {
|
||||
const config = createCameraConfig({
|
||||
camera_entity: 'camera.office_reolink',
|
||||
});
|
||||
const camera = new ReolinkCamera(config, mock<CameraManagerEngine>());
|
||||
|
||||
const entityRegistryManager = mock<EntityRegistryManager>();
|
||||
const entity = createRegistryEntity({
|
||||
unique_id: 'invalid',
|
||||
platform: 'reolink',
|
||||
});
|
||||
entityRegistryManager.getEntity.mockResolvedValue(entity);
|
||||
|
||||
expect(
|
||||
async () =>
|
||||
await camera.initialize({
|
||||
hass: createHASS(),
|
||||
entityRegistryManager: entityRegistryManager,
|
||||
stateWatcher: mock<StateWatcher>(),
|
||||
}),
|
||||
).rejects.toThrowError('Could not initialize Reolink camera');
|
||||
});
|
||||
});
|
||||
|
||||
it('successfully with main camera', async () => {
|
||||
const config = createCameraConfig({
|
||||
camera_entity: 'camera.office_reolink',
|
||||
});
|
||||
const camera = new ReolinkCamera(config, mock<CameraManagerEngine>());
|
||||
|
||||
const entityRegistryManager = mock<EntityRegistryManager>();
|
||||
const entity = createRegistryEntity({
|
||||
unique_id: '85270002TS7D4RUP_0_main',
|
||||
platform: 'reolink',
|
||||
});
|
||||
entityRegistryManager.getEntity.mockResolvedValue(entity);
|
||||
|
||||
await camera.initialize({
|
||||
hass: createHASS(),
|
||||
entityRegistryManager: entityRegistryManager,
|
||||
stateWatcher: mock<StateWatcher>(),
|
||||
});
|
||||
|
||||
expect(camera.getChannel()).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('should get proxy config', () => {
|
||||
it.each([
|
||||
[
|
||||
'when unspecified',
|
||||
{},
|
||||
{
|
||||
dynamic: true,
|
||||
media: true,
|
||||
ssl_verification: false,
|
||||
ssl_ciphers: 'intermediate' as const,
|
||||
},
|
||||
],
|
||||
[
|
||||
'when media set to on',
|
||||
{ media: true },
|
||||
{
|
||||
dynamic: true,
|
||||
media: true,
|
||||
ssl_verification: false,
|
||||
ssl_ciphers: 'intermediate' as const,
|
||||
},
|
||||
],
|
||||
[
|
||||
'when media set to off',
|
||||
{ media: false },
|
||||
{
|
||||
dynamic: true,
|
||||
media: false,
|
||||
ssl_verification: false,
|
||||
ssl_ciphers: 'intermediate' as const,
|
||||
},
|
||||
],
|
||||
[
|
||||
'when media set to auto',
|
||||
{ media: 'auto' as const },
|
||||
{
|
||||
dynamic: true,
|
||||
media: true,
|
||||
ssl_verification: false,
|
||||
ssl_ciphers: 'intermediate' as const,
|
||||
},
|
||||
],
|
||||
[
|
||||
'when ssl_verification is set to auto',
|
||||
{ ssl_verification: 'auto' as const },
|
||||
{
|
||||
dynamic: true,
|
||||
media: true,
|
||||
ssl_verification: false,
|
||||
ssl_ciphers: 'intermediate' as const,
|
||||
},
|
||||
],
|
||||
[
|
||||
'when ssl_verification is set to true',
|
||||
{ ssl_verification: true },
|
||||
{
|
||||
dynamic: true,
|
||||
media: true,
|
||||
ssl_verification: true,
|
||||
ssl_ciphers: 'intermediate' as const,
|
||||
},
|
||||
],
|
||||
[
|
||||
'when ssl_verification is set to false',
|
||||
{ ssl_verification: false },
|
||||
{
|
||||
dynamic: true,
|
||||
media: true,
|
||||
ssl_verification: false,
|
||||
ssl_ciphers: 'intermediate' as const,
|
||||
},
|
||||
],
|
||||
[
|
||||
'when ssl_ciphers is set to auto',
|
||||
{ ssl_ciphers: 'auto' as const },
|
||||
{
|
||||
dynamic: true,
|
||||
media: true,
|
||||
ssl_verification: false,
|
||||
ssl_ciphers: 'intermediate' as const,
|
||||
},
|
||||
],
|
||||
[
|
||||
'when ssl_ciphers is set to modern',
|
||||
{ ssl_ciphers: 'modern' as const },
|
||||
{
|
||||
dynamic: true,
|
||||
media: true,
|
||||
ssl_verification: false,
|
||||
ssl_ciphers: 'modern' as const,
|
||||
},
|
||||
],
|
||||
[
|
||||
'when dynamic is set to false',
|
||||
{ dynamic: false },
|
||||
{
|
||||
dynamic: false,
|
||||
media: true,
|
||||
ssl_verification: false,
|
||||
ssl_ciphers: 'intermediate' as const,
|
||||
},
|
||||
],
|
||||
])(
|
||||
'%s',
|
||||
(
|
||||
_name: string,
|
||||
proxyConfig: Partial<ProxyConfig>,
|
||||
expectedResult: CameraProxyConfig,
|
||||
) => {
|
||||
const camera = new ReolinkCamera(
|
||||
createCameraConfig({
|
||||
proxy: proxyConfig,
|
||||
}),
|
||||
mock<CameraManagerEngine>(),
|
||||
);
|
||||
expect(camera.getProxyConfig()).toEqual(expectedResult);
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
File diff suppressed because it is too large
Load Diff
@@ -38,10 +38,19 @@ describe('config defaults', () => {
|
||||
file_pattern: '%H-%M-%S',
|
||||
},
|
||||
},
|
||||
proxy: {
|
||||
dynamic: true,
|
||||
media: 'auto',
|
||||
ssl_verification: 'auto',
|
||||
ssl_ciphers: 'auto',
|
||||
},
|
||||
ptz: {
|
||||
c2r_delay_between_calls_seconds: 0.2,
|
||||
r2c_delay_between_calls_seconds: 0.5,
|
||||
},
|
||||
reolink: {
|
||||
media_resolution: 'low',
|
||||
},
|
||||
triggers: {
|
||||
events: ['events', 'clips', 'snapshots'],
|
||||
entities: [],
|
||||
|
||||
+1
-1
@@ -147,7 +147,7 @@ export const createRegistryEntity = (entity?: Partial<Entity>): Entity => {
|
||||
hidden_by: entity?.hidden_by ?? null,
|
||||
platform: entity?.platform ?? 'platform',
|
||||
translation_key: entity?.translation_key ?? null,
|
||||
unique_id: entity?.unique_id ?? 'unique_id',
|
||||
...(entity?.unique_id && { unique_id: entity?.unique_id }),
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import frigateSVG from '../../src/camera-manager/frigate/assets/frigate.svg';
|
||||
import motioneyeSVG from '../../src/camera-manager/motioneye/assets/motioneye.svg';
|
||||
import reolinkSVG from '../../src/camera-manager/reolink/assets/reolink.svg';
|
||||
import { getCustomIconURL } from '../../src/utils/custom-icons';
|
||||
|
||||
describe('getCustomIconURL', () => {
|
||||
@@ -12,6 +13,10 @@ describe('getCustomIconURL', () => {
|
||||
expect(getCustomIconURL('motioneye')).toBe(motioneyeSVG);
|
||||
});
|
||||
|
||||
it('should return reolink SVG for reolink icon', () => {
|
||||
expect(getCustomIconURL('reolink')).toBe(reolinkSVG);
|
||||
});
|
||||
|
||||
it('should return null for mdi icon', () => {
|
||||
expect(getCustomIconURL('mdi:car')).toBeNull();
|
||||
});
|
||||
|
||||
@@ -5,6 +5,7 @@ import { getLanguage } from '../../src/localize/localize';
|
||||
import { getDiagnostics, getReleaseVersion } from '../../src/utils/diagnostics.js';
|
||||
import { DeviceRegistryManager } from '../../src/utils/ha/registry/device';
|
||||
import { createHASS, createRegistryDevice } from '../test-utils';
|
||||
import { homeAssistantWSRequest } from '../../src/utils/ha/ws-request';
|
||||
|
||||
vi.mock('../../package.json', () => ({
|
||||
default: {
|
||||
@@ -16,6 +17,7 @@ vi.mock('../../package.json', () => ({
|
||||
vi.mock('../../src/utils/ha');
|
||||
vi.mock('../../src/localize/localize.js');
|
||||
vi.mock('../../src/utils/ha/registry/device/index.js');
|
||||
vi.mock('../../src/utils/ha/ws-request.js');
|
||||
|
||||
describe('getReleaseVersion', () => {
|
||||
it('should get release version', () => {
|
||||
@@ -30,6 +32,8 @@ describe('getDiagnostics', () => {
|
||||
hass.config = { version: '2023.9.0' } as HassConfig;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(now);
|
||||
|
||||
@@ -62,6 +66,11 @@ describe('getDiagnostics', () => {
|
||||
}),
|
||||
]);
|
||||
|
||||
vi.mocked(homeAssistantWSRequest).mockResolvedValue({
|
||||
domain: 'domain',
|
||||
version: '0.0.1',
|
||||
});
|
||||
|
||||
expect(
|
||||
await getDiagnostics(hass, deviceRegistryManager, {
|
||||
cameras: [{ camera_entity: 'camera.office' }],
|
||||
@@ -72,15 +81,33 @@ describe('getDiagnostics', () => {
|
||||
config: {
|
||||
cameras: [{ camera_entity: 'camera.office' }],
|
||||
},
|
||||
frigate_versions: {
|
||||
ac4e79d258449a83bc0cf6d47a021c46: '4.0.0/0.13.0-aded314',
|
||||
b03e70c659d58ae2ce7f2dc76fed2929: '4.0.0/0.13.0-aded314',
|
||||
},
|
||||
git: {
|
||||
build_date: 'Tue, 19 Sep 2023 04:59:27 GMT',
|
||||
commit_date: 'Wed, 6 Sep 2023 21:27:28 -0700',
|
||||
hash: 'g4cf13b1',
|
||||
},
|
||||
integrations: {
|
||||
frigate: {
|
||||
detected: true,
|
||||
devices: {
|
||||
ac4e79d258449a83bc0cf6d47a021c46: '4.0.0/0.13.0-aded314',
|
||||
b03e70c659d58ae2ce7f2dc76fed2929: '4.0.0/0.13.0-aded314',
|
||||
},
|
||||
version: '0.0.1',
|
||||
},
|
||||
hass_web_proxy: {
|
||||
detected: true,
|
||||
version: '0.0.1',
|
||||
},
|
||||
motioneye: {
|
||||
detected: true,
|
||||
version: '0.0.1',
|
||||
},
|
||||
reolink: {
|
||||
detected: true,
|
||||
version: '0.0.1',
|
||||
},
|
||||
},
|
||||
date: now,
|
||||
lang: 'en',
|
||||
ha_version: '2023.9.0',
|
||||
@@ -118,6 +145,20 @@ describe('getDiagnostics', () => {
|
||||
commit_date: 'Wed, 6 Sep 2023 21:27:28 -0700',
|
||||
hash: 'g4cf13b1',
|
||||
},
|
||||
integrations: {
|
||||
frigate: {
|
||||
detected: false,
|
||||
},
|
||||
hass_web_proxy: {
|
||||
detected: false,
|
||||
},
|
||||
motioneye: {
|
||||
detected: false,
|
||||
},
|
||||
reolink: {
|
||||
detected: false,
|
||||
},
|
||||
},
|
||||
date: now,
|
||||
lang: 'en',
|
||||
timezone: expect.anything(),
|
||||
@@ -136,6 +177,20 @@ describe('getDiagnostics', () => {
|
||||
commit_date: 'Wed, 6 Sep 2023 21:27:28 -0700',
|
||||
hash: 'g4cf13b1',
|
||||
},
|
||||
integrations: {
|
||||
frigate: {
|
||||
detected: false,
|
||||
},
|
||||
hass_web_proxy: {
|
||||
detected: false,
|
||||
},
|
||||
motioneye: {
|
||||
detected: false,
|
||||
},
|
||||
reolink: {
|
||||
detected: false,
|
||||
},
|
||||
},
|
||||
ha_version: '2023.9.0',
|
||||
date: now,
|
||||
lang: 'en',
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { HomeAssistant } from '@dermotduffy/custom-card-helpers';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import {
|
||||
canonicalizeHAURL,
|
||||
getEntityIcon,
|
||||
hasHAConnectionStateChanged,
|
||||
isHARelativeURL,
|
||||
} from '../../../src/utils/ha/index.js';
|
||||
import { createHASS, createStateEntity } from '../../test-utils.js';
|
||||
|
||||
@@ -67,3 +69,32 @@ describe('getEntityIcon', () => {
|
||||
expect(getEntityIcon(createHASS(), 'camera.test')).toBe('mdi:video');
|
||||
});
|
||||
});
|
||||
|
||||
describe('isHARelativeURL', () => {
|
||||
it('should return true when URL is HA relative', () => {
|
||||
expect(isHARelativeURL('/api/foo')).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should return false when URL is not HA relative', () => {
|
||||
expect(isHARelativeURL('http://localhost/api/foo')).toBeFalsy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('canonicalizeHAURL', () => {
|
||||
it('should return canonicalized HA url', () => {
|
||||
const hass = createHASS();
|
||||
vi.mocked(hass.hassUrl).mockReturnValue('http://localhost:8123/foo/bar');
|
||||
|
||||
expect(canonicalizeHAURL(hass, '/api/foo')).toBe('http://localhost:8123/foo/bar');
|
||||
});
|
||||
|
||||
it('should return untouched URL when not HA relative', () => {
|
||||
expect(canonicalizeHAURL(createHASS(), 'http://localhost:8123/foo/bar')).toBe(
|
||||
'http://localhost:8123/foo/bar',
|
||||
);
|
||||
});
|
||||
|
||||
it('should return null without a URL', () => {
|
||||
expect(canonicalizeHAURL(createHASS())).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { getIntegrationManifest } from '../../../../src/utils/ha/integration';
|
||||
import { integrationManifestSchema } from '../../../../src/utils/ha/integration/types';
|
||||
import { homeAssistantWSRequest } from '../../../../src/utils/ha/ws-request';
|
||||
import { createHASS } from '../../../test-utils';
|
||||
|
||||
vi.mock('../../../../src/utils/ha/ws-request');
|
||||
|
||||
describe('getIntegrationManifest', () => {
|
||||
it('should get integration manifest', async () => {
|
||||
const hass = createHASS();
|
||||
await getIntegrationManifest(hass, 'INTEGRATION');
|
||||
expect(homeAssistantWSRequest).toHaveBeenCalledWith(
|
||||
hass,
|
||||
integrationManifestSchema,
|
||||
{ type: 'manifest/get', integration: 'INTEGRATION' },
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -1,12 +1,12 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { homeAssistantWSRequest } from '../../../../../src/utils/ha';
|
||||
import { createHASS, createRegistryDevice } from '../../../../test-utils.js';
|
||||
import {
|
||||
createDeviceRegistryCache,
|
||||
DeviceRegistryManager,
|
||||
} from '../../../../../src/utils/ha/registry/device';
|
||||
import { homeAssistantWSRequest } from '../../../../../src/utils/ha/ws-request';
|
||||
import { createHASS, createRegistryDevice } from '../../../../test-utils.js';
|
||||
|
||||
vi.mock('../../../../../src/utils/ha');
|
||||
vi.mock('../../../../../src/utils/ha/ws-request');
|
||||
vi.spyOn(global.console, 'warn').mockImplementation(() => true);
|
||||
|
||||
describe('DeviceRegistryManager', () => {
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { homeAssistantWSRequest } from '../../../../../src/utils/ha';
|
||||
import {
|
||||
createEntityRegistryCache,
|
||||
EntityRegistryManager,
|
||||
} from '../../../../../src/utils/ha/registry/entity';
|
||||
import { homeAssistantWSRequest } from '../../../../../src/utils/ha/ws-request';
|
||||
import { createHASS, createRegistryEntity } from '../../../../test-utils.js';
|
||||
|
||||
vi.mock('../../../../../src/utils/ha');
|
||||
vi.mock('../../../../../src/utils/ha/ws-request');
|
||||
vi.spyOn(global.console, 'warn').mockImplementation(() => true);
|
||||
|
||||
describe('EntityRegistryManager', () => {
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { CameraProxyConfig } from '../../../src/camera-manager/types.js';
|
||||
import {
|
||||
addDynamicProxyURL,
|
||||
getWebProxiedURL,
|
||||
shouldUseWebProxy,
|
||||
} from '../../../src/utils/ha/web-proxy.js';
|
||||
import { createHASS } from '../../test-utils.js';
|
||||
|
||||
describe('getWebProxiedURL', () => {
|
||||
it('should return proxied URL with v != 0', () => {
|
||||
expect(getWebProxiedURL('http://example.com', 2)).toBe(
|
||||
'/api/hass_web_proxy/v2/?url=http%3A%2F%2Fexample.com',
|
||||
);
|
||||
});
|
||||
|
||||
it('should return proxied URL with default v', () => {
|
||||
expect(getWebProxiedURL('http://example.com')).toBe(
|
||||
'/api/hass_web_proxy/v0/?url=http%3A%2F%2Fexample.com',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('shouldUseWebProxy', () => {
|
||||
const createProxyConfig = (
|
||||
config: Partial<CameraProxyConfig> = {},
|
||||
): CameraProxyConfig => ({
|
||||
media: true,
|
||||
ssl_verification: true,
|
||||
ssl_ciphers: 'default',
|
||||
dynamic: true,
|
||||
...config,
|
||||
});
|
||||
|
||||
it('should return false without a the proxy installed', () => {
|
||||
const hass = createHASS();
|
||||
hass.config.components = [];
|
||||
|
||||
expect(shouldUseWebProxy(hass, createProxyConfig())).toBe(false);
|
||||
});
|
||||
|
||||
it('should return when proxy config does not want proxying', () => {
|
||||
const hass = createHASS();
|
||||
hass.config.components = ['hass_web_proxy'];
|
||||
|
||||
const proxyConfig = createProxyConfig({ media: false });
|
||||
expect(shouldUseWebProxy(hass, proxyConfig, 'media')).toBe(false);
|
||||
});
|
||||
|
||||
it('should return when proxy config does want proxying', () => {
|
||||
const hass = createHASS();
|
||||
hass.config.components = ['hass_web_proxy'];
|
||||
|
||||
const proxyConfig = createProxyConfig({ media: true });
|
||||
expect(shouldUseWebProxy(hass, proxyConfig, 'media')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('addDynamicProxyURL', () => {
|
||||
it('should add dynamic proxy URL', async () => {
|
||||
const hass = createHASS();
|
||||
|
||||
await addDynamicProxyURL(hass, 'http://example.com', {
|
||||
urlID: 'id',
|
||||
sslVerification: true,
|
||||
sslCiphers: 'modern',
|
||||
openLimit: 5,
|
||||
ttl: 60,
|
||||
allowUnauthenticated: false,
|
||||
});
|
||||
|
||||
expect(hass.callService).toHaveBeenCalledWith(
|
||||
'hass_web_proxy',
|
||||
'create_proxied_url',
|
||||
{
|
||||
url_pattern: 'http://example.com',
|
||||
url_id: 'id',
|
||||
ssl_verification: true,
|
||||
ssl_ciphers: 'modern',
|
||||
open_limit: 5,
|
||||
ttl: 60,
|
||||
allow_unauthenticated: false,
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user