Files
iOS/Sources/Shared/API/HAAPI.swift
Ryan Warner c3fd24aa0b Adds iOS Live Activities support (#4444)
## Summary

> For architecture decisions, data model details, iOS version strategy,
push token flow, and rate limiting — see

[technical-brief.pdf](https://github.com/user-attachments/files/26280274/technical-brief.pdf)

Adds iOS Live Activities support, letting Home Assistant automations
push real-time state to the Lock Screen — washing machine countdowns, EV
charging progress, delivery tracking, alarm states, or anything
time-sensitive that benefits from glanceable visibility without
unlocking the phone.

When an automation sends a notification with `live_update: true` in the
data payload, the companion app starts a Live Activity instead of (or in
addition to) a standard notification banner. Subsequent pushes with the
same `tag` update it in-place silently. `clear_notification` + `tag`
ends it.

Field names (`tag`, `title`, `message`, `progress`, `progress_max`,
`chronometer`, `when`, `when_relative`, `notification_icon`,
`notification_icon_color`) are intentionally shared with Android's Live
Notifications API. Both platforms use the same `live_update: true`
trigger — a single YAML block targets iOS 17.2+ and Android 16+ with no
platform-specific keys.

```yaml
data:
  title: "Washing Machine"
  message: "Cycle in progress"
  data:
    tag: washer_cycle
    live_update: true           # Android 16+ and iOS 17.2+
    progress: 2700
    progress_max: 3600
    chronometer: true
    when: 2700
    when_relative: true
    notification_icon: mdi:washing-machine
    notification_icon_color: "#2196F3"
```

**New files:**
- `Sources/Shared/LiveActivity/HALiveActivityAttributes.swift` — the
`ActivityAttributes` type. Field names match the Android payload spec.
**Struct name and `CodingKeys` are wire-format frozen** — APNs
push-to-start payloads reference the Swift type name directly.
- `Sources/Shared/LiveActivity/LiveActivityRegistry.swift` — Swift
`actor` managing `Activity<HALiveActivityAttributes>` lifecycle. Uses a
reservation pattern to prevent duplicate activities when two pushes with
the same `tag` arrive simultaneously.
-
`Sources/Shared/Notifications/NotificationCommands/HandlerLiveActivity.swift`
— start/update and end `NotificationCommandHandler` implementations,
guarded against the `PushProvider` extension process where ActivityKit
is unavailable.
- `Sources/Extensions/Widgets/LiveActivity/` — `ActivityConfiguration`
wrapper, Lock Screen / StandBy view, and compact / minimal / expanded
Dynamic Island views.
- `Sources/App/Settings/LiveActivity/LiveActivitySettingsView.swift` —
activity status, active list, privacy disclosure, and 11 debug scenarios
for pre-server-side testing.

**Modified files:**
- `Widgets.swift` — registers `HALiveActivityConfiguration` in all three
`WidgetBundle` variants behind `#available(iOSApplicationExtension 17.2,
*)`
- `NotificationsCommandManager.swift` — registers new handlers;
`HandlerClearNotification` now also ends a matching Live Activity when
`tag` is present
- `HAAPI.swift` — adds `supports_live_activities`,
`supports_live_activities_frequent_updates`,
`live_activity_push_to_start_token`,
`live_activity_push_to_start_apns_environment` to registration payload
under a single `#available(iOS 17.2, *)` check
- `AppDelegate.swift` — reattaches surviving activities at launch and
starts observing push-to-start tokens under a single `#available(iOS
17.2, *)` check
- `Info.plist` — `NSSupportsLiveActivities` +
`NSSupportsLiveActivitiesFrequentUpdates`
- `SettingsItem.swift` / `SettingsView.swift` — Live Activities settings
row is gated behind `Current.isTestFlight` and shows a `BetaLabel` badge

**Tests:** 
 - *Unit Tests*
- 45 new tests (36 handler tests, 9 command manager routing tests). All
490 existing tests continue to pass.
 - *Device Tests*
- I tried to test with my physical device, however I do not have a paid
account. Turns out I could not deploy without disabling a lot of
entitlements for compilation. Even so, once I did get it deployed and
running Live Activities wouldn't show unless some of those settings that
I disable were re-enable and leaving me in a particular spot.
   - I mainly tested with the simulator which is what is shown below

## Screenshots


![full-debug-scenarios](https://github.com/user-attachments/assets/b6f61b15-8f41-4fb5-b89d-75b5de719056)

## Link to pull request in Documentation repository

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

## Link to pull request in push relay repository

Relay server: home-assistant/mobile-apps-fcm-push#278

## Link to pull request in HA core

Core: home-assistant/core#166072

## Any other notes

**iOS version gating at 17.2.** The entire feature is gated at
`#available(iOS 17.2, *)` — this is the minimum for push-to-start (the
primary server-side start mechanism). A single availability check now
covers all Live Activity APIs: `supports_live_activities`,
`frequentPushesEnabled`, push-to-start token, and all ActivityKit usage.
This eliminates the nested 16.2/17.2 check pattern.

**Push-to-start (iOS 17.2+) is client-complete.** The token is observed,
stored in Keychain, and included in registration payloads. All companion
server-side PRs are now open — relay server at
home-assistant/mobile-apps-fcm-push#278 and HA core webhook handlers at
home-assistant/core#166072. The relay server uses FCM's native
`apns.liveActivityToken` support (Firebase Admin SDK v13.5.0+) — no
custom APNs client or credentials needed.

> **Server-side work** — all PRs now open:
> - ~~`supports_live_activities` field handling in device registration~~
✓ home-assistant/core#166072
> - ~~`mobile_app_live_activity_token` webhook handler~~ ✓
home-assistant/core#166072
> - ~~`mobile_app_live_activity_dismissed` webhook handler~~ ✓
home-assistant/core#166072
> - ~~Relay server: Live Activity delivery via FCM
`apns.liveActivityToken`~~ ✓ home-assistant/mobile-apps-fcm-push#278
> - ~~`notify.py` routing: includes Live Activity APNs token alongside
FCM token~~ ✓ home-assistant/core#166072

**Live Activities entry in Settings is gated behind TestFlight.** The
settings row only appears when `Current.isTestFlight` is true,
preventing it from surfacing in a release build before the feature is
fully tested. A `BetaLabel` badge is shown alongside the row title.

**iPad:** `areActivitiesEnabled` is always `false` on iPad — Apple
system restriction. The Settings screen shows "Not available on iPad."
The registry silently no-ops. HA receives `supports_live_activities:
false` in the device registration for iPad.

**`HALiveActivityAttributes` is frozen post-ship.** The struct name
appears as `attributes-type` in APNs push-to-start payloads. Renaming it
silently breaks all remote starts. The `ContentState` `CodingKeys` are
equally frozen — only additions are safe. Both have comments in the
source calling this out.

**The debug section in Settings is intentional.** Gated behind `#if
DEBUG` so it only appears in debug builds — it never ships to TestFlight
or the App Store. It exercises the full ActivityKit lifecycle without
requiring the server-side chain.

**`UNUserNotificationCenter` in tests.** The `clear_notification` +
`tag` → Live Activity dismissal path is covered by code review rather
than a unit test. `HandlerClearNotification` calls
`UNUserNotificationCenter.current().removeDeliveredNotifications`
synchronously, which requires a real app bundle and throws
`NSInternalInconsistencyException` in the XCTest host. A comment in the
test file explains this.

**Rate limiting on iOS 18.** Apple throttles Live Activity updates to
~15 seconds between renders. Automations should trigger on state change
events, not polling timers.

**Related:**
- Community discussion:
https://github.com/orgs/home-assistant/discussions/84
- Android companion reference: https://github.com/home-assistant/android
- Roadmap: https://github.com/home-assistant/roadmap/issues/52

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Bruno Pantaleão Gonçalves <5808343+bgoncal@users.noreply.github.com>
2026-04-07 14:50:49 +02:00

1180 lines
45 KiB
Swift

import Alamofire
import CoreLocation
import Foundation
import HAKit
import Intents
import ObjectMapper
import PromiseKit
import RealmSwift
import UIKit
import Version
#if os(iOS)
import Reachability
#endif
#if os(iOS) && canImport(ActivityKit)
import ActivityKit
#endif
public class HomeAssistantAPI {
public enum APIError: Error, Equatable {
case managerNotAvailable
case invalidResponse
case cantBuildURL
case notConfigured
case updateNotPossible
case mobileAppComponentNotLoaded
case mustUpgradeHomeAssistant(current: Version, minimum: Version)
case noAPIAvailable
case unknown
}
private struct TokenFetchFailure: LocalizedError {
let underlyingType: String
var errorDescription: String? {
"Token fetch failed (\(underlyingType))"
}
}
public static let didConnectNotification = Notification.Name(rawValue: "HomeAssistantAPIConnected")
public private(set) var manager: Alamofire.Session!
public static let unauthenticatedManager: Alamofire.Session = configureSessionManager()
public let tokenManager: TokenManager
public var server: Server
public internal(set) var connection: HAConnection
public static var clientVersionDescription: String {
"\(AppConstants.version) (\(AppConstants.build))"
}
public static var userAgent: String {
// This matches Alamofire's generated string, for consistency with the past
let bundle = AppConstants.BundleID
let appVersion = AppConstants.version
let appBuild = AppConstants.build
let osNameVersion: String = {
let version = ProcessInfo.processInfo.operatingSystemVersion
let versionString = "\(version.majorVersion).\(version.minorVersion).\(version.patchVersion)"
let notCircularReferenceWrapper = DeviceWrapper()
let osName = notCircularReferenceWrapper.systemName()
return "\(osName) \(versionString)"
}()
return "Home Assistant/\(appVersion) (\(bundle); build:\(appBuild); \(osNameVersion))"
}
/// "Mobile/BUILD_NUMBER" is what CodeMirror sniffs for to decide iOS or not; other things likely look for Safari
public static var applicationNameForUserAgent: String {
HomeAssistantAPI.userAgent + " Mobile/HomeAssistant, like Safari"
}
/// Initialize an API object with an authenticated tokenManager.
public init(server: Server, urlConfig: URLSessionConfiguration = .default) {
self.server = server
let tokenManager = TokenManager(server: server)
self.tokenManager = tokenManager
#if !os(watchOS)
// Create URLSession for HAKit REST API calls with certificate handling
Current.Log.info("[mTLS] Creating HAKit URLSession for server: \(server.info.name)")
Current.Log.info("[mTLS] Has client certificate: \(server.info.connection.clientCertificate != nil)")
Current.Log.info("[mTLS] Has security exceptions: \(server.info.connection.securityExceptions.hasExceptions)")
let hakitURLSession: URLSession
if server.info.connection.clientCertificate != nil || server.info.connection.securityExceptions.hasExceptions {
// Use HAKit's certificate provider protocol
Current.Log.info("[mTLS] Using HAKit certificate provider")
let certificateProvider = HomeAssistantCertificateProvider(server: server)
let delegate = HAURLSessionDelegate(certificateProvider: certificateProvider)
let configuration = URLSessionConfiguration.ephemeral
hakitURLSession = URLSession(configuration: configuration, delegate: delegate, delegateQueue: nil)
} else {
Current.Log.info("[mTLS] Using default URLSession for HAKit")
hakitURLSession = URLSession(configuration: .ephemeral)
}
#else
let hakitURLSession = URLSession(configuration: .ephemeral)
#endif
self.connection = HAKit.connection(
configuration: .init(
connectionInfo: {
do {
if let activeURL = server.info.connection.activeURL() {
#if !os(watchOS)
// Prepare client identity (SecIdentity) for mTLS if configured
let clientIdentityProvider: HAConnectionInfo.ClientIdentityProvider?
if let clientCert = server.info.connection.clientCertificate {
clientIdentityProvider = {
try? ClientCertificateManager.shared.retrieveIdentity(for: clientCert)
}
} else {
clientIdentityProvider = nil
}
return try .init(
url: activeURL,
userAgent: HomeAssistantAPI.userAgent,
evaluateCertificate: { secTrust, completion in
completion(
Swift.Result<Void, Error> {
try server.info.connection.securityExceptions.evaluate(secTrust)
}
)
},
clientIdentity: clientIdentityProvider
)
#else
return try .init(
url: activeURL,
userAgent: HomeAssistantAPI.userAgent,
evaluateCertificate: { secTrust, completion in
completion(
Swift.Result<Void, Error> {
try server.info.connection.securityExceptions.evaluate(secTrust)
}
)
}
)
#endif
} else {
Current.clientEventStore.addEvent(.init(
text: "No active URL available to interact with API, please check if you have internal or external URL available, for internal URL you need to specify your network SSID otherwise for security reasons it won't be available.",
type: .networkRequest
))
Current.Log.error("activeURL was not available when HAAPI called initializer")
return nil
}
} catch {
Current.Log.error("couldn't create connection info: \(error)")
return nil
}
},
fetchAuthToken: { completion in
tokenManager.bearerToken.done {
completion(.success($0.0))
}.catch { error in
let errorType = String(reflecting: type(of: error))
let errorDescription = String(describing: error)
Current.Log
.error("HAKit token fetch failed with error type: \(errorType), error: \(errorDescription)")
let underlyingInfo = "\(errorType): \(errorDescription)"
completion(.failure(TokenFetchFailure(underlyingType: underlyingInfo)))
}
}
),
urlSession: hakitURLSession
)
#if !os(watchOS)
// Use custom delegate that supports client certificates (mTLS)
let sessionDelegate: SessionDelegate = server.info.connection.clientCertificate != nil
? ClientCertificateSessionDelegate(server: server)
: SessionDelegate()
#else
let sessionDelegate = SessionDelegate()
#endif
let manager = HomeAssistantAPI.configureSessionManager(
urlConfig: urlConfig,
delegate: sessionDelegate,
interceptor: newInterceptor(),
trustManager: newServerTrustManager()
)
self.manager = manager
removeOldDownloadDirectory()
Current.sensors.register(observer: self)
}
convenience init?() {
if let server = Current.servers.all.first {
self.init(server: server, urlConfig: .default)
} else {
return nil
}
}
private static func configureSessionManager(
urlConfig: URLSessionConfiguration = .default,
delegate: SessionDelegate = SessionDelegate(),
interceptor: Interceptor = .init(),
trustManager: ServerTrustManager? = nil
) -> Session {
let configuration = urlConfig
var headers = configuration.httpAdditionalHeaders ?? [:]
headers["User-Agent"] = Self.userAgent
configuration.httpAdditionalHeaders = headers
return Alamofire.Session(
configuration: configuration,
delegate: delegate,
interceptor: interceptor,
serverTrustManager: trustManager
)
}
private func newInterceptor() -> Interceptor {
.init(
adapters: [
ServerRequestAdapter(server: server),
], retriers: [
], interceptors: [
tokenManager.authenticationInterceptor,
RetryPolicy(),
]
)
}
private func newServerTrustManager() -> ServerTrustManager {
CustomServerTrustManager(server: server)
}
public func VideoStreamer() -> MJPEGStreamer {
#if !os(watchOS)
let delegate: SessionDelegate = server.info.connection.clientCertificate != nil
? MJPEGCertificateSessionDelegate(server: server)
: MJPEGStreamerSessionDelegate()
#else
let delegate = MJPEGStreamerSessionDelegate()
#endif
return MJPEGStreamer(manager: HomeAssistantAPI.configureSessionManager(
delegate: delegate,
interceptor: newInterceptor(),
trustManager: newServerTrustManager()
))
}
public enum ConnectReason {
case cold
case warm
case periodic
case background
var updateSensorTrigger: LocationUpdateTrigger {
switch self {
case .cold, .warm, .background:
return .Launch
case .periodic:
return .Periodic
}
}
}
public func Connect(reason: ConnectReason) -> Promise<Void> {
Current.Log.info("running connect for \(reason)")
// websocket
connection.connect()
return firstly { () -> Promise<Void> in
guard !Current.isAppExtension else {
Current.Log.info("skipping registration changes in extension")
return Promise<Void>.value(())
}
return updateRegistration().asVoid().recover { [self] error -> Promise<Void> in
switch error as? WebhookError {
case .unmappableValue,
.unexpectedType,
.unacceptableStatusCode(404),
.unacceptableStatusCode(410):
// cloudhook will send a 404 for deleted
// ha directly will send a 200 with an empty body for deleted
let message = "Integration is missing; registering."
Current.clientEventStore
.addEvent(ClientEvent(text: message, type: .networkRequest, payload: [
"error": String(describing: error),
]))
return register()
case .unregisteredIdentifier,
.unacceptableStatusCode,
.replaced,
.none:
// not a WebhookError, or not one we think requires reintegration
Current.Log.info("not re-registering, but failed to update registration: \(error)")
throw error
}
}
}.then { [self] () -> Promise<Void> in
var promises: [Promise<Void>] = []
if !Current.isAppExtension {
promises.append(getConfig())
promises.append(Current.modelManager.fetch(apis: [self]))
promises.append(updateComplications(passively: false).asVoid())
}
promises.append(UpdateSensors(trigger: reason.updateSensorTrigger).asVoid())
return when(fulfilled: promises).asVoid()
}.get { _ in
NotificationCenter.default.post(
name: Self.didConnectNotification,
object: nil,
userInfo: nil
)
}
}
public func CreateEvent(eventType: String, eventData: [String: Any]) -> Promise<Void> {
let intent = FireEventIntent(eventName: eventType, payload: eventData)
INInteraction(intent: intent, response: nil).donate(completion: nil)
return Current.webhooks.sendEphemeral(
server: server,
request: .init(type: "fire_event", data: [
"event_type": eventType,
"event_data": eventData,
])
)
}
public func temporaryDownloadFileURL(appropriateFor downloadingURL: URL? = nil) -> URL? {
let url = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
// using a random file name so we always have one, see https://github.com/home-assistant/iOS/issues/1068
.appendingPathComponent(UUID().uuidString, isDirectory: false)
if let downloadingURL {
return url.appendingPathExtension(downloadingURL.pathExtension)
} else {
return url
}
}
private func removeOldDownloadDirectory() {
let fileManager = FileManager.default
if let downloadDataDir = fileManager.containerURL(
forSecurityApplicationGroupIdentifier: AppConstants.AppGroupID
)?.appendingPathComponent("downloadedData", isDirectory: true) {
try? fileManager.removeItem(at: downloadDataDir)
}
}
public func DownloadDataAt(url: URL, needsAuth: Bool) -> Promise<URL> {
Promise { seal in
var finalURL = url
let dataManager: Alamofire.Session = needsAuth ? self.manager : Self.unauthenticatedManager
if needsAuth {
guard let activeURL = server.info.connection.activeURL() else {
seal.reject(ServerConnectionError.noActiveURL(server.info.name))
return
}
if !url.absoluteString.hasPrefix(activeURL.absoluteString) {
Current.Log.verbose("URL does not contain base URL, prepending base URL to \(url.absoluteString)")
finalURL = activeURL.appendingPathComponent(url.absoluteString)
}
Current.Log.verbose("Data download needs auth!")
}
guard let downloadPath = temporaryDownloadFileURL(appropriateFor: finalURL) else {
Current.Log.error("Unable to get download path!")
seal.reject(APIError.cantBuildURL)
return
}
let destination: DownloadRequest.Destination = { _, _ in
(downloadPath, [.removePreviousFile, .createIntermediateDirectories])
}
dataManager.download(finalURL, to: destination).validate().responseData { downloadResponse in
switch downloadResponse.result {
case .success:
seal.fulfill(downloadResponse.fileURL!)
case let .failure(error):
seal.reject(error)
}
}
}
}
public func getConfig() -> Promise<Void> {
let promise: Promise<ConfigResponse> = Current.webhooks.sendEphemeral(
server: server,
request: .init(type: "get_config", data: [:])
)
return promise.done { [self] config in
server.update { serverInfo in
serverInfo.connection.cloudhookURL = config.CloudhookURL
serverInfo.connection.set(address: config.RemoteUIURL, for: .remoteUI)
serverInfo.remoteName = config.LocationName ?? ServerInfo.defaultName
serverInfo.hassDeviceId = config.hassDeviceId
if let version = try? Version(hassVersion: config.Version) {
serverInfo.version = version
}
}
Current.crashReporter.setUserProperty(value: config.Version, name: "HA_Version")
}
}
public func GetLogbook() -> Promise<[LogbookEntry]> {
request(path: "logbook", callingFunctionName: "\(#function)")
}
public func CallService(
domain: String,
service: String,
serviceData: [String: Any],
triggerSource: AppTriggerSource,
shouldLog: Bool = true
) -> Promise<Void> {
let intent = CallServiceIntent(domain: domain, service: service, payload: serviceData)
INInteraction(intent: intent, response: nil).donate(completion: nil)
return Current.webhooks.send(
identifier: .serviceCall,
server: server,
request: .init(type: "call_service", data: [
"domain": domain,
"service": service,
"service_data": serviceData,
])
)
}
public func turnOnScript(scriptEntityId: String, triggerSource: AppTriggerSource) -> Promise<Void> {
CallService(domain: Domain.script.rawValue, service: Service.turnOn.rawValue, serviceData: [
"entity_id": scriptEntityId,
], triggerSource: triggerSource)
}
public func getCameraSnapshot(cameraEntityID: String) -> Promise<UIImage> {
Promise { seal in
guard let queryUrl = server.info.connection.activeAPIURL()?
.appendingPathComponent("camera_proxy/\(cameraEntityID)") else {
seal.reject(ServerConnectionError.noActiveURL(server.info.name))
return
}
_ = manager.request(queryUrl)
.validate()
.responseData { response in
switch response.result {
case let .success(data):
if let image = UIImage(data: data) {
seal.fulfill(image)
}
case let .failure(error):
Current.Log.error("Error when attemping to GetCameraImage(): \(error)")
seal.reject(error)
}
}
}
}
public func register() -> Promise<Void> {
request(
path: "mobile_app/registrations",
callingFunctionName: "\(#function)",
method: .post,
parameters: buildMobileAppRegistration(),
encoding: JSONEncoding.default
).recover { error -> Promise<MobileAppRegistrationResponse> in
if case AFError.responseValidationFailed(reason: .unacceptableStatusCode(code: 404)) = error {
throw APIError.mobileAppComponentNotLoaded
}
throw error
}.done { [server] (resp: MobileAppRegistrationResponse) in
Current.Log.verbose("Registration response \(resp)")
server.update { server in
server.connection.set(address: resp.RemoteUIURL, for: .remoteUI)
server.connection.cloudhookURL = resp.CloudhookURL
server.connection.webhookID = resp.WebhookID
server.connection.webhookSecret = resp.WebhookSecret
}
}
}
public func updateRegistration() -> Promise<MobileAppRegistrationResponse> {
Current.webhooks.sendEphemeral(
server: server,
request: .init(
type: "update_registration",
data: buildMobileAppUpdateRegistration()
)
)
}
public func GetMobileAppConfig() -> Promise<MobileAppConfig> {
firstly { () -> Promise<MobileAppConfig> in
if server.info.version < .actionSyncing {
let old: Promise<MobileAppConfigPush> = requestImmutable(
path: "ios/push",
callingFunctionName: "\(#function)"
)
return old.map { MobileAppConfig(push: $0) }
} else {
return requestImmutable(path: "ios/config", callingFunctionName: "\(#function)")
}
}.recover { error -> Promise<MobileAppConfig> in
if case AFError.responseValidationFailed(reason: .unacceptableStatusCode(code: 404)) = error {
Current.Log.info("ios component is not loaded; pretending there's no push config")
return .value(.init())
}
throw error
}
}
public func StreamCamera(entityId: String) -> Promise<StreamCameraResponse> {
Current.webhooks.sendEphemeral(
server: server,
request: .init(
type: "stream_camera",
data: ["camera_entity_id": entityId]
)
)
}
private func buildMobileAppRegistration() -> [String: Any] {
let ident = mobileAppRegistrationRequestModel()
var json = Mapper().toJSON(ident)
if server.info.version < .canSendDeviceID {
// device_id was added in 0.104, but prior it would error for unknown keys
json.removeValue(forKey: "device_id")
}
return json
}
private func mobileAppRegistrationRequestModel() -> MobileAppRegistrationRequest {
with(MobileAppRegistrationRequest()) {
if let pushID = Current.settingsStore.pushID {
var appData: [String: Any] = [
"push_url": "https://mobile-apps.home-assistant.io/api/sendPushNotification",
"push_token": pushID,
]
#if os(iOS) && canImport(ActivityKit)
if #available(iOS 17.2, *) {
// Advertise Live Activity support so HA can gate the UI and send
// activity push tokens back to the relay server.
// Use areActivitiesEnabled so iPad and users who disabled Live Activities
// in Settings correctly report false.
appData["supports_live_activities"] = ActivityAuthorizationInfo().areActivitiesEnabled
appData["supports_live_activities_frequent_updates"] =
ActivityAuthorizationInfo().frequentPushesEnabled
// Push-to-start token (stored in Keychain at launch, updated via stream).
// The relay server uses this token to start a Live Activity entirely via APNs.
if let pushToStartToken = LiveActivityRegistry.storedPushToStartToken {
appData["live_activity_push_to_start_token"] = pushToStartToken
appData["live_activity_push_to_start_apns_environment"] = Current.apnsEnvironment
}
}
#endif
$0.AppData = appData
}
$0.AppIdentifier = AppConstants.BundleID
$0.AppName = Bundle.main.infoDictionary?["CFBundleDisplayName"] as? String
$0.AppVersion = HomeAssistantAPI.clientVersionDescription
$0.DeviceID = Current.settingsStore.integrationDeviceID
$0.DeviceName = server.info.setting(for: .overrideDeviceName) ?? Current.device.deviceName()
$0.Manufacturer = "Apple"
$0.Model = Current.device.systemModel()
$0.OSName = Current.device.systemName()
$0.OSVersion = Current.device.systemVersion()
$0.SupportsEncryption = true
}
}
private func buildMobileAppUpdateRegistration() -> [String: Any] {
let registerRequest = mobileAppRegistrationRequestModel()
let ident = with(MobileAppUpdateRegistrationRequest()) {
$0.AppData = registerRequest.AppData
$0.AppVersion = registerRequest.AppVersion
$0.DeviceName = registerRequest.DeviceName
$0.Manufacturer = registerRequest.Manufacturer
$0.Model = registerRequest.Model
$0.OSVersion = registerRequest.OSVersion
}
return Mapper().toJSON(ident)
}
public func SubmitLocation(
updateType: LocationUpdateTrigger,
location rawLocation: CLLocation?,
zone: RLMZone?
) -> Promise<Void> {
let update: WebhookUpdateLocation
let location: CLLocation?
let localMetadata = WebhookResponseLocation.localMetdata(
trigger: updateType,
zone: zone
)
switch server.info.setting(for: .locationPrivacy) {
case .exact:
update = .init(trigger: updateType, location: rawLocation, zone: zone)
location = rawLocation
case .zoneOnly:
if updateType == .BeaconRegionEnter {
update = .init(trigger: updateType, usingNameOf: zone)
} else if let rawLocation {
// note this is a different zone than the event - e.g. the zone may be the one we are exiting
update = .init(trigger: updateType, usingNameOf: RLMZone.zone(of: rawLocation, in: server))
} else {
update = .init(trigger: updateType)
}
location = nil
case .never:
update = .init(trigger: updateType)
location = nil
}
return firstly {
let realm = Current.realm()
return when(resolved: realm.reentrantWrite {
let accuracyAuthorization: CLAccuracyAuthorization = CLLocationManager().accuracyAuthorization
realm.add(LocationHistoryEntry(
updateType: updateType,
location: location,
zone: zone,
accuracyAuthorization: accuracyAuthorization,
payload: update.toJSONString(prettyPrint: false) ?? "(unknown)"
))
}).asVoid()
}.map { () -> [String: Any] in
let payloadDict = Mapper<WebhookUpdateLocation>().toJSON(update)
Current.Log.info("Location update payload: \(payloadDict)")
return payloadDict
}.then { [self] payload in
when(
resolved:
UpdateSensors(trigger: updateType, location: location).asVoid(),
Current.webhooks.send(
identifier: .location,
server: server,
request: .init(
type: "update_location",
data: payload,
localMetadata: localMetadata
)
)
)
}.asVoid()
}
public var sharedEventDeviceInfo: [String: String] {
[
"sourceDevicePermanentID": AppConstants.PermanentID,
"sourceDeviceName": server.info.setting(for: .overrideDeviceName) ?? Current.device.deviceName(),
"sourceDeviceID": Current.settingsStore.deviceID,
]
}
public func legacyNotificationActionEvent(
identifier: String,
category: String?,
actionData: Any?,
textInput: String?
) -> (eventType: String, eventData: [String: Any]) {
var eventData: [String: Any] = sharedEventDeviceInfo
eventData["actionName"] = identifier
if let category {
eventData["categoryName"] = category
}
if let actionData {
eventData["action_data"] = actionData
}
if let textInput {
eventData["response_info"] = textInput
eventData["textInput"] = textInput
}
return (eventType: "ios.notification_action_fired", eventData: eventData)
}
public func mobileAppNotificationActionEvent(
identifier: String,
category: String?,
actionData: Any?,
textInput: String?
) -> (eventType: String, eventData: [String: Any]) {
var eventData = [String: Any]()
eventData["action"] = identifier
if let actionData {
eventData["action_data"] = actionData
}
if let textInput {
eventData["reply_text"] = textInput
}
return (eventType: "mobile_app_notification_action", eventData: eventData)
}
public func actionEvent(
actionID: String,
actionName: String,
source: AppTriggerSource
) -> (eventType: String, eventData: [String: String]) {
var eventData = sharedEventDeviceInfo
eventData["actionName"] = actionName
eventData["actionID"] = actionID
eventData["triggerSource"] = source.description
return (eventType: "ios.action_fired", eventData: eventData)
}
public func actionScene(
actionID: String,
source: AppTriggerSource
) -> (serviceDomain: String, serviceName: String, serviceData: [String: String]) {
(
serviceDomain: Domain.scene.rawValue,
serviceName: Service.turnOn.rawValue,
serviceData: ["entity_id": actionID]
)
}
public func tagEvent(
tagPath: String
) -> (eventType: String, eventData: [String: String]) {
var eventData: [String: String] = sharedEventDeviceInfo
eventData["tag_id"] = tagPath
if server.info.version < .tagWebhookAvailable {
eventData["device_id"] = Current.settingsStore.integrationDeviceID
}
return (eventType: "tag_scanned", eventData: eventData)
}
@available(watchOS, unavailable)
public func zoneStateEvent(
region: CLRegion,
state: CLRegionState,
zone: RLMZone
) -> (eventType: String, eventData: [String: Any]) {
var eventData: [String: Any] = sharedEventDeviceInfo
eventData["zone"] = zone.entityId
if region.identifier.contains("@"), let subId = region.identifier.split(separator: "@").last {
eventData["multi_region_zone_id"] = String(subId)
}
if state == .inside {
return (eventType: "ios.zone_entered", eventData: eventData)
} else {
return (eventType: "ios.zone_exited", eventData: eventData)
}
}
public func shareEvent(
entered: String,
url: URL?,
text: String?
) -> (eventType: String, eventData: [String: String]) {
var eventData = sharedEventDeviceInfo
eventData["entered"] = entered
eventData["url"] = url?.absoluteString
eventData["text"] = text
return (
eventType: "mobile_app.share",
eventData: eventData
)
}
public struct PushActionInfo: ImmutableMappable {
public var identifier: String
public var category: String?
public var textInput: String?
public var actionData: Any?
public init?(response: UNNotificationResponse) {
guard response.actionIdentifier != UNNotificationDefaultActionIdentifier else {
return nil
}
self.identifier = UNNotificationContent.uncombinedAction(from: response.actionIdentifier)
self.category = response.notification.request.content.categoryIdentifier
self.actionData = response.notification.request.content.userInfo["homeassistant"]
self.textInput = (response as? UNTextInputNotificationResponse)?.userText
}
public init(map: ObjectMapper.Map) throws {
self.identifier = try map.value("identifier")
self.category = try? map.value("category")
self.textInput = try? map.value("textInput")
self.actionData = try? map.value("actionData")
}
public func mapping(map: ObjectMapper.Map) {
identifier >>> map["identifier"]
category >>> map["category"]
textInput >>> map["textInput"]
actionData >>> map["actionData"]
}
}
public func handlePushAction(for info: PushActionInfo) -> Promise<Void> {
let actions = [
legacyNotificationActionEvent(
identifier: info.identifier,
category: info.category,
actionData: info.actionData,
textInput: info.textInput
),
mobileAppNotificationActionEvent(
identifier: info.identifier,
category: info.category,
actionData: info.actionData,
textInput: info.textInput
),
]
return when(resolved: actions.map { action -> Promise<Void> in
Current.Log.verbose("Sending action: \(action.eventType) payload: \(action.eventData)")
return CreateEvent(eventType: action.eventType, eventData: action.eventData)
}).asVoid()
}
public func HandleAction(actionID: String, source: AppTriggerSource) -> Promise<Void> {
guard let action = Current.realm().object(ofType: Action.self, forPrimaryKey: actionID) else {
Current.Log.error("couldn't find action with id \(actionID)")
return .init(error: HomeAssistantAPI.APIError.cantBuildURL)
}
let intent = PerformActionIntent(action: action)
INInteraction(intent: intent, response: nil).donate(completion: nil)
switch action.triggerType {
case .event:
let actionInfo = actionEvent(actionID: action.ID, actionName: action.Name, source: source)
Current.Log.verbose("Sending action: \(actionInfo.eventType) payload: \(actionInfo.eventData)")
return CreateEvent(
eventType: actionInfo.eventType,
eventData: actionInfo.eventData
)
case .scene:
let serviceInfo = actionScene(actionID: action.ID, source: source)
Current.Log.verbose("activating scene: \(action.ID)")
return CallService(
domain: serviceInfo.serviceDomain,
service: serviceInfo.serviceName,
serviceData: serviceInfo.serviceData,
triggerSource: source
)
}
}
public func executeActionForDomainType(domain: Domain, entityId: String, state: String) -> Promise<Void> {
var request: HATypedRequest<HAResponseVoid>?
// Lock requires state-aware action
if domain == .lock {
guard let state = Domain.State(rawValue: state) else { return .value }
switch state {
case .unlocking, .unlocked, .opening:
request = .lockLock(entityId: entityId)
case .locked, .locking:
request = .unlockLock(entityId: entityId)
default:
break
}
} else {
// Use domain's main action for all other domains
request = .executeMainAction(domain: domain, entityId: entityId)
}
if let request {
return connection.send(request).promise
.map { _ in () }
} else {
return .value
}
}
public func registerSensors() -> Promise<Void> {
firstly {
Current.sensors.sensors(reason: .registration, server: server).map(\.sensors)
}.get { sensors in
Current.Log.verbose("Registering sensors \(sensors.map(\.UniqueID))")
}.thenMap { [server] sensor in
Current.webhooks.send(server: server, request: .init(type: "register_sensor", data: sensor.toJSON()))
}.tap { result in
Current.Log.info("finished registering sensors: \(result)")
}.asVoid()
}
public func UpdateSensors(
trigger: LocationUpdateTrigger,
location: CLLocation? = nil
) -> Promise<Void> {
UpdateSensors(trigger: trigger, limitedTo: nil, location: location)
}
func UpdateSensors(
trigger: LocationUpdateTrigger,
limitedTo: [SensorProvider.Type]?,
location: CLLocation? = nil
) -> Promise<Void> {
firstly {
Current.sensors.sensors(
reason: .trigger(trigger.rawValue),
limitedTo: limitedTo,
location: location,
server: server
)
}.map { sensorResponse -> (SensorResponse, [[String: Any]]) in
Current.Log.info("updating sensors \(sensorResponse.sensors.map { $0.UniqueID ?? "unknown" })")
let mapper = Mapper<WebhookSensor>(
context: WebhookSensorContext(update: true),
shouldIncludeNilValues: false
)
return (sensorResponse, mapper.toJSONArray(sensorResponse.sensors))
}.then { [server] _, payload -> Promise<Void> in
if payload.isEmpty {
Current.Log.info("skipping network request for unchanged sensor update")
return .value(())
} else {
return Current.webhooks.send(
identifier: .updateSensors,
server: server,
request: .init(type: "update_sensor_states", data: payload)
)
}
}
}
#if os(iOS)
public enum ManualUpdateType {
case userRequested
case appOpened
case programmatic
var allowsTemporaryAccess: Bool {
switch self {
case .userRequested, .appOpened: return true
case .programmatic: return false
}
}
}
public static func manuallyUpdate(
applicationState: UIApplication.State,
type: ManualUpdateType
) -> Promise<Void> {
Current.backgroundTask(withName: BackgroundTask.manualLocationUpdate.rawValue) { _ in
firstly { () -> Guarantee<Void> in
Guarantee { seal in
let locationManager = CLLocationManager()
guard locationManager.accuracyAuthorization != .fullAccuracy else {
// already have full accuracy, don't need to request
return seal(())
}
guard type.allowsTemporaryAccess else {
return seal(())
}
Current.Log.info("requesting full accuracy for manual update")
locationManager.requestTemporaryFullAccuracyAuthorization(
withPurposeKey: "TemporaryFullAccuracyReasonManualUpdate"
) { error in
Current.Log.info("got temporary full accuracy result: \(String(describing: error))")
withExtendedLifetime(locationManager) {
seal(())
}
}
}
}.then { () -> Promise<Void> in
func updateWithoutLocation() -> Promise<Void> {
when(fulfilled: Current.apis.map { $0.UpdateSensors(trigger: .Manual) })
}
if Current.settingsStore.isLocationEnabled(for: applicationState) {
return firstly {
Current.location.oneShotLocation(.Manual, nil)
}.then { location in
when(fulfilled: Current.apis.map { api in
api.SubmitLocation(updateType: .Manual, location: location, zone: nil)
}).asVoid()
}.recover { error -> Promise<Void> in
if error is CLError || error is OneShotError {
Current.Log.info("couldn't get location, sending remaining sensor data")
return updateWithoutLocation()
} else {
throw error
}
}
} else {
return updateWithoutLocation()
}
}
}
}
#endif
public func profilePictureURL(completion: @escaping (URL?) -> Void) {
connection.caches.user.once { [weak self] user in
guard let self else {
Current.Log.error("Failed to retrieve profile picture URL: self is nil")
completion(nil)
return
}
connection.caches.states().once { [weak self] states in
let states = states.all
guard let person = states.first(where: { $0.attributes["user_id"] as? String == user.id }) else {
Current.Log.error("Profile picture: No person found for user \(user.id)")
completion(nil)
return
}
guard let path = person.attributes["entity_picture"] as? String else {
Current.Log.error("Profile picture: Missing URL for user entity picture, user id \(user.id)")
completion(nil)
return
}
guard let url = self?.server.info.connection.activeURL()?.appendingPathComponent(path) else {
Current.Log.error("Profile picture: Missing active URL for user entity picture, user id \(user.id)")
completion(nil)
return
}
completion(url)
}
}
}
public func profilePicture(completion: @escaping (UIImage?) -> Void) {
profilePictureURL { [weak self] url in
guard let self, let url else {
completion(nil)
return
}
manager.download(url).validate().responseData { response in
switch response.result {
case let .success(data):
completion(UIImage(data: data))
case let .failure(error):
Current.Log.error("Failed to download profile picture: \(error)")
completion(nil)
}
}
}
}
}
#if !os(watchOS)
/// Certificate provider implementation for Home Assistant servers
private class HomeAssistantCertificateProvider: HACertificateProvider {
private let server: Server
init(server: Server) {
self.server = server
}
func provideClientCertificate(
for challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void
) {
Current.Log.info("[mTLS HAKit] Client certificate requested")
guard let clientCertificate = server.info.connection.clientCertificate else {
Current.Log.warning("[mTLS HAKit] Client certificate requested but none configured")
completionHandler(.performDefaultHandling, nil)
return
}
do {
let credential = try ClientCertificateManager.shared.urlCredential(for: clientCertificate)
Current.Log.info("[mTLS HAKit] Using client certificate: \(clientCertificate.displayName)")
completionHandler(.useCredential, credential)
} catch {
Current.Log.error("[mTLS HAKit] Failed to get credential: \(error)")
completionHandler(.cancelAuthenticationChallenge, nil)
}
}
func evaluateServerTrust(
_ serverTrust: SecTrust,
forHost host: String,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void
) {
Current.Log.info("[mTLS HAKit] Evaluating server trust for: \(host)")
do {
try server.info.connection.securityExceptions.evaluate(serverTrust)
Current.Log.info("[mTLS HAKit] Server trust validation successful")
let credential = URLCredential(trust: serverTrust)
completionHandler(.useCredential, credential)
} catch {
Current.Log.error("[mTLS HAKit] Server trust validation failed: \(error)")
completionHandler(.rejectProtectionSpace, nil)
}
}
}
#endif
extension HomeAssistantAPI.APIError: LocalizedError {
public var errorDescription: String? {
switch self {
case .managerNotAvailable:
return L10n.HaApi.ApiError.managerNotAvailable
case .invalidResponse:
return L10n.HaApi.ApiError.invalidResponse
case .cantBuildURL:
return L10n.HaApi.ApiError.cantBuildUrl
case .notConfigured:
return L10n.HaApi.ApiError.notConfigured
case .updateNotPossible:
return L10n.HaApi.ApiError.updateNotPossible
case .mobileAppComponentNotLoaded:
return L10n.HaApi.ApiError.mobileAppComponentNotLoaded
case let .mustUpgradeHomeAssistant(current: current, minimum: minimum):
return L10n.HaApi.ApiError.mustUpgradeHomeAssistant(
current.description,
minimum.description
)
case .noAPIAvailable:
return L10n.HaApi.ApiError.noAvailableApi
case .unknown:
return L10n.HaApi.ApiError.unknown
}
}
}
extension HomeAssistantAPI: SensorObserver {
public func sensorContainer(
_ container: SensorContainer,
didSignalForUpdateBecause reason: SensorContainerUpdateReason,
lastUpdate: SensorObserverUpdate?
) {
Current.backgroundTask(withName: BackgroundTask.signaledUpdateSensors.rawValue) { _ in
UpdateSensors(trigger: .Signaled)
}.cauterize()
}
public func sensorContainer(_ container: SensorContainer, didUpdate update: SensorObserverUpdate) {
// we don't do anything for this
}
}