HubitatServer: Adds HubitatServer type and adds it to the multiplexer

This commit is contained in:
2020-03-14 19:53:18 -07:00
parent 918818cd99
commit 9bff3dda48
7 changed files with 153 additions and 15 deletions

View File

@@ -8,15 +8,15 @@
import Foundation
enum DeviceState
enum DeviceState : String, Codable
{
case off
case on
case off = "off"
case on = "on"
}
enum DeviceType
enum DeviceType : String, Codable
{
case `switch`
case `switch` = "switch"
}
protocol Device

View 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)
}
}