-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add entity source API #12149
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
Merged
Merged
Add entity source API #12149
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { HomeAssistant } from "../../types"; | ||
|
|
||
| interface ResultCache<T> { | ||
| [entityId: string]: Promise<T> | undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Call a function with result caching per entity. | ||
| * @param cacheKey key to store the cache on hass object | ||
| * @param cacheTime time to cache the results | ||
| * @param func function to fetch the data | ||
| * @param hass Home Assistant object | ||
| * @param entityId entity to fetch data for | ||
| * @param args extra arguments to pass to the function to fetch the data | ||
| * @returns | ||
| */ | ||
| export const timeCacheEntityPromiseFunc = async <T>( | ||
| cacheKey: string, | ||
| cacheTime: number, | ||
| func: (hass: HomeAssistant, entityId: string, ...args: any[]) => Promise<T>, | ||
| hass: HomeAssistant, | ||
| entityId: string, | ||
| ...args: any[] | ||
| ): Promise<T> => { | ||
| let cache: ResultCache<T> | undefined = (hass as any)[cacheKey]; | ||
|
|
||
| if (!cache) { | ||
| cache = hass[cacheKey] = {}; | ||
| } | ||
|
|
||
| const lastResult = cache[entityId]; | ||
|
|
||
| if (lastResult) { | ||
| return lastResult; | ||
| } | ||
|
|
||
| const result = func(hass, entityId, ...args); | ||
| cache[entityId] = result; | ||
|
|
||
| result.then( | ||
| // When successful, set timer to clear cache | ||
| () => | ||
| setTimeout(() => { | ||
| cache![entityId] = undefined; | ||
| }, cacheTime), | ||
| // On failure, clear cache right away | ||
| () => { | ||
| cache![entityId] = undefined; | ||
| } | ||
| ); | ||
|
|
||
| return result; | ||
| }; | ||
This file contains hidden or 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,43 +1,80 @@ | ||
| import { HomeAssistant } from "../../types"; | ||
|
|
||
| interface ResultCache<T> { | ||
| [entityId: string]: Promise<T> | undefined; | ||
| interface CacheResult<T> { | ||
| result: T; | ||
| cacheKey: any; | ||
| } | ||
|
|
||
| /** | ||
| * Caches a result of a promise for X time. Allows optional extra validation | ||
| * check to invalidate the cache. | ||
| * @param cacheKey the key to store the cache | ||
| * @param cacheTime the time to cache the result | ||
| * @param func the function to fetch the data | ||
| * @param generateCacheKey optional function to generate a cache key based on current hass + cached result. Cache is invalid if generates a different cache key. | ||
| * @param hass Home Assistant object | ||
| * @param args extra arguments to pass to the function to fetch the data | ||
| * @returns | ||
| */ | ||
| export const timeCachePromiseFunc = async <T>( | ||
| cacheKey: string, | ||
| cacheTime: number, | ||
| func: (hass: HomeAssistant, entityId: string, ...args: any[]) => Promise<T>, | ||
| func: (hass: HomeAssistant, ...args: any[]) => Promise<T>, | ||
| generateCacheKey: | ||
| | ((hass: HomeAssistant, lastResult: T) => unknown) | ||
| | undefined, | ||
| hass: HomeAssistant, | ||
| entityId: string, | ||
| ...args: any[] | ||
| ): Promise<T> => { | ||
| let cache: ResultCache<T> | undefined = (hass as any)[cacheKey]; | ||
| const anyHass = hass as any; | ||
| const lastResult: Promise<CacheResult<T>> | CacheResult<T> | undefined = | ||
| anyHass[cacheKey]; | ||
|
|
||
| if (!cache) { | ||
| cache = hass[cacheKey] = {}; | ||
| } | ||
| const checkCachedResult = (result: CacheResult<T>): T | Promise<T> => { | ||
| if ( | ||
| !generateCacheKey || | ||
| generateCacheKey(hass, result.result) === result.cacheKey | ||
| ) { | ||
| return result.result; | ||
| } | ||
|
|
||
| const lastResult = cache[entityId]; | ||
| anyHass[cacheKey] = undefined; | ||
| return timeCachePromiseFunc( | ||
| cacheKey, | ||
| cacheTime, | ||
| func, | ||
| generateCacheKey, | ||
| hass, | ||
| ...args | ||
| ); | ||
| }; | ||
|
|
||
| // If we have a cached result, return it if it's still valid | ||
| if (lastResult) { | ||
| return lastResult; | ||
| return lastResult instanceof Promise | ||
| ? lastResult.then(checkCachedResult) | ||
| : checkCachedResult(lastResult); | ||
| } | ||
|
|
||
| const result = func(hass, entityId, ...args); | ||
| cache[entityId] = result; | ||
| const resultPromise = func(hass, ...args); | ||
| anyHass[cacheKey] = resultPromise; | ||
|
|
||
| result.then( | ||
| resultPromise.then( | ||
| // When successful, set timer to clear cache | ||
| () => | ||
| (result) => { | ||
| anyHass[cacheKey] = { | ||
| result, | ||
| cacheKey: generateCacheKey?.(hass, result), | ||
| }; | ||
| setTimeout(() => { | ||
| cache![entityId] = undefined; | ||
| }, cacheTime), | ||
| anyHass[cacheKey] = undefined; | ||
| }, cacheTime); | ||
| }, | ||
| // On failure, clear cache right away | ||
| () => { | ||
| cache![entityId] = undefined; | ||
| anyHass[cacheKey] = undefined; | ||
| } | ||
| ); | ||
|
|
||
| return result; | ||
| return resultPromise; | ||
| }; |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { timeCachePromiseFunc } from "../common/util/time-cache-function-promise"; | ||
| import { HomeAssistant } from "../types"; | ||
|
|
||
| interface EntitySourceConfigEntry { | ||
| source: "config_entry"; | ||
| domain: string; | ||
| custom_component: boolean; | ||
| config_entry: string; | ||
| } | ||
|
|
||
| interface EntitySourcePlatformConfig { | ||
| source: "platform_config"; | ||
| domain: string; | ||
| custom_component: boolean; | ||
| } | ||
|
|
||
| export type EntitySources = Record< | ||
| string, | ||
| EntitySourceConfigEntry | EntitySourcePlatformConfig | ||
| >; | ||
|
|
||
| const fetchEntitySources = ( | ||
| hass: HomeAssistant, | ||
| entity_id?: string | ||
| ): Promise<EntitySources> => | ||
| hass.callWS({ | ||
| type: "entity/source", | ||
| entity_id, | ||
| }); | ||
|
|
||
| export const fetchEntitySourcesWithCache = ( | ||
| hass: HomeAssistant, | ||
| entity_id?: string | ||
| ): Promise<EntitySources> => | ||
| entity_id | ||
| ? fetchEntitySources(hass, entity_id) | ||
| : timeCachePromiseFunc( | ||
| "_entitySources", | ||
| // cache for 30 seconds | ||
| 30000, | ||
| fetchEntitySources, | ||
| // We base the cache on number of states. If number of states | ||
| // changes we force a refresh | ||
| (hass2) => Object.keys(hass2.states).length, | ||
| hass | ||
| ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a new function, I just moved it.