mirror of
https://github.com/home-assistant/iOS.git
synced 2026-04-16 20:49:48 -05:00
<!-- 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 option in the debug section to receive debug notifications, this is a debugging tool only and it is not visible to the user unless it open the debugging tool hidden panel. First use case: Receive notification when location fails to update ein brackground. ## 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. -->
71 lines
2.4 KiB
Swift
71 lines
2.4 KiB
Swift
import AppIntents
|
|
import Foundation
|
|
import HAKit
|
|
import Shared
|
|
import SwiftUI
|
|
|
|
@available(iOS 16.4, *)
|
|
/// Intent activate scenes or scripts
|
|
struct CustomWidgetActivateAppIntent: AppIntent {
|
|
static var title: LocalizedStringResource = "Activate"
|
|
static var isDiscoverable: Bool = false
|
|
|
|
// No translation needed below, this is not a discoverable intent
|
|
@Parameter(title: "Server")
|
|
var serverId: String?
|
|
@Parameter(title: "Domain")
|
|
var domain: String?
|
|
@Parameter(title: "Entity ID")
|
|
var entityId: String?
|
|
|
|
func perform() async throws -> some IntentResult {
|
|
guard let serverId,
|
|
let domainString = domain,
|
|
let domain = Domain(rawValue: domainString),
|
|
let entityId,
|
|
let server = Current.servers.all.first(where: { server in
|
|
server.identifier.rawValue == serverId
|
|
}), let connection = Current.api(for: server)?.connection else {
|
|
return .result()
|
|
}
|
|
AppIntentHaptics.notify()
|
|
|
|
guard let request: HATypedRequest<HAResponseVoid> = {
|
|
switch domain {
|
|
case .script:
|
|
return .runScript(entityId: entityId)
|
|
case .scene:
|
|
return .applyScene(entityId: entityId)
|
|
default:
|
|
Current.Log.error("Attempt to use ActivateAppIntent with unsupported domain \(domain)")
|
|
return nil
|
|
}
|
|
}() else {
|
|
return .result()
|
|
}
|
|
|
|
await withCheckedContinuation { continuation in
|
|
connection.send(request).promise.pipe { result in
|
|
switch result {
|
|
case .fulfilled:
|
|
continuation.resume()
|
|
case let .rejected(error):
|
|
Current.Log
|
|
.error(
|
|
"Failed to execute ActivateAppIntent, serverId: \(serverId), domain: \(domain), entityId: \(entityId), error: \(error)"
|
|
)
|
|
Current.notificationDispatcher.send(.init(
|
|
id: .intentActivateFailed,
|
|
title: L10n.Widgets.Custom.IntentActivateFailed.title,
|
|
body: L10n.Widgets.Custom.IntentActivateFailed.body
|
|
))
|
|
|
|
continuation.resume()
|
|
}
|
|
}
|
|
}
|
|
_ = try await ResetAllCustomWidgetConfirmationAppIntent().perform()
|
|
return .result()
|
|
}
|
|
}
|