device action builder
This commit is contained in:
1 parent
6be6005531
commit
029df7932e
16 files changed
+725
-434
No files matched your search
@@ -0,0 +1,84 @@
|
||||
import { OptionsObject, useSnackbar } from "notistack";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { HASendMessage } from "./types";
|
||||
|
||||
export type DeviceTypeAction = {
|
||||
type: string;
|
||||
entity_id?: string;
|
||||
device_id?: string;
|
||||
domain: string;
|
||||
};
|
||||
export type DeviceTypeCapability = {
|
||||
extra_fields: Array<{
|
||||
name: string;
|
||||
required?: true;
|
||||
optional?: true;
|
||||
type: string;
|
||||
}>;
|
||||
};
|
||||
export const getDeviceExtraWsCalls = (callHA: HASendMessage) => {
|
||||
return {
|
||||
useDeviceActions(deviceId?: string) {
|
||||
const lastDeviceId = useRef<string | null>(null);
|
||||
const [options, setOptions] = useState<
|
||||
Array<{
|
||||
type: string;
|
||||
actions: DeviceTypeAction[];
|
||||
}>
|
||||
>([]);
|
||||
useEffect(() => {
|
||||
if (deviceId && lastDeviceId.current !== deviceId) {
|
||||
lastDeviceId.current = deviceId;
|
||||
callHA({
|
||||
type: "device_automation/action/list",
|
||||
device_id: deviceId,
|
||||
}).then((data: any[]) => {
|
||||
if (lastDeviceId.current === deviceId) {
|
||||
const dataMap: Record<string, DeviceTypeAction[]> = data.reduce(
|
||||
(all = {}, opt: any) => {
|
||||
if (!all[opt.type]) {
|
||||
all[opt.type] = [];
|
||||
}
|
||||
all[opt.type].push(opt);
|
||||
return all;
|
||||
},
|
||||
{}
|
||||
);
|
||||
setOptions(
|
||||
Object.keys(dataMap).map((type) => ({
|
||||
type,
|
||||
actions: dataMap[type],
|
||||
}))
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}, [deviceId]);
|
||||
return options;
|
||||
},
|
||||
useDeviceCapalities(action?: DeviceTypeAction) {
|
||||
const snackbr = useSnackbar();
|
||||
const lastAction = useRef<string | null>(null);
|
||||
const [caps, setCaps] = useState<Partial<DeviceTypeCapability>>({});
|
||||
useEffect(() => {
|
||||
const currentAction = JSON.stringify(action);
|
||||
if (action && lastAction.current !== currentAction) {
|
||||
lastAction.current = currentAction;
|
||||
callHA({
|
||||
type: "device_automation/action/capabilities",
|
||||
action,
|
||||
})
|
||||
.then((data: DeviceTypeCapability) => {
|
||||
if (lastAction.current === currentAction) {
|
||||
setCaps(data);
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
console.warn(err);
|
||||
});
|
||||
}
|
||||
}, [action]);
|
||||
return caps;
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -2,12 +2,13 @@ import { entitiesColl, servicesColl } from "home-assistant-js-websocket";
|
||||
import { Namer } from "utils/formatting";
|
||||
import { deviceRegistryColl } from "./DeviceRegistry";
|
||||
import { entityRegistryColl } from "./EntityRegistry";
|
||||
import { DeviceRegistryItem } from "./types";
|
||||
import { DeviceRegistryItem, HASendMessage } from "./types";
|
||||
import { useHassCollection } from "./useHassCollection";
|
||||
import { useHAConnection } from "./connection";
|
||||
import { useSnackbar } from "notistack";
|
||||
import { TypedHassService } from "./fieldTypes";
|
||||
import { Option } from "components/Inputs/InputAutoComplete";
|
||||
import { getDeviceExtraWsCalls } from "./extras";
|
||||
|
||||
export type EntityOption = Option<{ domain: string }>;
|
||||
export type HAEntitiesState = ReturnType<typeof useHAEntities>;
|
||||
@@ -216,6 +217,7 @@ export const useHAServices = () => {
|
||||
};
|
||||
|
||||
export type DeviceRegistryOption = Option<{ manufacturer: string }>;
|
||||
export type HADeviceRegistry = ReturnType<typeof useHADeviceRegistry>;
|
||||
export const useHADeviceRegistry = () => {
|
||||
const dr = useHassCollection(deviceRegistryColl);
|
||||
const getLabelForItem = (item: DeviceRegistryItem) =>
|
||||
@@ -255,6 +257,7 @@ export const useHAEntityRegistry = () => {
|
||||
return dr;
|
||||
};
|
||||
|
||||
export type HAService = ReturnType<typeof useHA>;
|
||||
export const useHA = () => {
|
||||
const entities = useHAEntities();
|
||||
const devices = useHADeviceRegistry();
|
||||
@@ -312,6 +315,12 @@ export const useHA = () => {
|
||||
}
|
||||
};
|
||||
|
||||
const callHA: HASendMessage = async (sendMsg) => {
|
||||
if (conn.status === "loaded") {
|
||||
return await conn.connection.sendMessagePromise(sendMsg);
|
||||
}
|
||||
};
|
||||
|
||||
const namer: Namer = {
|
||||
getDeviceName(device_id) {
|
||||
return devices.getLabel(device_id);
|
||||
@@ -336,6 +345,8 @@ export const useHA = () => {
|
||||
services,
|
||||
namer,
|
||||
callService,
|
||||
callHA,
|
||||
deviceExtras: getDeviceExtraWsCalls(callHA),
|
||||
reloadAutomations: () => callService("automation", "reload"),
|
||||
};
|
||||
};
|
||||
@@ -1,41 +1,28 @@
|
||||
export type EntityRegisteryItem = {
|
||||
area_id: null | string;
|
||||
config_entry_id: null | string;
|
||||
device_id: null | string;
|
||||
disabled_by:
|
||||
| "integration"
|
||||
| "user"
|
||||
| "config_entry"
|
||||
| string
|
||||
| null
|
||||
;
|
||||
entity_category:
|
||||
| "diagnostic"
|
||||
| "config"
|
||||
| string
|
||||
| null
|
||||
;
|
||||
hidden_by:
|
||||
| "integration"
|
||||
| "user"
|
||||
| string
|
||||
| null
|
||||
;
|
||||
entity_id: string;
|
||||
icon: null | string;
|
||||
name: null | string;
|
||||
platform: string;
|
||||
}
|
||||
area_id: null | string;
|
||||
config_entry_id: null | string;
|
||||
device_id: null | string;
|
||||
disabled_by: "integration" | "user" | "config_entry" | string | null;
|
||||
entity_category: "diagnostic" | "config" | string | null;
|
||||
hidden_by: "integration" | "user" | string | null;
|
||||
entity_id: string;
|
||||
icon: null | string;
|
||||
name: null | string;
|
||||
platform: string;
|
||||
};
|
||||
|
||||
export type DeviceRegistryItem = {
|
||||
id: string;
|
||||
name: string;
|
||||
name_by_user: string | null;
|
||||
manufacturer: string | null;
|
||||
entry_type: 'service' | string | null;
|
||||
disabled_by: EntityRegisteryItem['disabled_by'];
|
||||
area_id: string | null;
|
||||
}
|
||||
id: string;
|
||||
name: string;
|
||||
name_by_user: string | null;
|
||||
manufacturer: string | null;
|
||||
entry_type: "service" | string | null;
|
||||
disabled_by: EntityRegisteryItem["disabled_by"];
|
||||
area_id: string | null;
|
||||
};
|
||||
|
||||
export type HassDevices = Array<DeviceRegistryItem>;
|
||||
export type HassEntitiesRegistry = Array<EntityRegisteryItem>;
|
||||
export type HassEntitiesRegistry = Array<EntityRegisteryItem>;
|
||||
export type HASendMessage = (
|
||||
sendMsg: { type: string } & Record<string, any>
|
||||
) => Promise<any>;
|
||||
Reference in new issue
Block a user