refactor: Cache device information centrally (#1598)

This commit is contained in:
Dermot Duffy
2024-09-29 17:17:40 -07:00
committed by GitHub
parent 02fb6e1d4c
commit 9393722fa6
14 changed files with 281 additions and 93 deletions
+50 -10
View File
@@ -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;
}
}
+14 -5
View File
@@ -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;
}