Skip to content

Add Health Connect nutrition sensor support#6891

Draft
onpremcloudguy wants to merge 1 commit into
home-assistant:mainfrom
onpremcloudguy:feature/health-connect-nutrition-sensor
Draft

Add Health Connect nutrition sensor support#6891
onpremcloudguy wants to merge 1 commit into
home-assistant:mainfrom
onpremcloudguy:feature/health-connect-nutrition-sensor

Conversation

@onpremcloudguy
Copy link
Copy Markdown

Summary

Add a new Health Connect nutrition sensor to the Android companion app.

This change introduces support for syncing the latest Health Connect NutritionRecord into Home Assistant as a sensor entity (health_connect_nutrition) with additional nutrient-related attributes.

Motivation:

  • Extend Health Connect coverage to nutrition data for users already syncing health metrics

Impact:

  • Users can enable and sync nutrition information from Health Connect
  • No Home Assistant Core backend changes are required because mobile app sensor payloads already support additional attributes

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.

Screenshots

N/A - no new custom UI layout was introduced. This PR adds an additional supported Health Connect sensor type within existing system/app flows.

Link to pull request in documentation repositories

User Documentation: home-assistant/companion.home-assistant#N/A
Developer Documentation: home-assistant/developers.home-assistant#N/A

Any other notes

Validation performed locally:

  • ./gradlew :app:testFullDebugUnitTest --tests io.homeassistant.companion.android.sensors.HealthConnectSensorManagerTest
  • ./gradlew ktlintCheck :build-logic:convention:ktlintCheck --continue
  • ./gradlew test was attempted but is currently blocked by pre-existing unrelated local minimal/wear test-classpath issues (for example unresolved Wear/GMS references in existing app/src/test/.../settings/wear/* tests)

Copilot AI review requested due to automatic review settings May 24, 2026 04:12
Copy link
Copy Markdown

@home-assistant home-assistant Bot left a comment

Choose a reason for hiding this comment

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

Hi @SteveHosko

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant home-assistant Bot marked this pull request as draft May 24, 2026 04:12
@home-assistant
Copy link
Copy Markdown

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@TimoPtr
Copy link
Copy Markdown
Member

TimoPtr commented May 26, 2026

You need to update the companion app documentation with the new sensor https://companion.home-assistant.io/docs/core/sensors#health-connect-sensors

Comment on lines +831 to +840
record.energyFromFat?.inKilocalories?.let { attributes["energyFromFat"] = it }
record.totalCarbohydrate?.inGrams?.let { attributes["totalCarbohydrate"] = it }
record.protein?.inGrams?.let { attributes["protein"] = it }
record.totalFat?.inGrams?.let { attributes["totalFat"] = it }
record.sugar?.inGrams?.let { attributes["sugar"] = it }
record.dietaryFiber?.inGrams?.let { attributes["dietaryFiber"] = it }
record.sodium?.inGrams?.let { attributes["sodium"] = it }
record.saturatedFat?.inGrams?.let { attributes["saturatedFat"] = it }
record.transFat?.inGrams?.let { attributes["transFat"] = it }
record.unsaturatedFat?.inGrams?.let { attributes["unsaturatedFat"] = it }
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.

Why only taking those? The API does have more attributes like vitaminA?


val permissions = sensorManager.requiredPermissions(context, HealthConnectSensorManager.nutrition.id)

assertEquals(
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.

Use assertTrue


assertEquals(
true,
permissions.contains(HealthPermission.getReadPermission(NutritionRecord::class)),
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.

I think you can make an exhaustive test by testing that it contains PERMISSION_READ_HEALTH_DATA_IN_BACKGROUND too when backgroundReadAvailable is true.

Copy link
Copy Markdown
Member

@TimoPtr TimoPtr left a comment

Choose a reason for hiding this comment

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

Adding this would require us to update the App Content policy regarding Health. Could you provide a detail use case of this sensor?

Copy link
Copy Markdown
Member

@TimoPtr TimoPtr left a comment

Choose a reason for hiding this comment

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

It lakes the permission in the manifest <uses-permission android:name="android.permission.health.READ_NUTRITION" />

@home-assistant home-assistant Bot marked this pull request as draft May 26, 2026 07:52
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.

3 participants