Skip to content

Add barcode scanner external bus messages#6903

Open
TimoPtr wants to merge 1 commit into
mainfrom
feature/barcode_scanner
Open

Add barcode scanner external bus messages#6903
TimoPtr wants to merge 1 commit into
mainfrom
feature/barcode_scanner

Conversation

@TimoPtr
Copy link
Copy Markdown
Member

@TimoPtr TimoPtr commented May 26, 2026

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

  • New or updated tests have been added to cover the changes following the testing guidelines.
  • The code follows the project's code style and best_practices.
  • The changes have been thoroughly tested, and edge cases have been considered.
  • Changes are backward compatible whenever feasible. Any breaking changes are documented in the changelog for users and/or in the code for developers depending on the relevance.

Copilot AI review requested due to automatic review settings May 26, 2026 14:29
@TimoPtr TimoPtr added the WebViewActivity replacement Ongoing work to replace the WebViewActivity in favor of a well tested compose screen using nav. label May 26, 2026
@TimoPtr TimoPtr requested a review from jpelgrom May 26, 2026 14:29
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, and bar_code/close
  • Adds outgoing command factories for bar_code/scan_result and bar_code/aborted
  • Wires barcode messages into FrontendMessageHandler and 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

Comment on lines +104 to +115
/**
* 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) :
Comment on lines +235 to +241
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,
)
Comment on lines +399 to +413
@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()
}
}
Comment on lines +107 to +121
* @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),
),
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is lowercased in the bar code scanner (caller)

@TimoPtr TimoPtr force-pushed the feature/barcode_scanner branch 3 times, most recently from 7dfc793 to 4e0373a Compare May 27, 2026 14:21
@TimoPtr TimoPtr force-pushed the feature/barcode_scanner branch from 4e0373a to 8e19ce3 Compare May 28, 2026 09:59
Copy link
Copy Markdown
Member

@jpelgrom jpelgrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nitpicks about comments, up to you to agree or disagree

Comment on lines +280 to +283
*
* 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).
Copy link
Copy Markdown
Member

@jpelgrom jpelgrom May 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested 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.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Match other description

Suggested change
* 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.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Frontend asked the app to open the barcode scanner overlay.
* Frontend requested the app to open the barcode scanner overlay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed WebViewActivity replacement Ongoing work to replace the WebViewActivity in favor of a well tested compose screen using nav.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants