mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-26 18:56:39 +00:00
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 { mainWindow } from "../common/dom/get_main_window";
|
||||||
import { showAutomationEditor } from "../data/automation";
|
import { showAutomationEditor } from "../data/automation";
|
||||||
import { HomeAssistantMain } from "../layouts/home-assistant-main";
|
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) => {
|
export const attachExternalToApp = (hassMainEl: HomeAssistantMain) => {
|
||||||
window.addEventListener("haptic", (ev) =>
|
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 = (
|
const handleExternalMessage = (
|
||||||
hassMainEl: HomeAssistantMain,
|
hassMainEl: HomeAssistantMain,
|
||||||
msg: EMIncomingMessageCommands
|
msg: EMIncomingMessageCommands
|
||||||
@ -88,6 +113,22 @@ const handleExternalMessage = (
|
|||||||
success: true,
|
success: true,
|
||||||
result: null,
|
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 {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -37,9 +37,11 @@ interface EMOutgoingMessageConfigGet extends EMMessage {
|
|||||||
|
|
||||||
interface EMOutgoingMessageBarCodeScan extends EMMessage {
|
interface EMOutgoingMessageBarCodeScan extends EMMessage {
|
||||||
type: "bar_code/scan";
|
type: "bar_code/scan";
|
||||||
|
payload: {
|
||||||
title: string;
|
title: string;
|
||||||
description: string;
|
description: string;
|
||||||
alternative_option_label?: string;
|
alternative_option_label?: string;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
interface EMOutgoingMessageBarCodeClose extends EMMessage {
|
interface EMOutgoingMessageBarCodeClose extends EMMessage {
|
||||||
@ -48,7 +50,9 @@ interface EMOutgoingMessageBarCodeClose extends EMMessage {
|
|||||||
|
|
||||||
interface EMOutgoingMessageBarCodeNotify extends EMMessage {
|
interface EMOutgoingMessageBarCodeNotify extends EMMessage {
|
||||||
type: "bar_code/notify";
|
type: "bar_code/notify";
|
||||||
|
payload: {
|
||||||
message: string;
|
message: string;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
interface EMOutgoingMessageMatterCommission extends EMMessage {
|
interface EMOutgoingMessageMatterCommission extends EMMessage {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user