Files
iOS/Sources/Shared/Environment/WatchUserDefaults.swift
Bruno Pantaleão Gonçalves 9c9e544e62 Add Apple Watch mTLS compatibility and settings screen (#4753)
<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->
This PR bumps HAKit to 0.4.15 which allows mTLS on Apple Watch.
It also includes connectivity changes + a settings screen to visualize
connectivity information and define where to execute scripts and scenes
from.

## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->
<img width="742" height="864" alt="CleanShot 2026-06-16 at 16 39 30@2x"
src="https://github.com/user-attachments/assets/3b9ec512-76f4-4a2f-a677-710c7f37bef4"
/>


## Link to pull request in Documentation repository
<!-- Pull requests that add, change or remove functionality must have a
corresponding pull request in the Companion App Documentation repository
(https://github.com/home-assistant/companion.home-assistant). Please add
the number of this pull request after the "#" -->
Documentation: home-assistant/companion.home-assistant#

## Any other notes
<!-- If there is any other information of note, like if this Pull
Request is part of a bigger change, please include it here. -->

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2026-06-16 17:57:46 +02:00

67 lines
2.4 KiB
Swift

import Foundation
public enum WatchUserDefaultsKey: String {
case watchSSID
/// When the watch last received the server configuration from the paired iPhone.
case serversUpdatedAt
/// Where the watch runs actions (magic items): automatically, always via iPhone, or directly.
case performActionTarget
}
/// Where the Apple Watch performs actions such as executing magic items.
public enum WatchActionTarget: String, CaseIterable {
/// Use the iPhone when it's immediately reachable, otherwise connect directly from the Watch.
case auto
/// Always route through the paired iPhone.
case iPhone
/// Always connect directly from the Apple Watch.
case appleWatch
}
public final class WatchUserDefaults {
public static var shared = WatchUserDefaults()
private let userDefaults = UserDefaults()
public func set(_ value: Any?, key: WatchUserDefaultsKey) {
userDefaults.set(value, forKey: key.rawValue)
}
public func string(for key: WatchUserDefaultsKey) -> String? {
userDefaults.string(forKey: key.rawValue)
}
public func date(for key: WatchUserDefaultsKey) -> Date? {
userDefaults.object(forKey: key.rawValue) as? Date
}
// MARK: - Action target
public var performActionTarget: WatchActionTarget {
get { string(for: .performActionTarget).flatMap(WatchActionTarget.init(rawValue:)) ?? .auto }
set { set(newValue.rawValue, key: .performActionTarget) }
}
// MARK: - Per-server URL override (watch-local)
// The watch's server configuration is overwritten on every sync, so a "force this URL" choice
// can't live in `ConnectionInfo`. It's stored here per server and re-applied after each sync.
// The value is a `ConnectionInfo.URLType` raw value, or absent for automatic selection.
private func urlOverrideKey(forServerId serverId: String) -> String {
"serverURLOverride.\(serverId)"
}
public func urlOverrideRawValue(forServerId serverId: String) -> Int? {
userDefaults.object(forKey: urlOverrideKey(forServerId: serverId)) as? Int
}
public func setURLOverrideRawValue(_ rawValue: Int?, forServerId serverId: String) {
let key = urlOverrideKey(forServerId: serverId)
if let rawValue {
userDefaults.set(rawValue, forKey: key)
} else {
userDefaults.removeObject(forKey: key)
}
}
}