Files
iOS/Sources/Shared/Environment/MatterWrapper.swift
Bruno Pantaleão Gonçalves fa628036f8 Communicate to frontend when matter commissioning finishes (#4684)
<!-- 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 adds a frontend bus command to communicate when matter
commissioning finishes and also provides the device name chosen by the
user during the Apple matter flow
## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->

## 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. -->
2026-06-03 09:19:26 +00:00

88 lines
2.8 KiB
Swift

#if canImport(MatterSupport)
import MatterSupport
#endif
import PromiseKit
public class MatterWrapper {
public var isAvailable: Bool = {
#if canImport(MatterSupport) && !targetEnvironment(macCatalyst)
if #available(iOS 16.1, *) {
return true
} else {
return false
}
#else
return false
#endif
}()
public var threadCredentialsSharingEnabled: Bool {
// For now mac is not returning thread credentials for some reason
#if canImport(ThreadNetwork) && !targetEnvironment(macCatalyst)
if #available(iOS 16.4, *) {
return true
} else {
return false
}
#else
return false
#endif
}
public var threadCredentialsStoreInKeychainEnabled: Bool {
#if canImport(ThreadNetwork) && !targetEnvironment(macCatalyst)
if #available(iOS 16.4, *) {
return true
} else {
return false
}
#else
return false
#endif
}
#if os(iOS)
public var threadClientService: ThreadClientProtocol = ThreadClientService()
public var lastCommissionServerIdentifier: Identifier<Server>? {
get { Current.settingsStore.prefs.string(forKey: "lastCommissionServerID").flatMap { .init(rawValue: $0) } }
set { Current.settingsStore.prefs.set(newValue?.rawValue, forKey: "lastCommissionServerID") }
}
public lazy var commission: (_ server: Server) -> Promise<String?> = { [self] server in
#if canImport(MatterSupport)
guard #available(iOS 16.4, *) else {
return .value(nil)
}
lastCommissionServerIdentifier = server.identifier
Current.settingsStore.matterLastCommissionedDeviceName = nil
let request = MatterAddDeviceRequest(
topology: .init(ecosystemName: "Home Assistant", homes: []),
shouldScanNetworks: true
)
return Promise<String?> { seal in
Task {
do {
try await request.perform()
let deviceName = Current.settingsStore.matterLastCommissionedDeviceName
// Reset device name after reading it, so that if the user tries to pair another device without
// going through the flow again, we won't have a stale name hanging around
Current.settingsStore.matterLastCommissionedDeviceName = nil
Current.Log.info("Matter pairing finished (native flow manually closed or pairing succeeded)")
seal.fulfill(deviceName)
} catch {
Current.Log.error("Matter pairing failed: \(error)")
seal.reject(error)
}
}
}
#else
return .value(nil)
#endif
}
#endif
}