Skip to content

Commit

Permalink
refactor: Cache device information centrally
Browse files Browse the repository at this point in the history
  • Loading branch information
dermotduffy committed Sep 30, 2024
1 parent 02fb6e1 commit 17774c9
Show file tree
Hide file tree
Showing 14 changed files with 282 additions and 94 deletions.
4 changes: 2 additions & 2 deletions src/camera-manager/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ export class CameraManager {
camerasConfig.some((config) => hasAutoTriggers(config))
) {
// ... then we need to populate the entity cache by fetching all entities
// from Home Assistant. Do this once upfront, to avoid each camera doing
// it.
// from Home Assistant. Attempt to do this once upfront, to avoid each
// camera doing needing to fetch entity state.
await this._api.getEntityRegistryManager().fetchEntityList(hass);
}

Expand Down
11 changes: 11 additions & 0 deletions src/card-controller/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { LovelaceCardEditor } from '@dermotduffy/custom-card-helpers';
import { ReactiveController } from 'lit';
import { CameraManager } from '../camera-manager/manager';
import { FrigateCardConfig } from '../config/types';
import {
createDeviceRegistryCache,
DeviceRegistryManager,
} from '../utils/ha/registry/device';
import {
createEntityRegistryCache,
EntityRegistryManager,
Expand Down Expand Up @@ -90,6 +94,9 @@ export class CardController
{
// These properties may be used in the construction of 'managers' (and should
// be created first).
protected _deviceRegistryManager = new DeviceRegistryManager(
createDeviceRegistryCache(),
);
protected _entityRegistryManager = new EntityRegistryManager(
createEntityRegistryCache(),
);
Expand Down Expand Up @@ -178,6 +185,10 @@ export class CardController
return this._defaultManager;
}

public getDeviceRegistryManager(): DeviceRegistryManager {
return this._deviceRegistryManager;
}

public getDownloadManager(): DownloadManager {
return this._downloadManager;
}
Expand Down
1 change: 1 addition & 0 deletions src/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ class FrigateCard extends LitElement {
.triggeredCameraIDs=${this._config?.view.triggers.show_trigger_status
? this._controller.getTriggersManager().getTriggeredCameraIDs()
: undefined}
.deviceRegistryManager=${this._controller.getDeviceRegistryManager()}
></frigate-card-views>
${
// Keep message rendering to last to show messages that may have been
Expand Down
10 changes: 9 additions & 1 deletion src/components/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@ import { localize } from '../localize/localize';
import basicBlockStyle from '../scss/basic-block.scss';
import { Diagnostics, getDiagnostics } from '../utils/diagnostics';
import { renderMessage } from './message';
import { DeviceRegistryManager } from '../utils/ha/registry/device';

@customElement('frigate-card-diagnostics')
export class FrigateCardDiagnostics extends LitElement {
@property({ attribute: false })
public hass?: HomeAssistant;

@property({ attribute: false })
public deviceRegistryManager?: DeviceRegistryManager;

@property({ attribute: false })
public rawConfig?: RawFrigateCardConfig;

@state()
protected _diagnostics: Diagnostics | null = null;

protected async _fetchDiagnostics(): Promise<void> {
this._diagnostics = await getDiagnostics(this.hass, this.rawConfig);
this._diagnostics = await getDiagnostics(
this.hass,
this.deviceRegistryManager,
this.rawConfig,
);
}

protected shouldUpdate(): boolean {
Expand Down
8 changes: 7 additions & 1 deletion src/components/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import {
} from '../config/types.js';
import viewsStyle from '../scss/views.scss';
import { ExtendedHomeAssistant } from '../types.js';
import { DeviceRegistryManager } from '../utils/ha/registry/device/index.js';
import { ResolvedMediaCache } from '../utils/ha/resolved-media.js';

// As a special case: Diagnostics is not dynamically loaded in case something goes wrong.
// As a special case: The diagnostics view is not dynamically loaded in case
// something goes wrong.
import './diagnostics.js';

@customElement('frigate-card-views')
Expand Down Expand Up @@ -62,6 +64,9 @@ export class FrigateCardViews extends LitElement {
@property({ attribute: false })
public triggeredCameraIDs?: Set<string>;

@property({ attribute: false })
public deviceRegistryManager?: DeviceRegistryManager;

protected willUpdate(changedProps: PropertyValues): void {
if (changedProps.has('viewManagerEpoch') || changedProps.has('config')) {
const view = this.viewManagerEpoch?.manager.getView();
Expand Down Expand Up @@ -215,6 +220,7 @@ export class FrigateCardViews extends LitElement {
? html` <frigate-card-diagnostics
.hass=${this.hass}
.rawConfig=${this.rawConfig}
.deviceRegistryManager=${this.deviceRegistryManager}
>
</frigate-card-diagnostics>`
: ``}
Expand Down
22 changes: 10 additions & 12 deletions src/utils/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { HomeAssistant } from '@dermotduffy/custom-card-helpers';
import pkg from '../../package.json';
import { RawFrigateCardConfig } from '../config/types';
import { getLanguage } from '../localize/localize';
import { getAllDevices } from './ha/registry/device';
import { DeviceList } from './ha/registry/device/types';
import { DeviceRegistryManager } from './ha/registry/device';

type FrigateVersions = Record<string, string>;

Expand Down Expand Up @@ -44,20 +43,19 @@ export interface Diagnostics {

export const getDiagnostics = async (
hass?: HomeAssistant,
deviceRegistryManager?: DeviceRegistryManager,
rawConfig?: RawFrigateCardConfig,
): Promise<Diagnostics> => {
let devices: DeviceList | undefined = [];
if (hass) {
try {
devices = await getAllDevices(hass);
} catch (e) {
// Pass. This is optional.
}
}

// Get the Frigate devices in order to extract the Frigate integration and
// server version numbers.
const frigateDevices = devices?.filter((device) => device.manufacturer === 'Frigate');
const frigateDevices =
hass && deviceRegistryManager
? await deviceRegistryManager.getMatchingDevices(
hass,
(device) => device.manufacturer === 'Frigate',
)
: [];

const frigateVersionMap: Map<string, string> = new Map();
frigateDevices?.forEach((device) => {
device.config_entries.forEach((configEntry) => {
Expand Down
62 changes: 51 additions & 11 deletions src/utils/ha/registry/device/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,54 @@
import { HomeAssistant } from '@dermotduffy/custom-card-helpers';
import { homeAssistantWSRequest } from '../..';
import { DeviceList, deviceListSchema } from './types';

/**
* Get a list of all entities from the entity registry. May throw.
* @param hass The Home Assistant object.
* @returns An entity list object.
*/
export const getAllDevices = async (hass: HomeAssistant): Promise<DeviceList> => {
return await homeAssistantWSRequest<DeviceList>(hass, deviceListSchema, {
type: 'config/device_registry/list',
});
import { errorToConsole } from '../../../basic';
import { RegistryCache } from '../cache';
import { Device, DeviceList, deviceListSchema } from './types';

export const createDeviceRegistryCache = (): RegistryCache<Device> => {
return new RegistryCache<Device>((device) => device.id);
};

export class DeviceRegistryManager {
protected _cache: RegistryCache<Device>;
protected _fetchedDeviceList = false;

constructor(cache: RegistryCache<Device>) {
this._cache = cache;
}

public async getDevice(hass: HomeAssistant, deviceID: string): Promise<Device | null> {
if (this._cache.has(deviceID)) {
return this._cache.get(deviceID);
}

// There is currently no way to fetch a single device.
await this._fetchDeviceList(hass);
return this._cache.get(deviceID) ?? null;
}

public async getMatchingDevices(
hass: HomeAssistant,
func: (arg: Device) => boolean,
): Promise<Device[]> {
await this._fetchDeviceList(hass);
return this._cache.getMatches(func);
}

protected async _fetchDeviceList(hass: HomeAssistant): Promise<void> {
if (this._fetchedDeviceList) {
return;
}

let deviceList: DeviceList | null = null;
try {
deviceList = await homeAssistantWSRequest<DeviceList>(hass, deviceListSchema, {
type: 'config/device_registry/list',
});
} catch (e) {
errorToConsole(e as Error);
return;
}
this._cache.add(deviceList);
this._fetchedDeviceList = true;
}
}
19 changes: 14 additions & 5 deletions src/utils/ha/registry/entity/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { HomeAssistant } from '@dermotduffy/custom-card-helpers';
import { homeAssistantWSRequest } from '../..';
import { Entity, EntityList, entitySchema, entityListSchema } from './types.js';
import { errorToConsole } from '../../../basic';
import { RegistryCache } from '../cache';
import { Entity, EntityList, entityListSchema, entitySchema } from './types.js';

export const createEntityRegistryCache = (): RegistryCache<Entity> => {
return new RegistryCache<Entity>((entity) => entity.entity_id);
Expand Down Expand Up @@ -31,7 +32,8 @@ export class EntityRegistryManager {
type: 'config/entity_registry/get',
entity_id: entityID,
});
} catch {
} catch (e) {
errorToConsole(e as Error);
return null;
}
this._cache.add(entity);
Expand Down Expand Up @@ -68,9 +70,16 @@ export class EntityRegistryManager {
if (this._fetchedEntityList) {
return;
}
const entityList = await homeAssistantWSRequest<EntityList>(hass, entityListSchema, {
type: 'config/entity_registry/list',
});

let entityList: EntityList | null = null;
try {
entityList = await homeAssistantWSRequest<EntityList>(hass, entityListSchema, {
type: 'config/entity_registry/list',
});
} catch (e) {
errorToConsole(e as Error);
return;
}
this._cache.add(entityList);
this._fetchedEntityList = true;
}
Expand Down
8 changes: 8 additions & 0 deletions tests/card-controller/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { StyleManager } from '../../src/card-controller/style-manager';
import { TriggersManager } from '../../src/card-controller/triggers-manager';
import { ViewManager } from '../../src/card-controller/view/view-manager';
import { FrigateCardEditor } from '../../src/editor';
import { DeviceRegistryManager } from '../../src/utils/ha/registry/device';
import { EntityRegistryManager } from '../../src/utils/ha/registry/entity';
import { ResolvedMediaCache } from '../../src/utils/ha/resolved-media';

Expand Down Expand Up @@ -55,6 +56,7 @@ vi.mock('../../src/card-controller/status-bar-item-manager');
vi.mock('../../src/card-controller/style-manager');
vi.mock('../../src/card-controller/triggers-manager');
vi.mock('../../src/card-controller/view/view-manager');
vi.mock('../../src/utils/ha/registry/device');
vi.mock('../../src/utils/ha/registry/entity');
vi.mock('../../src/utils/ha/resolved-media');

Expand Down Expand Up @@ -149,6 +151,12 @@ describe('CardController', () => {
);
});

it('getDeviceRegistryManager', () => {
expect(createController().getDeviceRegistryManager()).toBe(
vi.mocked(DeviceRegistryManager).mock.instances[0],
);
});

it('getDownloadManager', () => {
expect(createController().getDownloadManager()).toBe(
vi.mocked(DownloadManager).mock.instances[0],
Expand Down
10 changes: 10 additions & 0 deletions tests/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
} from '../src/config/types';
import { CapabilitiesRaw, ExtendedHomeAssistant, MediaLoadedInfo } from '../src/types';
import { HassStateDifference } from '../src/utils/ha';
import { Device } from '../src/utils/ha/registry/device/types';
import { EntityRegistryManager } from '../src/utils/ha/registry/entity';
import { Entity } from '../src/utils/ha/registry/entity/types';
import { ViewMedia, ViewMediaType } from '../src/view/media';
Expand Down Expand Up @@ -128,6 +129,15 @@ export const createUser = (user?: Partial<CurrentUser>): CurrentUser => ({
...user,
});

export const createRegistryDevice = (device?: Partial<Device>): Device => {
return {
id: device?.id ?? 'id',
model: device?.model ?? null,
config_entries: device?.config_entries ?? [],
manufacturer: device?.manufacturer ?? null,
};
};

export const createRegistryEntity = (entity?: Partial<Entity>): Entity => {
return {
config_entry_id: entity?.config_entry_id ?? null,
Expand Down
Loading

0 comments on commit 17774c9

Please sign in to comment.