Skip to content

Commit

Permalink
Add HVAC requests
Browse files Browse the repository at this point in the history
  • Loading branch information
JagCesar committed Jul 15, 2018
1 parent c6b73ad commit 5cee5f2
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Source/Requests/HVACRequests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Foundation

public struct HVACRequest: RequestProtocol {
public enum HVACState {
case on
case off
}
typealias CompletionType = Bool
var path: String {
switch state {
case .on:
return "/api/1/vehicles/\(vehicleIdentifier)/command/auto_conditioning_start"
case .off:
return "/api/1/vehicles/\(vehicleIdentifier)/command/auto_conditioning_stop"
}
}
let method = WebRequest.RequestMethod.post
let accessToken: String
let vehicleIdentifier: String
let state: HVACState

public init(accessToken: String, vehicleIdentifier: String, state: HVACState) {
self.accessToken = accessToken
self.vehicleIdentifier = vehicleIdentifier
self.state = state
}

public func execute(completion: @escaping (Result<Bool>) -> Void) {
WebRequest.request(
path: path,
method: method,
accessToken: accessToken) { response, error in
if let error = error {
DispatchQueue.main.async {
completion(Result.failure(error))
}
} else if let response = response as? [String: [String: Any]],
let resultBool = response["response"]?["result"] as? Bool {
DispatchQueue.main.async {
completion(Result.success(resultBool))
}
} else {
DispatchQueue.main.async {
completion(Result.failure(APIError()))
}
}
}
}
}
18 changes: 18 additions & 0 deletions Tesla-API.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@
FCB9329520FBB50B00D19874 /* SetTemperaturesRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCB9329420FBB50B00D19874 /* SetTemperaturesRequestTests.swift */; };
FCB9329620FBB50B00D19874 /* SetTemperaturesRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCB9329420FBB50B00D19874 /* SetTemperaturesRequestTests.swift */; };
FCB9329720FBB50B00D19874 /* SetTemperaturesRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCB9329420FBB50B00D19874 /* SetTemperaturesRequestTests.swift */; };
FCB9329920FBB91100D19874 /* HVACRequests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCB9329820FBB91100D19874 /* HVACRequests.swift */; };
FCB9329A20FBB91100D19874 /* HVACRequests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCB9329820FBB91100D19874 /* HVACRequests.swift */; };
FCB9329B20FBB91100D19874 /* HVACRequests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCB9329820FBB91100D19874 /* HVACRequests.swift */; };
FCB9329C20FBB91100D19874 /* HVACRequests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCB9329820FBB91100D19874 /* HVACRequests.swift */; };
FCB9329E20FBB97500D19874 /* HVACRequestsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCB9329D20FBB97500D19874 /* HVACRequestsTests.swift */; };
FCB9329F20FBB97500D19874 /* HVACRequestsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCB9329D20FBB97500D19874 /* HVACRequestsTests.swift */; };
FCB932A020FBB97500D19874 /* HVACRequestsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCB9329D20FBB97500D19874 /* HVACRequestsTests.swift */; };
FCD161971F1E79F0003FE381 /* Tesla-API.h in Headers */ = {isa = PBXBuildFile; fileRef = FCD161901F1E793F003FE381 /* Tesla-API.h */; settings = {ATTRIBUTES = (Public, ); }; };
FCE31678205E3284004EF4B0 /* TeslaAPITestsEmpty.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCE31677205E3284004EF4B0 /* TeslaAPITestsEmpty.swift */; };
FCE3167A205E3284004EF4B0 /* TeslaAPI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FCD1615D1F1E7844003FE381 /* TeslaAPI.framework */; };
Expand Down Expand Up @@ -167,6 +174,8 @@
FCB9328B20FB9B3A00D19874 /* ClimateStateRequestTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClimateStateRequestTests.swift; sourceTree = "<group>"; };
FCB9328F20FBADE700D19874 /* SetTemperaturesRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetTemperaturesRequest.swift; sourceTree = "<group>"; };
FCB9329420FBB50B00D19874 /* SetTemperaturesRequestTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetTemperaturesRequestTests.swift; sourceTree = "<group>"; };
FCB9329820FBB91100D19874 /* HVACRequests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HVACRequests.swift; sourceTree = "<group>"; };
FCB9329D20FBB97500D19874 /* HVACRequestsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HVACRequestsTests.swift; sourceTree = "<group>"; };
FCD1615D1F1E7844003FE381 /* TeslaAPI.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TeslaAPI.framework; sourceTree = BUILT_PRODUCTS_DIR; };
FCD1618D1F1E793F003FE381 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
FCD1618F1F1E793F003FE381 /* Vehicle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Vehicle.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -254,6 +263,7 @@
children = (
FCE826051F8C092C00B72C08 /* ChargeStateRequestTests.swift */,
FCB9328B20FB9B3A00D19874 /* ClimateStateRequestTests.swift */,
FCB9329D20FBB97500D19874 /* HVACRequestsTests.swift */,
FC27C97E1F4995240064E1AE /* ListVehiclesRequestTests.swift */,
FC27C97F1F4995240064E1AE /* LockRequestTests.swift */,
FC27C9801F4995240064E1AE /* MobileEnabledForRequestTests.swift */,
Expand Down Expand Up @@ -302,6 +312,7 @@
FCE946461F2CF10E004913FC /* AuthenticateRequest.swift */,
FCE825FB1F8C068F00B72C08 /* ChargeStateRequest.swift */,
FCB9328120FB913700D19874 /* ClimateStateRequest.swift */,
FCB9329820FBB91100D19874 /* HVACRequests.swift */,
FC80D5011F2CE6FD0013F6DF /* ListVehiclesRequest.swift */,
FC75EC521F2E4AD7007ED697 /* LockRequest.swift */,
FC1B80961F49883500D24310 /* MobileEnabledForVehicleRequest.swift */,
Expand Down Expand Up @@ -674,6 +685,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
FCB9329E20FBB97500D19874 /* HVACRequestsTests.swift in Sources */,
FC27C98B1F4995240064E1AE /* OpenChargePortRequestTests.swift in Sources */,
FCB9328C20FB9B3A00D19874 /* ClimateStateRequestTests.swift in Sources */,
FC27C9821F4995240064E1AE /* ListVehiclesRequestTests.swift in Sources */,
Expand All @@ -693,6 +705,7 @@
files = (
FC48F9AE1F1E7FCE00D2400B /* Vehicle.swift in Sources */,
FC48F9AA1F1E7FBA00D2400B /* WebRequest.swift in Sources */,
FCB9329A20FBB91100D19874 /* HVACRequests.swift in Sources */,
FC48F9A61F1E7FB600D2400B /* TeslaAPI.swift in Sources */,
FCB9328820FB917F00D19874 /* ClimateState.swift in Sources */,
FCE825FD1F8C068F00B72C08 /* ChargeStateRequest.swift in Sources */,
Expand All @@ -716,6 +729,7 @@
files = (
FC48F9AD1F1E7FCE00D2400B /* Vehicle.swift in Sources */,
FC48F9A91F1E7FBA00D2400B /* WebRequest.swift in Sources */,
FCB9329B20FBB91100D19874 /* HVACRequests.swift in Sources */,
FC48F9A51F1E7FB500D2400B /* TeslaAPI.swift in Sources */,
FCB9328920FB917F00D19874 /* ClimateState.swift in Sources */,
FCE825FE1F8C068F00B72C08 /* ChargeStateRequest.swift in Sources */,
Expand All @@ -739,6 +753,7 @@
files = (
FC48F9AC1F1E7FCD00D2400B /* Vehicle.swift in Sources */,
FC48F9A81F1E7FBA00D2400B /* WebRequest.swift in Sources */,
FCB9329C20FBB91100D19874 /* HVACRequests.swift in Sources */,
FC48F9A41F1E7FB400D2400B /* TeslaAPI.swift in Sources */,
FCB9328A20FB917F00D19874 /* ClimateState.swift in Sources */,
FCE825FF1F8C068F00B72C08 /* ChargeStateRequest.swift in Sources */,
Expand All @@ -760,6 +775,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
FCB9329F20FBB97500D19874 /* HVACRequestsTests.swift in Sources */,
FC27C98C1F4995240064E1AE /* OpenChargePortRequestTests.swift in Sources */,
FCB9328D20FB9B3A00D19874 /* ClimateStateRequestTests.swift in Sources */,
FC27C9831F4995240064E1AE /* ListVehiclesRequestTests.swift in Sources */,
Expand All @@ -777,6 +793,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
FCB932A020FBB97500D19874 /* HVACRequestsTests.swift in Sources */,
FC27C98D1F4995250064E1AE /* OpenChargePortRequestTests.swift in Sources */,
FCB9328E20FB9B3A00D19874 /* ClimateStateRequestTests.swift in Sources */,
FC27C9841F4995240064E1AE /* ListVehiclesRequestTests.swift in Sources */,
Expand All @@ -796,6 +813,7 @@
files = (
FC48F9AF1F1E7FCE00D2400B /* Vehicle.swift in Sources */,
FC99DFD31F1FB41400013FE9 /* Token.swift in Sources */,
FCB9329920FBB91100D19874 /* HVACRequests.swift in Sources */,
FC48F9AB1F1E7FBB00D2400B /* WebRequest.swift in Sources */,
FCB9328720FB917F00D19874 /* ClimateState.swift in Sources */,
FCE825FC1F8C068F00B72C08 /* ChargeStateRequest.swift in Sources */,
Expand Down
48 changes: 48 additions & 0 deletions Tests/Request Tests/HVACRequestsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import XCTest
@testable import TeslaAPI

extension TeslaAPITests {
func testEnableHvac() {
let waitExpectation = expectation(description: "Enable HVAC")

HVACRequest(
accessToken: accessToken(),
vehicleIdentifier: vehicleIdentifier(),
state: .on).execute { result in
XCTAssert(Thread.isMainThread)
switch result {
case .success(let result):
if result {
waitExpectation.fulfill()
} else {
XCTFail()
}
case .failure(_):
XCTFail()
}
}
waitForExpectations(timeout: 30, handler: nil)
}

func testDisableHvac() {
let waitExpectation = expectation(description: "Disable HVAC")

HVACRequest(
accessToken: accessToken(),
vehicleIdentifier: vehicleIdentifier(),
state: .off).execute { result in
XCTAssert(Thread.isMainThread)
switch result {
case .success(let result):
if result {
waitExpectation.fulfill()
} else {
XCTFail()
}
case .failure(_):
XCTFail()
}
}
waitForExpectations(timeout: 30, handler: nil)
}
}

0 comments on commit 5cee5f2

Please sign in to comment.