Update external barcode scanning API (#20470)
This commit is contained in:
parent
7ecf8b755e
commit
ac378cfe6d
|
@ -9,7 +9,19 @@ import { fireEvent } from "../common/dom/fire_event";
|
|||
import { mainWindow } from "../common/dom/get_main_window";
|
||||
import { showAutomationEditor } from "../data/automation";
|
||||
import { HomeAssistantMain } from "../layouts/home-assistant-main";
|
||||
import type { EMIncomingMessageCommands } from "./external_messaging";
|
||||
import type {
|
||||
EMIncomingMessageBarCodeScanAborted,
|
||||
EMIncomingMessageBarCodeScanResult,
|
||||
EMIncomingMessageCommands,
|
||||
} from "./external_messaging";
|
||||
|
||||
const barCodeListeners = new Set<
|
||||
(
|
||||
msg:
|
||||
| EMIncomingMessageBarCodeScanResult
|
||||
| EMIncomingMessageBarCodeScanAborted
|
||||
) => boolean
|
||||
>();
|
||||
|
||||
export const attachExternalToApp = (hassMainEl: HomeAssistantMain) => {
|
||||
window.addEventListener("haptic", (ev) =>
|
||||
|
@ -24,6 +36,19 @@ export const attachExternalToApp = (hassMainEl: HomeAssistantMain) => {
|
|||
);
|
||||
};
|
||||
|
||||
export const addExternalBarCodeListener = (
|
||||
listener: (
|
||||
msg:
|
||||
| EMIncomingMessageBarCodeScanResult
|
||||
| EMIncomingMessageBarCodeScanAborted
|
||||
) => boolean
|
||||
) => {
|
||||
barCodeListeners.add(listener);
|
||||
return () => {
|
||||
barCodeListeners.delete(listener);
|
||||
};
|
||||
};
|
||||
|
||||
const handleExternalMessage = (
|
||||
hassMainEl: HomeAssistantMain,
|
||||
msg: EMIncomingMessageCommands
|
||||
|
@ -88,6 +113,22 @@ const handleExternalMessage = (
|
|||
success: true,
|
||||
result: null,
|
||||
});
|
||||
} else if (msg.command === "bar_code/scan_result") {
|
||||
barCodeListeners.forEach((listener) => listener(msg));
|
||||
bus.fireMessage({
|
||||
id: msg.id,
|
||||
type: "result",
|
||||
success: true,
|
||||
result: null,
|
||||
});
|
||||
} else if (msg.command === "bar_code/aborted") {
|
||||
barCodeListeners.forEach((listener) => listener(msg));
|
||||
bus.fireMessage({
|
||||
id: msg.id,
|
||||
type: "result",
|
||||
success: true,
|
||||
result: null,
|
||||
});
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -37,9 +37,11 @@ interface EMOutgoingMessageConfigGet extends EMMessage {
|
|||
|
||||
interface EMOutgoingMessageBarCodeScan extends EMMessage {
|
||||
type: "bar_code/scan";
|
||||
title: string;
|
||||
description: string;
|
||||
alternative_option_label?: string;
|
||||
payload: {
|
||||
title: string;
|
||||
description: string;
|
||||
alternative_option_label?: string;
|
||||
};
|
||||
}
|
||||
|
||||
interface EMOutgoingMessageBarCodeClose extends EMMessage {
|
||||
|
@ -48,7 +50,9 @@ interface EMOutgoingMessageBarCodeClose extends EMMessage {
|
|||
|
||||
interface EMOutgoingMessageBarCodeNotify extends EMMessage {
|
||||
type: "bar_code/notify";
|
||||
message: string;
|
||||
payload: {
|
||||
message: string;
|
||||
};
|
||||
}
|
||||
|
||||
interface EMOutgoingMessageMatterCommission extends EMMessage {
|
||||
|
|
Loading…
Reference in New Issue