-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32472ca
commit 085b26d
Showing
1 changed file
with
15 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,25 @@ | ||
import { timeCachePromiseFunc } from "../common/util/time-cache-function-promise"; | ||
import { HomeAssistant } from "../types"; | ||
|
||
interface EntitySourceConfigEntry { | ||
source: "config_entry"; | ||
interface EntitySource { | ||
domain: string; | ||
custom_component: boolean; | ||
config_entry: string; | ||
} | ||
|
||
interface EntitySourcePlatformConfig { | ||
source: "platform_config"; | ||
domain: string; | ||
custom_component: boolean; | ||
} | ||
export type EntitySources = Record<string, EntitySource>; | ||
|
||
export type EntitySources = Record< | ||
string, | ||
EntitySourceConfigEntry | EntitySourcePlatformConfig | ||
>; | ||
|
||
const fetchEntitySources = ( | ||
hass: HomeAssistant, | ||
entity_id?: string | ||
): Promise<EntitySources> => | ||
hass.callWS({ | ||
type: "entity/source", | ||
entity_id, | ||
}); | ||
const fetchEntitySources = (hass: HomeAssistant): Promise<EntitySources> => | ||
hass.callWS({ type: "entity/source" }); | ||
|
||
export const fetchEntitySourcesWithCache = ( | ||
hass: HomeAssistant, | ||
entity_id?: string | ||
hass: HomeAssistant | ||
): 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 | ||
); | ||
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 | ||
); |