1
0
mirror of https://github.com/home-assistant/frontend.git synced 2025-12-25 05:06:18 +00:00
Files
frontend/src/data/lovelace.ts
Ian Richardson 274c2016c0 action-handler (#4115)
* action-handler

* comments

* address comments

* finish conversion

* move haptics

* address comments

* lint

* keyup

* double enter

* address comments

* keyup
2019-11-01 13:15:11 -05:00

141 lines
3.0 KiB
TypeScript

import { HomeAssistant } from "../types";
import { Connection, getCollection } from "home-assistant-js-websocket";
import { HASSDomEvent } from "../common/dom/fire_event";
export interface LovelaceConfig {
title?: string;
views: LovelaceViewConfig[];
background?: string;
resources?: Array<{ type: "css" | "js" | "module" | "html"; url: string }>;
}
export interface LovelaceViewConfig {
index?: number;
title?: string;
badges?: Array<string | LovelaceBadgeConfig>;
cards?: LovelaceCardConfig[];
path?: string;
icon?: string;
theme?: string;
panel?: boolean;
background?: string;
visible?: boolean | ShowViewConfig[];
}
export interface ShowViewConfig {
user?: string;
}
export interface LovelaceBadgeConfig {
type?: string;
[key: string]: any;
}
export interface LovelaceCardConfig {
index?: number;
view_index?: number;
type: string;
[key: string]: any;
}
export interface ToggleActionConfig extends BaseActionConfig {
action: "toggle";
}
export interface CallServiceActionConfig extends BaseActionConfig {
action: "call-service";
service: string;
service_data?: {
entity_id?: string | [string];
[key: string]: any;
};
}
export interface NavigateActionConfig extends BaseActionConfig {
action: "navigate";
navigation_path: string;
}
export interface UrlActionConfig extends BaseActionConfig {
action: "url";
url_path: string;
}
export interface MoreInfoActionConfig extends BaseActionConfig {
action: "more-info";
}
export interface NoActionConfig extends BaseActionConfig {
action: "none";
}
export interface BaseActionConfig {
confirmation?: ConfirmationRestrictionConfig;
}
export interface ConfirmationRestrictionConfig {
text?: string;
exemptions?: RestrictionConfig[];
}
export interface RestrictionConfig {
user: string;
}
export type ActionConfig =
| ToggleActionConfig
| CallServiceActionConfig
| NavigateActionConfig
| UrlActionConfig
| MoreInfoActionConfig
| NoActionConfig;
export const fetchConfig = (
conn: Connection,
force: boolean
): Promise<LovelaceConfig> =>
conn.sendMessagePromise({
type: "lovelace/config",
force,
});
export const saveConfig = (
hass: HomeAssistant,
config: LovelaceConfig
): Promise<void> =>
hass.callWS({
type: "lovelace/config/save",
config,
});
export const subscribeLovelaceUpdates = (
conn: Connection,
onChange: () => void
) => conn.subscribeEvents(onChange, "lovelace_updated");
export const getLovelaceCollection = (conn: Connection) =>
getCollection(
conn,
"_lovelace",
(conn2) => fetchConfig(conn2, false),
(_conn, store) =>
subscribeLovelaceUpdates(conn, () =>
fetchConfig(conn, false).then((config) => store.setState(config, true))
)
);
export interface WindowWithLovelaceProm extends Window {
llConfProm?: Promise<LovelaceConfig>;
}
export interface ActionHandlerOptions {
hasHold?: boolean;
hasDoubleClick?: boolean;
}
export interface ActionHandlerDetail {
action: string;
}
export type ActionHandlerEvent = HASSDomEvent<ActionHandlerDetail>;