HubitatServer: Adds HubitatServer type and adds it to the multiplexer
This commit is contained in:
47
XIONControlPanel/Models/HubitatDevice.swift
Normal file
47
XIONControlPanel/Models/HubitatDevice.swift
Normal file
@@ -0,0 +1,47 @@
|
||||
//
|
||||
// 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, forKey: .attributes)
|
||||
state = try attributes.decode(DeviceState.self, forKey: .switch)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user