Skip to content

Commit

Permalink
Skip updating registration in push provider (#2227)
Browse files Browse the repository at this point in the history
## 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
<!-- 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. -->
  • Loading branch information
zacwest authored Oct 17, 2022
1 parent 7e276a2 commit 471b27e
Showing 1 changed file with 44 additions and 30 deletions.
74 changes: 44 additions & 30 deletions Sources/Shared/API/HAAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,40 +171,54 @@ public class HomeAssistantAPI {
}

public func Connect(reason: ConnectReason) -> Promise<Void> {
Current.Log.info("running connect for \(reason)")

// websocket
connection.connect()

return firstly {
updateRegistration().asVoid()
}.recover { [self] error -> Promise<Void> 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<Void> in
guard !Current.isAppExtension else {
Current.Log.info("skipping registration changes in extension")
return Promise<Void>.value(())
}

return updateRegistration().asVoid().recover { [self] error -> Promise<Void> 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<Void> in
var promises: [Promise<Void>] = []

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,
Expand Down

0 comments on commit 471b27e

Please sign in to comment.