Skip to content

Commit 5f765e5

Browse files
authored
Add fallback case enum for Assist events preventing crashes on newer event types (#3461)
…events <!-- 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. -->
1 parent 0d389c8 commit 5f765e5

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Sources/Shared/Intents/AppIntent/AssistInApp/AssistModel.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,12 @@ public struct AssistResponse: HADataDecodable {
8080
public init(data: HAData) throws {
8181
self.data = try? data.decode("data")
8282
let type = try data.decode("type") as String
83-
self.type = AssistEvent(rawValue: type)!
83+
if let eventType = AssistEvent(rawValue: type) {
84+
self.type = eventType
85+
} else {
86+
Current.Log.error("Unknown assist event type: \(type)")
87+
self.type = .unknown
88+
}
8489
self.timestamp = try data.decode("timestamp")
8590
}
8691

@@ -184,6 +189,13 @@ public enum AssistEvent: String, Codable {
184189
case ttsStart = "tts-start"
185190
case ttsEnd = "tts-end"
186191
case error = "error"
192+
case unknown
193+
194+
public init(from decoder: Decoder) throws {
195+
let container = try decoder.singleValueContainer()
196+
let rawValue = try container.decode(String.self)
197+
self = AssistEvent(rawValue: rawValue) ?? .unknown
198+
}
187199
}
188200

189201
/// Saved in database

Sources/Shared/Intents/AppIntent/AssistInApp/AssistService.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ public final class AssistService: AssistServiceProtocol {
189189
Current.Log.error("Received error while interating with Assist: \(data)")
190190
delegate?.didReceiveError(code: data.data?.code ?? "-1", message: data.data?.message ?? "Unknown error")
191191
cancellable.cancel()
192+
case .unknown:
193+
Current.Log.verbose("Unmapped event received from Assist")
192194
}
193195
}
194196
}

0 commit comments

Comments
 (0)