mirror of
https://github.com/home-assistant/iOS.git
synced 2026-06-16 04:16:39 -05:00
<!-- 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 --> landingpage: https://github.com/home-assistant/landingpage/pull/195 android: https://github.com/home-assistant/android/pull/6970 ## 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. -->
56 lines
1.8 KiB
Swift
56 lines
1.8 KiB
Swift
import Foundation
|
|
import ObjectMapper
|
|
import PromiseKit
|
|
import Version
|
|
|
|
public struct DiscoveredHomeAssistant: ImmutableMappable, Equatable {
|
|
public static let defaultVersion = Version(major: 2022, minor: 4)
|
|
|
|
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") {
|
|
let serverURL = manualURL.serverBaseURL()
|
|
self.version = Self.defaultVersion
|
|
self.uuid = "autogenerated-\(UUID().uuidString)"
|
|
self.internalOrExternalURL = serverURL
|
|
if serverURL.isLocal {
|
|
self.internalURL = serverURL
|
|
self.externalURL = nil
|
|
} else {
|
|
self.internalURL = nil
|
|
self.externalURL = serverURL
|
|
}
|
|
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
|
|
}
|
|
}
|
|
}
|