Skip to content

Commit

Permalink
Add fallback case enum for Assist events preventing crashes on newer …
Browse files Browse the repository at this point in the history
…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. -->
  • Loading branch information
bgoncal authored Feb 26, 2025
1 parent 0d389c8 commit 5f765e5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Sources/Shared/Intents/AppIntent/AssistInApp/AssistModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ public struct AssistResponse: HADataDecodable {
public init(data: HAData) throws {
self.data = try? data.decode("data")
let type = try data.decode("type") as String
self.type = AssistEvent(rawValue: type)!
if let eventType = AssistEvent(rawValue: type) {
self.type = eventType
} else {
Current.Log.error("Unknown assist event type: \(type)")
self.type = .unknown
}
self.timestamp = try data.decode("timestamp")
}

Expand Down Expand Up @@ -184,6 +189,13 @@ public enum AssistEvent: String, Codable {
case ttsStart = "tts-start"
case ttsEnd = "tts-end"
case error = "error"
case unknown

public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let rawValue = try container.decode(String.self)
self = AssistEvent(rawValue: rawValue) ?? .unknown
}
}

/// Saved in database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ public final class AssistService: AssistServiceProtocol {
Current.Log.error("Received error while interating with Assist: \(data)")
delegate?.didReceiveError(code: data.data?.code ?? "-1", message: data.data?.message ?? "Unknown error")
cancellable.cancel()
case .unknown:
Current.Log.verbose("Unmapped event received from Assist")
}
}
}

0 comments on commit 5f765e5

Please sign in to comment.