Skip to content

Declare sensor settings instead of creating them on read - #7289

Open
loganrosen wants to merge 2 commits into
home-assistant:mainfrom
loganrosen:loganrosen-declare-sensor-settings
Open

Declare sensor settings instead of creating them on read#7289
loganrosen wants to merge 2 commits into
home-assistant:mainfrom
loganrosen:loganrosen-declare-sensor-settings

Conversation

@loganrosen

Copy link
Copy Markdown
Contributor

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

  • 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.
  • I have read the Open Home Foundation AI Policy.

Select exactly one option that describes AI usage in this contribution:

  • I have not used AI for this contribution.
  • AI assistance was used for this contribution.
  • AI fully generated the code for this contribution, but I've reviewed and understood it before submitting and will respond without AI during review.

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.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings August 1, 2026 18:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.BasicSensor and 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 Exception here will also catch CancellationException, which can break structured concurrency (cancellation should propagate). Re-throw CancellationException before 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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review details

  • Files reviewed: 21/21 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sensor settings should be declared, not created as a side effect of reading them

2 participants