mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-27 11:16:35 +00:00
Simplify entity sources (#17770)
This commit is contained in:
parent
32472ca627
commit
085b26d5ea
@ -1,46 +1,25 @@
|
|||||||
import { timeCachePromiseFunc } from "../common/util/time-cache-function-promise";
|
import { timeCachePromiseFunc } from "../common/util/time-cache-function-promise";
|
||||||
import { HomeAssistant } from "../types";
|
import { HomeAssistant } from "../types";
|
||||||
|
|
||||||
interface EntitySourceConfigEntry {
|
interface EntitySource {
|
||||||
source: "config_entry";
|
|
||||||
domain: string;
|
domain: string;
|
||||||
custom_component: boolean;
|
|
||||||
config_entry: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface EntitySourcePlatformConfig {
|
export type EntitySources = Record<string, EntitySource>;
|
||||||
source: "platform_config";
|
|
||||||
domain: string;
|
|
||||||
custom_component: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type EntitySources = Record<
|
const fetchEntitySources = (hass: HomeAssistant): Promise<EntitySources> =>
|
||||||
string,
|
hass.callWS({ type: "entity/source" });
|
||||||
EntitySourceConfigEntry | EntitySourcePlatformConfig
|
|
||||||
>;
|
|
||||||
|
|
||||||
const fetchEntitySources = (
|
|
||||||
hass: HomeAssistant,
|
|
||||||
entity_id?: string
|
|
||||||
): Promise<EntitySources> =>
|
|
||||||
hass.callWS({
|
|
||||||
type: "entity/source",
|
|
||||||
entity_id,
|
|
||||||
});
|
|
||||||
|
|
||||||
export const fetchEntitySourcesWithCache = (
|
export const fetchEntitySourcesWithCache = (
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant
|
||||||
entity_id?: string
|
|
||||||
): Promise<EntitySources> =>
|
): Promise<EntitySources> =>
|
||||||
entity_id
|
timeCachePromiseFunc(
|
||||||
? fetchEntitySources(hass, entity_id)
|
"_entitySources",
|
||||||
: timeCachePromiseFunc(
|
// cache for 30 seconds
|
||||||
"_entitySources",
|
30000,
|
||||||
// cache for 30 seconds
|
fetchEntitySources,
|
||||||
30000,
|
// We base the cache on number of states. If number of states
|
||||||
fetchEntitySources,
|
// changes we force a refresh
|
||||||
// We base the cache on number of states. If number of states
|
(hass2) => Object.keys(hass2.states).length,
|
||||||
// changes we force a refresh
|
hass
|
||||||
(hass2) => Object.keys(hass2.states).length,
|
);
|
||||||
hass
|
|
||||||
);
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user