Add barcode scanner external bus messages#6903
Conversation
There was a problem hiding this comment.
Pull request overview
Adds typed external-bus support for barcode-scanner interactions in the new FrontendScreen pipeline (incoming message models, outgoing command factories, and handler events), with accompanying unit tests.
Changes:
- Introduces incoming external-bus message models for
bar_code/scan,bar_code/notify, andbar_code/close - Adds outgoing command factories for
bar_code/scan_resultandbar_code/aborted - Wires barcode messages into
FrontendMessageHandlerand adds unit tests for parsing/serialization and handler event emission
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| app/src/main/kotlin/io/homeassistant/companion/android/frontend/externalbus/incoming/IncomingExternalBusMessage.kt | Adds typed incoming barcode message models (scan, notify, close) |
| app/src/main/kotlin/io/homeassistant/companion/android/frontend/externalbus/outgoing/CommandMessage.kt | Adds typed outgoing barcode command factories (scan_result, aborted) |
| app/src/main/kotlin/io/homeassistant/companion/android/frontend/handler/FrontendMessageHandler.kt | Maps incoming barcode messages to new handler events |
| app/src/main/kotlin/io/homeassistant/companion/android/frontend/handler/FrontendHandlerEvent.kt | Defines new barcode-related handler events and documentation |
| app/src/main/kotlin/io/homeassistant/companion/android/frontend/FrontendViewModel.kt | Stubs barcode event handling (logging only) until follow-up PR |
| app/src/test/kotlin/io/homeassistant/companion/android/frontend/externalbus/incoming/IncomingExternalBusMessageTest.kt | Adds JSON parsing tests for incoming barcode messages |
| app/src/test/kotlin/io/homeassistant/companion/android/frontend/externalbus/outgoing/CommandMessageTest.kt | Adds serialization tests for outgoing barcode commands |
| app/src/test/kotlin/io/homeassistant/companion/android/frontend/handler/FrontendMessageHandlerTest.kt | Adds handler emission tests for barcode events |
| /** | ||
| * Frontend asked the app to open the barcode scanner overlay. | ||
| * | ||
| * Carries the original message [messageId] (required — the frontend correlates the eventual | ||
| * scan result or cancellation by this id) and the user-facing strings the overlay should display. | ||
| */ | ||
| data class ShowBarcodeScanner( | ||
| val messageId: Int, | ||
| val title: String, | ||
| val description: String, | ||
| val alternativeOptionLabel: String?, | ||
| ) : FrontendHandlerEvent |
| */ | ||
| @Serializable | ||
| @SerialName("bar_code/scan") | ||
| data class BarcodeScanMessage(override val id: Int? = null, val payload: BarcodeScanPayload) : |
| Timber.d("Barcode scan request received with id: ${message.id}") | ||
| FrontendHandlerEvent.ShowBarcodeScanner( | ||
| messageId = message.id ?: -1, | ||
| title = message.payload.title, | ||
| description = message.payload.description, | ||
| alternativeOptionLabel = message.payload.alternativeOptionLabel, | ||
| ) |
| @Test | ||
| fun `Given bar_code scan message without id when messageResults then ShowBarcodeScanner messageId is -1`() = runTest { | ||
| val message = BarcodeScanMessage( | ||
| id = null, | ||
| payload = BarcodeScanPayload(title = "t", description = "d"), | ||
| ) | ||
| every { externalBusRepository.incomingMessages() } returns flowOf(message) | ||
|
|
||
| handler.messageResults().test { | ||
| val result = awaitItem() | ||
| assertTrue(result is FrontendHandlerEvent.ShowBarcodeScanner) | ||
| assertEquals(-1, (result as FrontendHandlerEvent.ShowBarcodeScanner).messageId) | ||
| expectNoEvents() | ||
| } | ||
| } |
| * @param id The id of the originating [io.homeassistant.companion.android.frontend.externalbus.incoming.BarcodeScanMessage] | ||
| * @param rawValue The decoded barcode contents, verbatim | ||
| * @param format The decoded format name, lowercased — `qr_code`, `code_128`, `pdf417`, etc., or | ||
| * `unknown` for formats the frontend does not recognise. | ||
| * | ||
| * @see CommandMessage | ||
| */ | ||
| object BarcodeScanResultMessage { | ||
| operator fun invoke(id: Int, rawValue: String, format: String): OutgoingExternalBusMessage = CommandMessage( | ||
| id = id, | ||
| command = "bar_code/scan_result", | ||
| payload = frontendExternalBusJson.encodeToJsonElement( | ||
| ScanResultPayload(rawValue = rawValue, format = format), | ||
| ), | ||
| ) |
There was a problem hiding this comment.
It is lowercased in the bar code scanner (caller)
7dfc793 to
4e0373a
Compare
4e0373a to
8e19ce3
Compare
jpelgrom
left a comment
There was a problem hiding this comment.
Minor nitpicks about comments, up to you to agree or disagree
| * | ||
| * Will not be sent by the frontend when the device reports | ||
| * [io.homeassistant.companion.android.frontend.externalbus.outgoing.ConfigResult.hasBarCodeScanner] = `0` | ||
| * (Automotive or no camera). |
There was a problem hiding this comment.
It also won't be sent if you don't use a feature requiring a barcode scanner, or the server version is old, or any number of other reasons. I don't think we should mention when the frontend sends the message if it is the start with a generic feature, like here. The frontend will send it whenever it wants and might change.
| * | |
| * Will not be sent by the frontend when the device reports | |
| * [io.homeassistant.companion.android.frontend.externalbus.outgoing.ConfigResult.hasBarCodeScanner] = `0` | |
| * (Automotive or no camera). |
| ) : FrontendHandlerEvent | ||
|
|
||
| /** | ||
| * Frontend asked the app to surface a notification dialog on top of the active scanner. |
There was a problem hiding this comment.
Match other description
| * Frontend asked the app to surface a notification dialog on top of the active scanner. | |
| * Frontend requested the app to display a notification dialog on top of the active scanner. |
| data class ConfigureImprovDevice(val deviceName: String) : FrontendHandlerEvent | ||
|
|
||
| /** | ||
| * Frontend asked the app to open the barcode scanner overlay. |
There was a problem hiding this comment.
| * Frontend asked the app to open the barcode scanner overlay. | |
| * Frontend requested the app to open the barcode scanner overlay. |
Summary
This is the first PR to bring barcode capabilities to the FrontendScreen by adding the external bus messages definition and handling to the new code.
Checklist