refactor: Cache device information centrally (#1598)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
@@ -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(),
|
||||
);
|
||||
@@ -178,6 +185,10 @@ export class CardController
|
||||
return this._defaultManager;
|
||||
}
|
||||
|
||||
public getDeviceRegistryManager(): DeviceRegistryManager {
|
||||
return this._deviceRegistryManager;
|
||||
}
|
||||
|
||||
public getDownloadManager(): DownloadManager {
|
||||
return this._downloadManager;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -6,12 +6,16 @@ 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;
|
||||
|
||||
@@ -19,7 +23,11 @@ export class FrigateCardDiagnostics extends LitElement {
|
||||
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 {
|
||||
|
||||
@@ -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')
|
||||
@@ -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();
|
||||
@@ -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>`
|
||||
: ``}
|
||||
|
||||
+10
-12
@@ -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>;
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
@@ -1,14 +1,54 @@
|
||||
import { HomeAssistant } from '@dermotduffy/custom-card-helpers';
|
||||
import { homeAssistantWSRequest } from '../..';
|
||||
import { DeviceList, deviceListSchema } from './types';
|
||||
import { errorToConsole } from '../../../basic';
|
||||
import { RegistryCache } from '../cache';
|
||||
import { Device, 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',
|
||||
});
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
@@ -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);
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user