Files
Xaibatsu-Control-Panel/XIONControlPanel/Models/HubitatDevice.swift

53 lines
1.2 KiB
Swift

//
// HubitatDevice.swift
// XIONControlPanel
//
// Created by James Magahern on 3/14/20.
// Copyright © 2020 XION. All rights reserved.
//
import Foundation
public struct HubitatDevice : Device
{
var name: String = ""
var serial: String = ""
var type: DeviceType = .switch
var state: DeviceState = .off
var id: Int = -1
enum CodingKeys: String, CodingKey
{
case name = "label"
case id = "id"
case attributes = "attributes"
}
enum AttributesKeys: String, CodingKey
{
case power
case dataType
case `switch`
}
}
extension HubitatDevice : Decodable
{
public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
let idStr = try values.decode(String.self, forKey: .id)
id = Int(idStr)!
serial = String(format: "hubitat:%d", id)
name = try values.decode(String.self, forKey: .name)
let attributes = try values.nestedContainer(
keyedBy: Self.AttributesKeys.self,
forKey: .attributes
)
state = try attributes.decode(DeviceState.self, forKey: .switch)
}
}