Declare sensor settings instead of creating them on read - #7289
Open
loganrosen wants to merge 2 commits into
Open
Declare sensor settings instead of creating them on read#7289loganrosen wants to merge 2 commits into
loganrosen wants to merge 2 commits into
Conversation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR removes the “write-on-read” side effect for sensor settings by declaring settings alongside each sensor definition and having the repository merge those declarations with any persisted overrides when settings are read. This aligns settings behavior with the existing “default when missing; only persist on actual change” model used for sensors, improving discovery and reducing scattered default logic.
Changes:
- Add declarative setting definitions to
SensorManager.BasicSensorand migrate multiple sensor managers to use the declared settings instead of persisting defaults during reads. - Update the sensor settings persistence layer to support field-specific updates/upserts and a “get-or-initialize” flow for cases like BLE UUID initialization.
- Add/adjust unit tests to validate declaration behavior, merge logic, and persistence semantics.
Show a summary per file
| File | Description |
|---|---|
| wear/src/main/kotlin/io/homeassistant/companion/android/util/PreviewSensorManager.kt | Updates preview repository stub for renamed/added settings APIs. |
| common/src/main/kotlin/io/homeassistant/companion/android/database/sensor/SensorDao.kt | Adds setting value lookup and introduces upsert/get-or-initialize helpers for settings. |
| common/src/main/kotlin/io/homeassistant/companion/android/common/sensors/SensorRepository.kt | Updates repository API to distinguish dynamic settings and adds get-or-initialize value API. |
| common/src/main/kotlin/io/homeassistant/companion/android/common/sensors/SensorRepositoryImpl.kt | Merges declared setting definitions with stored overrides and routes persistence through upsert helpers. |
| common/src/main/kotlin/io/homeassistant/companion/android/common/sensors/SensorManager.kt | Adds per-sensor setting declarations and removes implicit persistence from setting reads. |
| common/src/main/kotlin/io/homeassistant/companion/android/common/sensors/NextAlarmManager.kt | Declares settings for the sensor and stops persisting defaults on read. |
| common/src/main/kotlin/io/homeassistant/companion/android/common/sensors/NetworkSensorManager.kt | Declares settings and updates dynamic-setting persistence for BSSID replacement. |
| common/src/main/kotlin/io/homeassistant/companion/android/common/sensors/LastUpdateManager.kt | Declares the toggle setting and persists generated intent settings as dynamic settings. |
| common/src/main/kotlin/io/homeassistant/companion/android/common/sensors/LastRebootSensorManager.kt | Declares deadband setting and reads via declared setting APIs instead of persisting on read. |
| common/src/main/kotlin/io/homeassistant/companion/android/common/sensors/BluetoothSensorManager.kt | Declares BLE/beacon settings and adds initialization path for BLE UUID. |
| common/src/main/kotlin/io/homeassistant/companion/android/common/sensors/BatterySensorManager.kt | Declares divisor settings and removes default-persist-on-read calls. |
| app/src/main/kotlin/io/homeassistant/companion/android/settings/sensor/SensorDetailViewModel.kt | Switches setting persistence to value-only update API. |
| app/src/main/kotlin/io/homeassistant/companion/android/sensors/NotificationListenerSensorManager.kt | Declares notification-related settings and removes refresh-time settings persistence. |
| app/src/main/kotlin/io/homeassistant/companion/android/sensors/GeocodeSensorManager.kt | Declares geocode settings and removes persistence-on-read behavior. |
| app/src/full/kotlin/io/homeassistant/companion/android/sensors/LocationSensorManager.kt | Declares multiple location settings and replaces persistence-on-read with declared setting reads/updates. |
| common/src/test/kotlin/io/homeassistant/companion/android/database/sensor/SensorDaoTest.kt | Adds unit tests for new DAO helper behaviors (upsert and get-or-initialize). |
| common/src/test/kotlin/io/homeassistant/companion/android/common/sensors/SensorRepositoryImplTest.kt | Adds repository tests for declaration merge behavior and persistence paths. |
| common/src/test/kotlin/io/homeassistant/companion/android/common/sensors/BluetoothSensorManagerTest.kt | Tests BLE UUID initialization is persisted once and reused. |
| app/src/testFull/kotlin/io/homeassistant/companion/android/sensors/LocationSensorManagerTest.kt | Validates declared location settings exist on sensor definitions. |
| app/src/test/kotlin/io/homeassistant/companion/android/sensors/NotificationListenerSensorManagerTest.kt | Updates tests to assert settings are declared and not persisted during refresh updates. |
| app/src/test/kotlin/io/homeassistant/companion/android/sensors/GeocodeSensorManagerTest.kt | Adds test asserting geocode settings are declared. |
Review details
Suppressed comments (1)
app/src/main/kotlin/io/homeassistant/companion/android/settings/sensor/SensorDetailViewModel.kt:348
- Catching
Exceptionhere will also catchCancellationException, which can break structured concurrency (cancellation should propagate). Re-throwCancellationExceptionbefore logging other exceptions.
try {
sensorManager?.requestSensorUpdate()
} catch (e: Exception) {
Timber.e(e, "Exception while requesting update for sensor $sensorId")
}
- Files reviewed: 21/21 changed files
- Comments generated: 2
- Review effort level: Lite
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Sensor settings were previously created in the database as a side effect of reading them, which made discovery mutate state and spread default definitions across call sites. This declares each sensor's settings alongside its sensor definition and merges those declarations with persisted value and enabled overrides when settings are read.
Dynamic settings remain persisted normally, while first edits to declared settings and Bluetooth UUID initialization use transactional field-specific operations so concurrent updates cannot overwrite each other. This keeps existing user values compatible while allowing declaration metadata and defaults to evolve with the app.
Fixes #7225.
Checklist
Select exactly one option that describes AI usage in this contribution:
Any other notes
The declaration-only settings flow was also exercised on an API 35 emulator by editing a setting through the Compose UI, force-stopping the app, and confirming the persisted override after a fresh process launch.