From 471b27edd1c326d1576af2cfc70ed8b9a214ec1b Mon Sep 17 00:00:00 2001 From: Zac West <74188+zacwest@users.noreply.github.com> Date: Mon, 17 Oct 2022 13:35:00 -0700 Subject: [PATCH] Skip updating registration in push provider (#2227) ## Summary The push provider's periodic updating runs the 'connect' mechanism, which also updates the registration/integration. In iOS 16, this means we're updating the device name to 'iPhone' because our device name entitlement only exists in the main app. To avoid this issue, we no longer update the registration in the push provider. ## Screenshots ## Link to pull request in Documentation repository Documentation: home-assistant/companion.home-assistant# ## Any other notes --- Sources/Shared/API/HAAPI.swift | 74 ++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/Sources/Shared/API/HAAPI.swift b/Sources/Shared/API/HAAPI.swift index 6194a0053..b2afa1f87 100644 --- a/Sources/Shared/API/HAAPI.swift +++ b/Sources/Shared/API/HAAPI.swift @@ -171,40 +171,54 @@ public class HomeAssistantAPI { } public func Connect(reason: ConnectReason) -> Promise { + Current.Log.info("running connect for \(reason)") + + // websocket connection.connect() - return firstly { - updateRegistration().asVoid() - }.recover { [self] error -> Promise 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." - return Current.clientEventStore.addEvent(ClientEvent(text: message, type: .networkRequest, payload: [ - "error": String(describing: error), - ])).then { [self] in - register() + return firstly { () -> Promise in + guard !Current.isAppExtension else { + Current.Log.info("skipping registration changes in extension") + return Promise.value(()) + } + + return updateRegistration().asVoid().recover { [self] error -> Promise 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." + return Current.clientEventStore + .addEvent(ClientEvent(text: message, type: .networkRequest, payload: [ + "error": String(describing: error), + ])).then { [self] in + 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 } - 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] in - when(fulfilled: [ - getConfig(), - Current.modelManager.fetch(apis: [self]), - UpdateSensors(trigger: reason.updateSensorTrigger).asVoid(), - updateComplications(passively: false).asVoid(), - ]).asVoid() + }.then { [self] () -> Promise in + var promises: [Promise] = [] + + 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,