Skip to content

Commit

Permalink
Revert to old actions logic for Apple Watch (#2784)
Browse files Browse the repository at this point in the history
<!-- 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 -->

## 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
bgoncal authored May 23, 2024
1 parent b089ef8 commit 474975f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Configuration/Version.xcconfig
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
MARKETING_VERSION=2024.6
MARKETING_VERSION=2024.5.1
CURRENT_PROJECT_VERSION=2024
57 changes: 45 additions & 12 deletions Sources/Extensions/Watch/Home/WatchHomeViewModel.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Communicator
import Foundation
import PromiseKit
import RealmSwift
import Shared

Expand Down Expand Up @@ -27,6 +29,11 @@ enum WatchHomeViewState {
}

final class WatchHomeViewModel: WatchHomeViewModelProtocol {
enum SendError: Error {
case notImmediate
case phoneFailed
}

@Published var actions: [WatchActionItem] = []
@Published var state: WatchHomeViewState = .idle {
didSet {
Expand All @@ -50,21 +57,47 @@ final class WatchHomeViewModel: WatchHomeViewModelProtocol {

Current.Log.verbose("Selected action id: \(actionId)")

guard let server = Current.servers.server(for: selectedAction) else {
Current.Log.verbose("Failed to get server for action id: \(actionId)")
return
}

setState(.loading)

Current.api(for: server).HandleAction(actionID: actionId, source: .Watch).pipe { [weak self] result in
switch result {
case .fulfilled:
self?.setState(.success)
case let .rejected(error):
Current.Log.info(error)
self?.setState(.failure)
firstly { () -> Promise<Void> in
Promise { seal in
guard Communicator.shared.currentReachability == .immediatelyReachable else {
seal.reject(SendError.notImmediate)
return
}

Current.Log.verbose("Signaling action pressed via phone")
let actionMessage = InteractiveImmediateMessage(
identifier: "ActionRowPressed",
content: ["ActionID": selectedAction.ID],
reply: { message in
Current.Log.verbose("Received reply dictionary \(message)")
if message.content["fired"] as? Bool == true {
seal.fulfill(())
} else {
seal.reject(SendError.phoneFailed)
}
}
)

Current.Log.verbose("Sending ActionRowPressed message \(actionMessage)")
Communicator.shared.send(actionMessage, errorHandler: { error in
Current.Log.error("Received error when sending immediate message \(error)")
seal.reject(error)
})
}
}.recover { error -> Promise<Void> in
guard error == SendError.notImmediate, let server = Current.servers.server(for: selectedAction) else {
throw error
}

Current.Log.error("recovering error \(error) by trying locally")
return Current.api(for: server).HandleAction(actionID: selectedAction.ID, source: .Watch)
}.done { [weak self] in
self?.setState(.success)
}.catch { [weak self] err in
Current.Log.error("Error during action event fire: \(err)")
self?.setState(.failure)
}
}

Expand Down

0 comments on commit 474975f

Please sign in to comment.