mirror of
https://github.com/home-assistant/iOS.git
synced 2026-06-16 13:26:27 -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 --> ## Screenshots <!-- If this is a user-facing change not in the frontend, please include screenshots in light and dark mode. --> <img width="800" height="480" alt="Simulator Screenshot - Daily tester 2 - 2026-03-31 at 16 45 28" src="https://github.com/user-attachments/assets/927764c8-9de7-49e9-98c4-8cc8fd0bcf6b" /> ## 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. -->
70 lines
2.0 KiB
Swift
70 lines
2.0 KiB
Swift
import Foundation
|
|
|
|
public enum EntityColorAttributesParser {
|
|
public typealias ParsedAttributes = (
|
|
colorMode: String?,
|
|
rgbColor: [Int]?,
|
|
hsColor: [Double]?
|
|
)
|
|
|
|
public static func parse(from attributes: [String: Any]?) -> ParsedAttributes {
|
|
guard let attributes else {
|
|
return (nil, nil, nil)
|
|
}
|
|
|
|
return (
|
|
colorMode: attributes["color_mode"] as? String,
|
|
rgbColor: parseRGBColor(from: attributes["rgb_color"]),
|
|
hsColor: parseHSColor(from: attributes["hs_color"])
|
|
)
|
|
}
|
|
|
|
public static func parseRGBColor(from value: Any?) -> [Int]? {
|
|
if let rgb = value as? [Int], rgb.count == 3 {
|
|
return rgb
|
|
}
|
|
|
|
if let rgbAny = value as? [Any] {
|
|
let ints = rgbAny.compactMap { value -> Int? in
|
|
if let int = value as? Int {
|
|
return int
|
|
}
|
|
if let number = value as? NSNumber {
|
|
return number.intValue
|
|
}
|
|
if let string = value as? String {
|
|
return Int(string)
|
|
}
|
|
return nil
|
|
}
|
|
return ints.count == 3 ? ints : nil
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
public static func parseHSColor(from value: Any?) -> [Double]? {
|
|
if let hs = value as? [Double], hs.count >= 2 {
|
|
return Array(hs.prefix(2))
|
|
}
|
|
|
|
if let hsAny = value as? [Any] {
|
|
let doubles = hsAny.compactMap { value -> Double? in
|
|
if let double = value as? Double {
|
|
return double
|
|
}
|
|
if let number = value as? NSNumber {
|
|
return number.doubleValue
|
|
}
|
|
if let string = value as? String {
|
|
return Double(string)
|
|
}
|
|
return nil
|
|
}
|
|
return doubles.count >= 2 ? Array(doubles.prefix(2)) : nil
|
|
}
|
|
|
|
return nil
|
|
}
|
|
}
|