Skip to content

Add HealthKit sensors to iOS app#4923

Draft
aero-oli wants to merge 3 commits into
home-assistant:mainfrom
aero-oli:feature/apple-health-sensors
Draft

Add HealthKit sensors to iOS app#4923
aero-oli wants to merge 3 commits into
home-assistant:mainfrom
aero-oli:feature/apple-health-sensors

Conversation

@aero-oli

@aero-oli aero-oli commented Jul 2, 2026

Copy link
Copy Markdown

Summary

Adds optional HealthKit-backed iOS sensors so selected health metrics can be exposed through the existing Home Assistant sensor webhook flow.

This includes:

  • HealthKit authorization and sensor value collection for supported health metrics.
  • A persisted app setting for enabling/disabling HealthKit sensors.
  • Sensors settings UI wiring so the HealthKit group appears alongside existing sensor controls.
  • The required HealthKit entitlement and usage description.
  • Unit coverage for HealthKit sensor encoding/collection behavior and the sensor list view model wiring.

Screenshots

N/A - this uses the existing Sensors settings list UI patterns and does not introduce a new custom screen.

Link to pull request in Documentation repository

Documentation: home-assistant/companion.home-assistant#1368

Any other notes

Validation: CI passed on the fork PR, including lint, unused-string checks, SwiftLint disable checks, and the test job: https://github.com/aero-oli/iOS/actions/runs/28614134149

@aero-oli aero-oli marked this pull request as ready for review July 2, 2026 19:38
Copilot AI review requested due to automatic review settings July 2, 2026 19:38

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

Adds optional Apple Health (HealthKit) backed sensors to the iOS app so selected health metrics can be exposed via the existing Home Assistant sensor webhook flow.

Changes:

  • Introduces a new HealthKitSensor provider with caching and per-metric enablement.
  • Adds a persisted master toggle + cache storage in SettingsStore, and wires a new “Apple Health” section into the Sensors settings UI.
  • Adds HealthKit entitlement + usage description strings, plus unit tests for sensor collection/encoding and settings wiring.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
Tests/Shared/Sensors/HealthKitSensor.test.swift Adds unit tests for HealthKit sensor behavior (availability, caching, enable/disable).
Tests/App/Settings/SensorListViewModelHealthKitTests.swift Adds unit tests for HealthKit toggle behavior in the sensors settings view model.
Sources/Shared/Settings/SettingsStore.swift Persists HealthKit sensors master enable state, “ever enabled” flag, and cached values.
Sources/Shared/Resources/Swiftgen/Strings.swift Adds SwiftGen accessors for new HealthKit-related localized strings.
Sources/Shared/Environment/Environment.swift Adds HealthKit dependency wrapper + registers the new HealthKit sensor provider.
Sources/Shared/API/Webhook/Sensors/HealthKitSensor.swift Implements the HealthKit-backed sensor provider and cache model types.
Sources/App/Settings/Sensors/List/SensorListViewModel.swift Adds HealthKit status state, toggle handling, and hides HealthKit sensors when master toggle is off.
Sources/App/Settings/Sensors/List/SensorListView.swift Adds an Apple Health section with master toggle + status display to the Sensors settings UI.
Sources/App/Resources/Info.plist Adds NSHealthShareUsageDescription.
Sources/App/Resources/en.lproj/Localizable.strings Adds localized strings for the Sensors settings Apple Health section.
Sources/App/Resources/en.lproj/InfoPlist.strings Adds localized NSHealthShareUsageDescription.
HomeAssistant.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved Adds a new SPM lockfile under the .xcodeproj workspace.
HomeAssistant.xcodeproj/project.pbxproj Adds new source/test files to the project and enables the HealthKit capability.
Configuration/Entitlements/App-ios.entitlements Enables the HealthKit entitlement for the iOS app target.

Comment thread Sources/Shared/Environment/Environment.swift Outdated
Comment thread Sources/Shared/API/Webhook/Sensors/HealthKitSensor.swift Outdated
Comment thread Tests/Shared/Sensors/HealthKitSensor.test.swift
Comment thread Tests/Shared/Sensors/HealthKitSensor.test.swift
Comment thread Tests/Shared/Sensors/HealthKitSensor.test.swift
Comment thread Tests/Shared/Sensors/HealthKitSensor.test.swift
Comment thread Sources/Shared/Settings/SettingsStore.swift Outdated
Comment thread HomeAssistant.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved Outdated
Comment thread Sources/Shared/Settings/SettingsStore.swift Outdated
Comment thread Sources/Shared/Settings/SettingsStore.swift Outdated
Comment thread Sources/Shared/Settings/SettingsStore.swift Outdated
Comment thread Sources/App/Settings/Sensors/List/SensorListViewModel.swift Outdated
Comment thread Sources/App/Settings/Sensors/List/SensorListViewModel.swift Outdated
@home-assistant home-assistant Bot marked this pull request as draft July 2, 2026 19:57
@home-assistant

home-assistant Bot commented Jul 2, 2026

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.

@aero-oli aero-oli marked this pull request as ready for review July 2, 2026 20:23
@home-assistant home-assistant Bot requested a review from bgoncal July 2, 2026 20:23
@aero-oli aero-oli requested a review from Copilot July 2, 2026 20:39

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

Copilot reviewed 12 out of 12 changed files in this pull request and generated 5 comments.

Comment on lines +66 to +70
func requestHealthAuthorization() -> Promise<Void> {
Current.healthKit.requestReadAuthorization().get { [weak self] in
self?.healthKitStatus = Current.healthKit.authorizationStatus()
}
}
Comment on lines +524 to +526
public var authorizationStatus: () -> HealthKitSensor.AuthorizationStatus = {
Current.healthKit.isAvailable() ? .available : .unavailable
}
Comment on lines +541 to +548
healthStore.requestAuthorization(toShare: Set<HKSampleType>(), read: types) { success, error in
if let error {
seal.reject(error)
} else if success {
seal.fulfill(())
} else {
seal.reject(HealthKitSensor.HealthKitSensorError.authorizationFailed)
}
Comment on lines +611 to +613
let sample = samples?.first as? HKQuantitySample
seal.fulfill(sample?.quantity.doubleValue(for: .init(from: "count/min")))
}
Comment thread Sources/Shared/API/Webhook/Sensors/HealthKitSensor.swift
@bgoncal

bgoncal commented Jul 2, 2026

Copy link
Copy Markdown
Member

Please do not use AI to answer PR comments, if you/AI implemented it in a specific way, the implementation could be correct, I'm just making questions to evaluate the reasoning.

@bgoncal

bgoncal commented Jul 2, 2026

Copy link
Copy Markdown
Member

Please provide screenshots of how the sensors in app, sensors in HA, and permission request look like.

Current.healthKit.isAvailable() ? .available : .unavailable
}

public var requestReadAuthorization: () -> Promise<Void> = {

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.

Authorization request should not live in Environment, create a HealthKit service that handles health related requests.

#endif
}

public var queryStepCount: (Date, Date) -> Promise<Int?> = { start, end in

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.

Same

#endif
}

public var queryLatestRestingHeartRate: (Date, Date) -> Promise<Double?> = { start, end in

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.

Same

}
}

public var healthKit = HealthKit()

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 should only be available for iOS

@home-assistant home-assistant Bot marked this pull request as draft July 2, 2026 20:58
@codecov

codecov Bot commented Jul 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.77778% with 32 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@23628b9). Learn more about missing BASE report.

Files with missing lines Patch % Lines
Sources/Shared/Environment/Environment.swift 65.15% 23 Missing ⚠️
...s/Shared/API/Webhook/Sensors/HealthKitSensor.swift 88.46% 9 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4923   +/-   ##
=======================================
  Coverage        ?   50.14%           
=======================================
  Files           ?      284           
  Lines           ?    18147           
  Branches        ?        0           
=======================================
  Hits            ?     9099           
  Misses          ?     9048           
  Partials        ?        0           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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