2020-03-14 19:53:18 -07:00
|
|
|
//
|
|
|
|
|
// 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
|
|
|
|
|
|
2020-05-01 00:16:10 -07:00
|
|
|
enum CodingKeys: String, CodingKey
|
|
|
|
|
{
|
2020-03-14 19:53:18 -07:00
|
|
|
case name = "label"
|
|
|
|
|
case id = "id"
|
|
|
|
|
case attributes = "attributes"
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-01 00:16:10 -07:00
|
|
|
enum AttributesKeys: String, CodingKey
|
|
|
|
|
{
|
2020-03-14 19:53:18 -07:00
|
|
|
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)
|
|
|
|
|
|
2026-07-27 16:23:40 -07:00
|
|
|
let attributes = try values.nestedContainer(
|
|
|
|
|
keyedBy: Self.AttributesKeys.self,
|
|
|
|
|
forKey: .attributes
|
|
|
|
|
)
|
2020-03-14 19:53:18 -07:00
|
|
|
state = try attributes.decode(DeviceState.self, forKey: .switch)
|
|
|
|
|
}
|
|
|
|
|
}
|