iOS/Sources/Shared/API/Responses/DiscoveredHomeAssistant.swift
Bruno Pantaleão Gonçalves ce14424585
Best effort to define if a manually entry URL is local or remote (#3900)
<!-- 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. -->

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-10-15 13:15:56 +02:00

53 lines
1.6 KiB
Swift

import Foundation
import ObjectMapper
import PromiseKit
import Version
public struct DiscoveredHomeAssistant: ImmutableMappable, Equatable {
public var uuid: String?
public var version: Version
public var internalOrExternalURL: URL
public var externalURL: URL?
public var internalURL: URL?
public var locationName: String
public var bonjourName: String?
public init(manualURL: URL, name: String = "Home") {
self.version = Version(major: 2022, minor: 4)
self.uuid = "autogenerated-\(UUID().uuidString)"
self.internalOrExternalURL = manualURL
if manualURL.isLocal {
self.internalURL = manualURL
self.externalURL = nil
} else {
self.internalURL = nil
self.externalURL = manualURL
}
self.locationName = name
}
private enum TransformError: Error {
case missingUsableURL
}
public init(map: Map) throws {
self.uuid = try map.value("uuid")
self.version = try map.value("version", using: VersionTransform())
self.externalURL = try? map.value("external_url", using: URLTransform())
self.internalURL = try? map.value("internal_url", using: URLTransform())
if externalURL == nil, internalURL == nil {
// compatibility with HA <0.110
self.externalURL = try? map.value("base_url", using: URLTransform())
}
self.locationName = (try? map.value("location_name")) ?? "Home"
if let url = internalURL ?? externalURL {
self.internalOrExternalURL = url
} else {
throw TransformError.missingUsableURL
}
}
}