Skip to content

Commit

Permalink
Add device registry id to assist call (#2838)
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 -->

Integration PR:
home-assistant/core#121496 (comment)

## 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 committed Jul 9, 2024
1 parent 02f13ad commit df68401
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions Sources/Shared/API/HAAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ public class HomeAssistantAPI {
server.connection.cloudhookURL = config.CloudhookURL
server.connection.set(address: config.RemoteUIURL, for: .remoteUI)
server.remoteName = config.LocationName ?? ServerInfo.defaultName
server.hassDeviceId = config.hassDeviceId

if let version = try? Version(hassVersion: config.Version) {
server.version = version
Expand Down
2 changes: 2 additions & 0 deletions Sources/Shared/API/Responses/ConfigResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ObjectMapper
public class ConfigResponse: Mappable {
public var Components: [String] = []
public var Version: String = ""
public var hassDeviceId: String?

public var TemperatureUnit: String?
public var LengthUnit: String?
Expand All @@ -27,6 +28,7 @@ public class ConfigResponse: Mappable {
public func mapping(map: Map) {
Components <- map["components"]
Version <- map["version"]
hassDeviceId <- map["hass_device_id"]

TemperatureUnit <- map["unit_system.temperature"]
LengthUnit <- map["unit_system.length"]
Expand Down
4 changes: 4 additions & 0 deletions Sources/Shared/API/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public struct ServerInfo: Codable, Equatable {
}

public var remoteName: String
public var hassDeviceId: String?
public var sortOrder: Int
public var version: Version
public var connection: ConnectionInfo
Expand All @@ -84,12 +85,14 @@ public struct ServerInfo: Codable, Equatable {
case connectionInfo
case tokenInfo
case settings
case hassDeviceId
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

self.remoteName = try container.decode(String.self, forKey: .name)
self.hassDeviceId = try container.decodeIfPresent(String.self, forKey: .hassDeviceId)
self.sortOrder = try container.decode(Int.self, forKey: .sortOrder)
self.connection = try container.decode(ConnectionInfo.self, forKey: .connectionInfo)
self.token = try container.decode(TokenInfo.self, forKey: .tokenInfo)
Expand All @@ -105,6 +108,7 @@ public struct ServerInfo: Codable, Equatable {
try container.encode(connection, forKey: .connectionInfo)
try container.encode(token, forKey: .tokenInfo)
try container.encode(settings, forKey: .settings)
try container.encode(hassDeviceId, forKey: .hassDeviceId)
}

public init(
Expand Down
12 changes: 10 additions & 2 deletions Sources/Shared/Intents/AssistInApp/AssistRequests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ public enum AssistRequests {
public static func assistByVoiceTypedSubscription(
preferredPipelineId: String,
audioSampleRate: Double,
conversationId: String?
conversationId: String?,
hassDeviceId: String?
) -> HATypedSubscription<AssistResponse> {
var data: [String: Any] = [
"pipeline": preferredPipelineId,
Expand All @@ -18,13 +19,17 @@ public enum AssistRequests {
if let conversationId {
data["conversation_id"] = conversationId
}
if let hassDeviceId {
data["device_id"] = hassDeviceId
}
return .init(request: .init(type: .webSocket("assist_pipeline/run"), data: data))
}

public static func assistByTextTypedSubscription(
preferredPipelineId: String,
inputText: String,
conversationId: String?
conversationId: String?,
hassDeviceId: String?
) -> HATypedSubscription<AssistResponse> {
var data: [String: Any] = [
"pipeline": preferredPipelineId,
Expand All @@ -37,6 +42,9 @@ public enum AssistRequests {
if let conversationId {
data["conversation_id"] = conversationId
}
if let hassDeviceId {
data["device_id"] = hassDeviceId
}
return .init(request: .init(type: .webSocket("assist_pipeline/run"), data: data))
}

Expand Down
6 changes: 4 additions & 2 deletions Sources/Shared/Intents/AssistInApp/AssistService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ public final class AssistService: AssistServiceProtocol {
connection.subscribe(to: AssistRequests.assistByVoiceTypedSubscription(
preferredPipelineId: pipelineId,
audioSampleRate: audioSampleRate,
conversationId: conversationId
conversationId: conversationId,
hassDeviceId: server.info.hassDeviceId
)) { [weak self] cancellable, data in
guard let self else { return }
self.cancellable = cancellable
Expand All @@ -122,7 +123,8 @@ public final class AssistService: AssistServiceProtocol {
connection.subscribe(to: AssistRequests.assistByTextTypedSubscription(
preferredPipelineId: pipelineId,
inputText: input,
conversationId: conversationId
conversationId: conversationId,
hassDeviceId: server.info.hassDeviceId
)) { [weak self] cancellable, data in
guard let self else { return }
self.cancellable = cancellable
Expand Down

0 comments on commit df68401

Please sign in to comment.