generated from custom-cards/boilerplate-card
-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Cache device information centrally
- Loading branch information
1 parent
02fb6e1
commit 17774c9
Showing
14 changed files
with
282 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.