mirror of
https://github.com/home-assistant/iOS.git
synced 2026-02-04 21:15:17 -06:00
Starting in iOS 15, there's a number of crashes happening in the background with Realm. They don't appear to be due to the file lock in the shared app container, but this may help resolve them either way -- easy to see if the next beta doesn't crash a bunch.
47 lines
1.5 KiB
Swift
47 lines
1.5 KiB
Swift
import Foundation
|
|
import PromiseKit
|
|
import UserNotifications
|
|
|
|
extension WebhookResponseIdentifier {
|
|
static var serviceCall: Self { .init(rawValue: "serviceCall") }
|
|
}
|
|
|
|
struct WebhookResponseServiceCall: WebhookResponseHandler {
|
|
let api: HomeAssistantAPI
|
|
init(api: HomeAssistantAPI) {
|
|
self.api = api
|
|
}
|
|
|
|
static func shouldReplace(request current: WebhookRequest, with proposed: WebhookRequest) -> Bool {
|
|
// every service call is distinct
|
|
false
|
|
}
|
|
|
|
func handle(
|
|
request: Promise<WebhookRequest>,
|
|
result: Promise<Any>
|
|
) -> Guarantee<WebhookResponseHandlerResult> {
|
|
firstly {
|
|
when(fulfilled: request, result)
|
|
}.then { request, _ -> Promise<Void> in
|
|
let requestDictionary = try request.asDictionary()
|
|
|
|
let domain = requestDictionary["domain"] as? String ?? "(unknown)"
|
|
let service = requestDictionary["service"] as? String ?? "(unknown)"
|
|
let payload = requestDictionary["service_data"] as? [String: Any] ?? [:]
|
|
|
|
let event = ClientEvent(
|
|
text: "Called service: \(domain).\(service)",
|
|
type: .serviceCall,
|
|
payload: payload
|
|
)
|
|
|
|
return Current.clientEventStore.addEvent(event)
|
|
}.then {
|
|
Guarantee.value(WebhookResponseHandlerResult.default)
|
|
}.recover { _ in
|
|
Guarantee.value(WebhookResponseHandlerResult.default)
|
|
}
|
|
}
|
|
}
|