Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Untriggering to default should reset to default camera #1571

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/card-controller/view/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,37 @@ export class ViewFactory {
return null;
}

let forceCameraID: string | null = options?.params?.camera ?? null;
// Neither options.baseView.camera nor options.baseView.view are respected
// here, since this is the default view / camera.
// See: https://github.com/dermotduffy/frigate-hass-card/issues/1564

let cameraID: string | null = null;
const viewName = options?.params?.view ?? config.view.default;

if (
!forceCameraID &&
options?.baseView?.camera &&
config.view.default_cycle_camera
) {
if (options?.params?.camera) {
cameraID = options.params.camera;
} else {
const cameraIDs = [
...getCameraIDsForViewName(this._api.getCameraManager(), viewName),
];
const currentIndex = cameraIDs.indexOf(options?.baseView?.camera);
const targetIndex = currentIndex + 1 >= cameraIDs.length ? 0 : currentIndex + 1;
forceCameraID = cameraIDs[targetIndex];
if (!cameraIDs.length) {
return null;
}

if (options?.baseView?.camera && config.view.default_cycle_camera) {
const currentIndex = cameraIDs.indexOf(options.baseView.camera);
const targetIndex = currentIndex + 1 >= cameraIDs.length ? 0 : currentIndex + 1;
cameraID = cameraIDs[targetIndex];
} else {
cameraID = cameraIDs[0];
}
}

return this.getViewByParameters({
params: {
...options?.params,
view: viewName,
...(forceCameraID && { camera: forceCameraID }),
camera: cameraID,
},
baseView: options?.baseView,
});
Expand Down
45 changes: 43 additions & 2 deletions tests/card-controller/view/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,33 @@ describe('getViewDefault', () => {
expect(factory.getViewDefault()).toBeNull();
});

it('should return null if no cameras support view', () => {
const api = createCardAPI();
vi.mocked(api.getConfigManager().getConfig).mockReturnValue(createConfig());
vi.mocked(api.getCameraManager).mockReturnValue(createCameraManager());

// No cameras support live.
vi.mocked(api.getCameraManager().getStore).mockReturnValue(
createStore([
{
cameraID: 'camera.office',
capabilities: createCapabilities({
live: false,
}),
},
{
cameraID: 'camera.kitchen',
capabilities: createCapabilities({
live: false,
}),
},
]),
);

const factory = new ViewFactory(api);
expect(factory.getViewDefault()).toBeNull();
});

it('should create view', () => {
const factory = new ViewFactory(createPopulatedAPI());
const view = factory.getViewDefault();
Expand Down Expand Up @@ -64,16 +91,30 @@ describe('getViewDefault', () => {
expect(view?.camera).toBe('camera.office');
});

it('should use default camera when camera unspecified', () => {
// Even though baseView has a camera, since default is called it should
// use that camera.

const factory = new ViewFactory(createPopulatedAPI());
const baseView = createView({ camera: 'camera.kitchen' });
const view = factory.getViewDefault({
baseView: baseView,
});

expect(view?.is('live')).toBeTruthy();
expect(view?.camera).toBe('camera.office');
});

it('should respect parameters', () => {
const factory = new ViewFactory(createPopulatedAPI());
const view = factory.getViewDefault({
params: {
camera: 'camera.office',
camera: 'camera.kitchen',
},
});

expect(view?.is('live')).toBeTruthy();
expect(view?.camera).toBe('camera.office');
expect(view?.camera).toBe('camera.kitchen');
});
});

Expand Down
Loading