2025-06-19 01:02:51 -07:00
|
|
|
//
|
|
|
|
|
// HomeAssistantServer.swift
|
|
|
|
|
// XIONControlPanel
|
|
|
|
|
//
|
|
|
|
|
// Created by James Magahern on 6/18/25.
|
|
|
|
|
// Copyright © 2025 XION. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
2025-06-19 19:25:53 -07:00
|
|
|
import Combine
|
2025-06-19 01:02:51 -07:00
|
|
|
import Foundation
|
|
|
|
|
|
|
|
|
|
class HomeAssistantServer: Server
|
|
|
|
|
{
|
|
|
|
|
var connectionStatus: ConnectionStatus = .disconnected
|
|
|
|
|
|
|
|
|
|
// Configure:
|
|
|
|
|
private let authToken: String = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiI2ZTAwODI1N2Y4N2Q0OWIxYWRkN2ExNTBhOWRmNGZiZCIsImlhdCI6MTc1MDMwMDg5NiwiZXhwIjoyMDY1NjYwODk2fQ.KRf9GpRZdpW9Z_vkbL3sl74rgKS7eAyc8kO0a5jLTgg"
|
|
|
|
|
|
|
|
|
|
// Configure:
|
|
|
|
|
private static let filterLabel = "control_panel"
|
|
|
|
|
|
|
|
|
|
private let url: URL
|
2025-06-19 19:25:53 -07:00
|
|
|
private let liaison: Liaison
|
2025-06-19 01:02:51 -07:00
|
|
|
private var connectionContinuations: [CheckedContinuation<Bool, Never>] = []
|
2025-06-19 19:25:53 -07:00
|
|
|
private var eventTask: Task<(), any Error>? = nil
|
|
|
|
|
|
|
|
|
|
typealias NoResult = [String: String]
|
2025-06-19 01:02:51 -07:00
|
|
|
|
|
|
|
|
required init(_ url: URL) {
|
|
|
|
|
self.url = url
|
2025-06-19 19:25:53 -07:00
|
|
|
|
|
|
|
|
let websocketURL = url.appending(component: "websocket").websocket()
|
|
|
|
|
self.liaison = Liaison(websocketURL: websocketURL)
|
2025-06-19 01:02:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func connect(_ completion: @escaping ((any Error)?) -> Void) {
|
2025-06-19 19:25:53 -07:00
|
|
|
guard self.eventTask == nil else {
|
|
|
|
|
completion(nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-06-19 01:02:51 -07:00
|
|
|
|
2025-06-19 19:25:53 -07:00
|
|
|
self.eventTask = Task {
|
|
|
|
|
for await event in try await liaison.partialEvents() {
|
|
|
|
|
do {
|
|
|
|
|
try await handleLiaisonEvent(event)
|
|
|
|
|
} catch {
|
|
|
|
|
encounteredWebsocketError(error)
|
2025-06-19 01:02:51 -07:00
|
|
|
}
|
|
|
|
|
}
|
2025-06-19 19:25:53 -07:00
|
|
|
|
|
|
|
|
print("Event loop exited")
|
|
|
|
|
connectionStatus = .error
|
2025-06-19 01:02:51 -07:00
|
|
|
}
|
|
|
|
|
completion(nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func disconnect(_ completion: ((any Error)?) -> Void) {
|
2025-06-19 19:25:53 -07:00
|
|
|
Task { await liaison.disconnect() }
|
2025-06-19 01:02:51 -07:00
|
|
|
completion(nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func fetchDevices(_ completion: @escaping (Result<[AnyDevice], any Error>) -> Void) {
|
2025-06-19 19:25:53 -07:00
|
|
|
guard connectionStatus != .error else {
|
|
|
|
|
// Need to report this state to the UI.
|
|
|
|
|
completion(.failure(ServerError.disconnected))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-19 01:02:51 -07:00
|
|
|
Task {
|
|
|
|
|
await waitConnected()
|
|
|
|
|
|
|
|
|
|
do {
|
2025-06-19 19:25:53 -07:00
|
|
|
let registryEntries: [RegistryEntry] = try await liaison.sendMessage(RegistryEntriesMessage())
|
2025-06-19 01:02:51 -07:00
|
|
|
let filteredEntries = registryEntries.filter { $0.labels.contains(Self.filterLabel) }
|
|
|
|
|
|
2025-06-19 19:25:53 -07:00
|
|
|
let states: [EntityState] = try await liaison.sendMessage(GetStatesMessage())
|
2025-06-19 01:02:51 -07:00
|
|
|
let stateMap = states.reduce(into: [String: DeviceState]()) { partialResult, state in
|
|
|
|
|
partialResult[state.entityId] = switch state.state {
|
|
|
|
|
case .on: .on
|
|
|
|
|
case .off: .off
|
|
|
|
|
case .other(_): .off
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let devices = filteredEntries.map { entry in
|
|
|
|
|
HomeAssistantDevice(entity: entry, state: stateMap[entry.entityId] ?? .on)
|
|
|
|
|
.eraseToAnyDevice()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Task { @MainActor in completion(.success(devices)) }
|
|
|
|
|
} catch {
|
|
|
|
|
Task { @MainActor in completion(.failure(error)) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func toggleDevice(_ device: AnyDevice, state: DeviceState, completion: @escaping ((any Error)?) -> Void) {
|
2025-06-19 19:25:53 -07:00
|
|
|
guard connectionStatus != .error else {
|
|
|
|
|
completion(ServerError.disconnected)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-19 01:02:51 -07:00
|
|
|
Task {
|
|
|
|
|
await waitConnected()
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
let entityID = device.serial
|
|
|
|
|
let service: CallServiceMessage.Service = switch state {
|
|
|
|
|
case .off: .turnOff
|
|
|
|
|
case .on: .turnOn
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-19 19:25:53 -07:00
|
|
|
var domain: CallServiceMessage.ServiceDomain = .switch
|
|
|
|
|
if entityID.starts(with: "light.") {
|
|
|
|
|
// device.type is a UI concept, not related to what HA knows about it.
|
|
|
|
|
domain = .light
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let message = CallServiceMessage(
|
|
|
|
|
entityID: entityID,
|
|
|
|
|
service: service,
|
|
|
|
|
domain: domain
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
let _: NoResult = try await liaison.sendMessage(message)
|
2025-06-19 01:02:51 -07:00
|
|
|
Task { @MainActor in completion(nil) }
|
|
|
|
|
} catch {
|
|
|
|
|
Task { @MainActor in completion(error) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func responsibleForDevice(_ device: AnyDevice) -> Bool {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
|
private func waitConnected() async -> Bool {
|
|
|
|
|
if connectionStatus == .connecting || connectionStatus == .disconnected {
|
|
|
|
|
return await withCheckedContinuation { continuation in
|
|
|
|
|
self.connectionContinuations.append(continuation)
|
|
|
|
|
}
|
2025-06-19 19:25:53 -07:00
|
|
|
} else if connectionStatus == .error {
|
|
|
|
|
return false
|
2025-06-19 01:02:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-19 15:00:19 -07:00
|
|
|
private func encounteredWebsocketError(_ error: any Error) {
|
|
|
|
|
print("Websocket Error: \(error)")
|
|
|
|
|
self.connectionStatus = .error
|
2025-06-19 19:25:53 -07:00
|
|
|
self.eventTask?.cancel()
|
|
|
|
|
self.eventTask = nil
|
2025-06-19 01:02:51 -07:00
|
|
|
}
|
|
|
|
|
|
2025-06-19 19:25:53 -07:00
|
|
|
private func handleLiaisonEvent(_ event: Liaison.Event) async throws {
|
|
|
|
|
switch event {
|
|
|
|
|
case .partialEvent(let partialEvent, _):
|
2025-06-19 01:02:51 -07:00
|
|
|
switch partialEvent.type {
|
|
|
|
|
case .authRequired:
|
2025-06-19 19:25:53 -07:00
|
|
|
Task { await authenticate() }
|
2025-06-19 01:02:51 -07:00
|
|
|
case .authOK:
|
2025-06-19 19:25:53 -07:00
|
|
|
Task { didConnectToWebsocket() }
|
|
|
|
|
default:
|
|
|
|
|
break
|
2025-06-19 01:02:51 -07:00
|
|
|
}
|
2025-06-19 19:25:53 -07:00
|
|
|
|
|
|
|
|
case .pong:
|
|
|
|
|
didConnectToWebsocket()
|
|
|
|
|
|
|
|
|
|
case .disconnected(let error):
|
|
|
|
|
encounteredWebsocketError(error)
|
|
|
|
|
|
|
|
|
|
case .error(let error):
|
|
|
|
|
encounteredWebsocketError(error)
|
2025-06-19 01:02:51 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func didConnectToWebsocket() {
|
|
|
|
|
self.connectionStatus = .connected
|
|
|
|
|
|
|
|
|
|
connectionContinuations.forEach { continuation in
|
|
|
|
|
continuation.resume(returning: true)
|
|
|
|
|
}
|
|
|
|
|
connectionContinuations.removeAll()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func authenticate() async {
|
|
|
|
|
do {
|
2025-06-19 19:25:53 -07:00
|
|
|
let _: NoResult = try await liaison.sendMessage(AuthMessage(token: authToken))
|
2025-06-19 01:02:51 -07:00
|
|
|
} catch {
|
|
|
|
|
print("Error authenticating: \(error)")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-19 19:25:53 -07:00
|
|
|
// MARK: - Liaison
|
|
|
|
|
|
|
|
|
|
actor Liaison
|
|
|
|
|
{
|
|
|
|
|
let websocketURL: URL
|
|
|
|
|
|
|
|
|
|
private typealias Continuation = AsyncStream<Event>.Continuation
|
|
|
|
|
private typealias Reply = (Data) -> Void
|
|
|
|
|
|
|
|
|
|
private var websocketTask: URLSessionWebSocketTask? = nil
|
|
|
|
|
private var eventTask: Task<(), Never>? = nil
|
|
|
|
|
private var pingTask: Task<(), Never>? = nil
|
|
|
|
|
|
|
|
|
|
private var pendingMessages: [MessageID: Reply] = [:]
|
|
|
|
|
private var continuations: [UUID: Continuation] = [:]
|
|
|
|
|
|
|
|
|
|
fileprivate init(websocketURL: URL) {
|
|
|
|
|
self.websocketURL = websocketURL
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileprivate func connect() {
|
|
|
|
|
spawnWebsocketTaskIfNecessary()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileprivate func disconnect() {
|
|
|
|
|
self.websocketTask?.cancel()
|
|
|
|
|
self.websocketTask = nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
|
fileprivate func sendMessage<R: Decodable>(_ message: Message) async throws -> R {
|
|
|
|
|
return try await withCheckedThrowingContinuation { continuation in
|
|
|
|
|
Task {
|
|
|
|
|
var outgoingMessage = message
|
|
|
|
|
outgoingMessage.id = outgoingMessage.nextMessageID() // important that we update this here.
|
|
|
|
|
|
|
|
|
|
self.pendingMessages[outgoingMessage.id] = { response in
|
|
|
|
|
do {
|
|
|
|
|
let decodedResponse = try JSONDecoder().decode(HomeAssistantServer.Event<R>.self, from: response)
|
|
|
|
|
if let result = decodedResponse.result {
|
|
|
|
|
continuation.resume(returning: result)
|
|
|
|
|
} else if R.self != NoResult.self {
|
|
|
|
|
let error = decodedResponse.error ?? ErrorResult(code: "unknown", message: "unknown")
|
|
|
|
|
continuation.resume(throwing: error)
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
print("Response decoding error: \(error)")
|
|
|
|
|
continuation.resume(throwing: error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.pendingMessages.removeValue(forKey: outgoingMessage.id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let encodedMessage = try JSONEncoder().encode(outgoingMessage)
|
|
|
|
|
let jsonString = String(data: encodedMessage, encoding: .utf8)!
|
|
|
|
|
try await websocketTask?.send(.string(jsonString))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileprivate func partialEvents() throws -> AsyncStream<Liaison.Event> {
|
|
|
|
|
return AsyncStream { continuation in
|
|
|
|
|
let uuid = UUID()
|
|
|
|
|
self.continuations[uuid] = continuation
|
|
|
|
|
continuation.onTermination = { termination in
|
|
|
|
|
Task { await self.removeContinuation(uuid: uuid) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spawnWebsocketTaskIfNecessary()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func removeContinuation(uuid: UUID) {
|
|
|
|
|
print("removing continuation: \(uuid)")
|
|
|
|
|
self.continuations.removeValue(forKey: uuid)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func restartWebsocket() {
|
|
|
|
|
disconnect()
|
|
|
|
|
spawnWebsocketTaskIfNecessary()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func spawnWebsocketTaskIfNecessary() {
|
|
|
|
|
guard self.websocketTask == nil else { return }
|
|
|
|
|
|
|
|
|
|
let websocketTask = URLSession.shared.webSocketTask(with: websocketURL)
|
|
|
|
|
self.websocketTask = websocketTask
|
|
|
|
|
websocketTask.resume()
|
|
|
|
|
|
|
|
|
|
// Event Task
|
|
|
|
|
let decoder = JSONDecoder()
|
|
|
|
|
|
|
|
|
|
eventTask?.cancel()
|
|
|
|
|
eventTask = Task.detached { [weak self] in
|
|
|
|
|
guard let self else { return }
|
|
|
|
|
do {
|
|
|
|
|
while websocketTask.state == .running {
|
|
|
|
|
switch try await websocketTask.receive() {
|
|
|
|
|
case .string(let string):
|
|
|
|
|
let data = string.data(using: .utf8)!
|
|
|
|
|
let partialEvent = try decoder.decode(PartialEvent.self, from: data)
|
|
|
|
|
await handlePartialEvent(partialEvent, data: data)
|
|
|
|
|
case .data(let data):
|
|
|
|
|
let partialEvent = try decoder.decode(PartialEvent.self, from: data)
|
|
|
|
|
await handlePartialEvent(partialEvent, data: data)
|
|
|
|
|
default:
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print("Websocket not running: \(String(describing: websocketTask.error))")
|
|
|
|
|
} catch {
|
|
|
|
|
await yield(.error(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ping Task
|
|
|
|
|
pingTask?.cancel()
|
|
|
|
|
pingTask = Task.detached { [weak self] in
|
|
|
|
|
guard let self else { return }
|
|
|
|
|
guard let websocket = await self.websocketTask else { return }
|
|
|
|
|
|
|
|
|
|
var pinging = true
|
|
|
|
|
while pinging {
|
|
|
|
|
do {
|
|
|
|
|
try await Task.sleep(for: .seconds(5))
|
|
|
|
|
} catch { print("Task cancelled.") }
|
|
|
|
|
|
|
|
|
|
websocket.sendPing { error in
|
|
|
|
|
if let error {
|
|
|
|
|
pinging = false
|
|
|
|
|
print("Ping error: \(error). Disconnecting.")
|
|
|
|
|
Task {
|
|
|
|
|
await self.disconnect()
|
|
|
|
|
await self.yield(.disconnected(error))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
print("pong!")
|
|
|
|
|
Task {
|
|
|
|
|
await self.yield(.pong)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func handlePartialEvent(_ event: PartialEvent, data: Data) {
|
|
|
|
|
if event.type == .result {
|
|
|
|
|
guard let id = event.id else { return }
|
|
|
|
|
if let resultHandler = self.pendingMessages[id] {
|
|
|
|
|
resultHandler(data)
|
|
|
|
|
} else {
|
|
|
|
|
print("No result handler for id: \(id)")
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
yield(.partialEvent(event, data))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func yield(_ event: Event) {
|
|
|
|
|
for (_, continuation) in continuations {
|
|
|
|
|
continuation.yield(event)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum Event
|
|
|
|
|
{
|
|
|
|
|
case partialEvent(PartialEvent, Data)
|
|
|
|
|
case pong
|
|
|
|
|
|
|
|
|
|
case disconnected(any Error)
|
|
|
|
|
case error(any Error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-19 01:02:51 -07:00
|
|
|
// MARK: - Types
|
|
|
|
|
|
|
|
|
|
struct PartialEvent: Codable
|
|
|
|
|
{
|
|
|
|
|
let id: Int64?
|
|
|
|
|
let type: EventType
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Event<Result: Decodable>: Decodable
|
|
|
|
|
{
|
|
|
|
|
let id: Int?
|
|
|
|
|
let type: EventType
|
|
|
|
|
let result: Result?
|
|
|
|
|
let error: ErrorResult?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum EventType: String, Codable
|
|
|
|
|
{
|
|
|
|
|
case authRequired = "auth_required"
|
|
|
|
|
case authOK = "auth_ok"
|
|
|
|
|
case result = "result"
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-19 19:25:53 -07:00
|
|
|
struct ErrorResult: Codable, Swift.Error
|
2025-06-19 01:02:51 -07:00
|
|
|
{
|
|
|
|
|
let code: String
|
|
|
|
|
let message: String
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-19 19:25:53 -07:00
|
|
|
enum ServerError: Swift.Error
|
|
|
|
|
{
|
|
|
|
|
case disconnected
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-19 01:02:51 -07:00
|
|
|
// MARK: - Result Types
|
|
|
|
|
|
|
|
|
|
struct RegistryEntry: Codable
|
|
|
|
|
{
|
|
|
|
|
let id: String
|
|
|
|
|
let entityId: String
|
|
|
|
|
let name: String?
|
|
|
|
|
let labels: [String]
|
|
|
|
|
|
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
|
|
|
case id
|
|
|
|
|
case name
|
|
|
|
|
case labels
|
|
|
|
|
case entityId = "entity_id"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct EntityState: Decodable
|
|
|
|
|
{
|
|
|
|
|
let entityId: String
|
|
|
|
|
let state: State
|
|
|
|
|
|
|
|
|
|
enum State: Decodable {
|
|
|
|
|
case on
|
|
|
|
|
case off
|
|
|
|
|
case other(String)
|
|
|
|
|
|
|
|
|
|
init(from decoder: Decoder) throws {
|
|
|
|
|
let container = try decoder.singleValueContainer()
|
|
|
|
|
let stringValue = try container.decode(String.self)
|
|
|
|
|
|
|
|
|
|
switch stringValue.lowercased() {
|
|
|
|
|
case "on":
|
|
|
|
|
self = .on
|
|
|
|
|
case "off":
|
|
|
|
|
self = .off
|
|
|
|
|
default:
|
|
|
|
|
self = .other(stringValue)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Attributes: Decodable {
|
|
|
|
|
let friendlyName: String?
|
|
|
|
|
|
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
|
|
|
case friendlyName = "friendly_name"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
|
|
|
case entityId = "entity_id"
|
|
|
|
|
case state
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension HomeAssistantDevice
|
|
|
|
|
{
|
|
|
|
|
convenience init(entity: HomeAssistantServer.RegistryEntry, state: DeviceState) {
|
2025-06-19 19:25:53 -07:00
|
|
|
var type: DeviceType = .switch
|
|
|
|
|
if entity.labels.contains("control_panel_light") {
|
|
|
|
|
type = .light
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-19 01:02:51 -07:00
|
|
|
self.init(
|
|
|
|
|
name: entity.name?.replacingOccurrences(of: " Switch", with: "") ?? entity.entityId,
|
|
|
|
|
serial: entity.entityId,
|
2025-06-19 19:25:53 -07:00
|
|
|
type: type,
|
2025-06-19 01:02:51 -07:00
|
|
|
state: state
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typealias MessageID = Int64?
|
|
|
|
|
|
|
|
|
|
extension MessageID
|
|
|
|
|
{
|
2025-06-19 15:00:19 -07:00
|
|
|
private static var counter: Int64 = 1
|
2025-06-19 01:02:51 -07:00
|
|
|
static func next() -> Self {
|
2025-06-19 15:00:19 -07:00
|
|
|
OSAtomicIncrement64(&Self.counter)
|
2025-06-19 01:02:51 -07:00
|
|
|
return Self.counter
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Message Types
|
|
|
|
|
|
|
|
|
|
fileprivate protocol Message: Encodable
|
|
|
|
|
{
|
2025-06-19 19:25:53 -07:00
|
|
|
var id: MessageID { get set }
|
2025-06-19 01:02:51 -07:00
|
|
|
var type: String { get }
|
2025-06-19 19:25:53 -07:00
|
|
|
|
|
|
|
|
func nextMessageID() -> MessageID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension Message
|
|
|
|
|
{
|
|
|
|
|
func nextMessageID() -> MessageID { .next() }
|
2025-06-19 01:02:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileprivate struct RegistryEntriesMessage: Message
|
|
|
|
|
{
|
2025-06-19 19:25:53 -07:00
|
|
|
var id: MessageID = nil
|
2025-06-19 01:02:51 -07:00
|
|
|
let type: String = "config/entity_registry/list"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileprivate struct GetStatesMessage: Message
|
|
|
|
|
{
|
2025-06-19 19:25:53 -07:00
|
|
|
var id: MessageID = nil
|
2025-06-19 01:02:51 -07:00
|
|
|
let type: String = "get_states"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileprivate struct AuthMessage: Message
|
|
|
|
|
{
|
2025-06-19 19:25:53 -07:00
|
|
|
var id: MessageID = nil
|
2025-06-19 01:02:51 -07:00
|
|
|
let type: String = "auth"
|
|
|
|
|
let token: String
|
|
|
|
|
|
2025-06-19 19:25:53 -07:00
|
|
|
// No ID for auth messages
|
|
|
|
|
func nextMessageID() -> MessageID { nil }
|
|
|
|
|
|
2025-06-19 01:02:51 -07:00
|
|
|
enum CodingKeys: String, CodingKey {
|
|
|
|
|
case id
|
|
|
|
|
case type
|
|
|
|
|
case token = "access_token"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileprivate struct CallServiceMessage: Message
|
|
|
|
|
{
|
2025-06-19 19:25:53 -07:00
|
|
|
var id: MessageID = nil
|
2025-06-19 01:02:51 -07:00
|
|
|
let type: String = "call_service"
|
|
|
|
|
|
|
|
|
|
let service: Service
|
|
|
|
|
let domain: ServiceDomain
|
|
|
|
|
let target: Target
|
|
|
|
|
|
|
|
|
|
init(entityID: String, service: Service, domain: ServiceDomain = .switch) {
|
|
|
|
|
self.service = service
|
|
|
|
|
self.domain = domain
|
|
|
|
|
self.target = Target(entityID: entityID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum ServiceDomain: String, Codable
|
|
|
|
|
{
|
|
|
|
|
case `switch`
|
2025-06-19 19:25:53 -07:00
|
|
|
case light
|
2025-06-19 01:02:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum Service: String, Codable
|
|
|
|
|
{
|
|
|
|
|
case toggle
|
|
|
|
|
case turnOn = "turn_on"
|
|
|
|
|
case turnOff = "turn_off"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Target: Codable
|
|
|
|
|
{
|
|
|
|
|
let entityID: String
|
|
|
|
|
|
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
|
|
|
case entityID = "entity_id"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|