diff --git a/Sources/Extensions/Combine/TeslaSwift+Combine.swift b/Sources/Extensions/Combine/TeslaSwift+Combine.swift index ab0233b..0a18e02 100644 --- a/Sources/Extensions/Combine/TeslaSwift+Combine.swift +++ b/Sources/Extensions/Combine/TeslaSwift+Combine.swift @@ -11,228 +11,225 @@ import Combine import TeslaSwift extension TeslaSwift { - public func revokeWeb() -> Future { - let future = Future { (subscriber: @escaping (Result) -> Void) in + public func revokeToken() -> Future { + Future { promise in Task { do { - let result = try await self.revokeWeb() - subscriber(.success(result)) + let result = try await self.revokeToken() + promise(.success(result)) } catch let error { - subscriber(.failure(error)) + promise(.failure(error)) } } } - return future } public func getVehicles() -> Future<[Vehicle], Error> { - let future = Future<[Vehicle], Error> { (subscriber: @escaping (Result<[Vehicle], Error>) -> Void) in + Future { promise in Task { do { let result = try await self.getVehicles() - subscriber(.success(result)) + promise(.success(result)) } catch let error { - subscriber(.failure(error)) + promise(.failure(error)) } } } - return future } public func getVehicle(_ vehicleID: String) -> Future { - let future = Future { (subscriber: @escaping (Result) -> Void) in + Future { promise in Task { do { let result = try await self.getVehicle(vehicleID) - subscriber(.success(result)) + promise(.success(result)) } catch let error { - subscriber(.failure(error)) + promise(.failure(error)) } } } - return future } public func getVehicle(_ vehicle: Vehicle) -> Future { - let future = Future { (subscriber: @escaping (Result) -> Void) in + Future { promise in Task { do { let result = try await self.getVehicle(vehicle) - subscriber(.success(result)) + promise(.success(result)) } catch let error { - subscriber(.failure(error)) + promise(.failure(error)) } } } - return future } public func getAllData(_ vehicle: Vehicle) -> Future { - let future = Future { (subscriber: @escaping (Result) -> Void) in + Future { promise in Task { do { let result = try await self.getAllData(vehicle) - subscriber(.success(result)) + promise(.success(result)) } catch let error { - subscriber(.failure(error)) + promise(.failure(error)) } } } - return future } public func getVehicleMobileAccessState(_ vehicle: Vehicle) -> Future { - let future = Future { (subscriber: @escaping (Result) -> Void) in + Future { promise in Task { do { let result = try await self.getVehicleMobileAccessState(vehicle) - subscriber(.success(result)) + promise(.success(result)) } catch let error { - subscriber(.failure(error)) + promise(.failure(error)) } } } - return future } public func wakeUp(_ vehicle: Vehicle) -> Future { - let future = Future { (subscriber: @escaping (Result) -> Void) in + Future { promise in Task { do { let result = try await self.wakeUp(vehicle) - subscriber(.success(result)) + promise(.success(result)) } catch let error { - subscriber(.failure(error)) + promise(.failure(error)) } } } - return future } public func sendCommandToVehicle(_ vehicle: Vehicle, command: VehicleCommand) -> Future { - let future = Future { (subscriber: @escaping (Result) -> Void) in + Future { promise in Task { do { let result = try await self.sendCommandToVehicle(vehicle, command: command) - subscriber(.success(result)) + promise(.success(result)) } catch let error { - subscriber(.failure(error)) + promise(.failure(error)) + } + } + } + } + + public func getNearbyChargingSite(vehicle: Vehicle) -> Future { + Future { promise in + Task { + do { + let result = try await self.getNearbyChargingSites(vehicle) + promise(.success(result)) + } catch let error { + promise(.failure(error)) } } } - return future } public func getProducts() -> Future<[Product], Error> { - let future = Future<[Product], Error> { (subscriber: @escaping (Result<[Product], Error>) -> Void) in + Future { promise in Task { do { let result = try await self.getProducts() - subscriber(.success(result)) + promise(.success(result)) } catch let error { - subscriber(.failure(error)) + promise(.failure(error)) } } } - return future } public func getEnergySiteStatus(siteID: String) -> Future { - let future = Future { (subscriber: @escaping (Result) -> Void) in + Future { promise in Task { do { let result = try await self.getEnergySiteStatus(siteID: siteID) - subscriber(.success(result)) + promise(.success(result)) } catch let error { - subscriber(.failure(error)) + promise(.failure(error)) } } } - return future } public func getEnergySiteLiveStatus(siteID: String) -> Future { - let future = Future { (subscriber: @escaping (Result) -> Void) in + Future { promise in Task { do { let result = try await self.getEnergySiteLiveStatus(siteID: siteID) - subscriber(.success(result)) + promise(.success(result)) } catch let error { - subscriber(.failure(error)) + promise(.failure(error)) } } } - return future } public func getEnergySiteInfo(siteID: String) -> Future { - let future = Future { (subscriber: @escaping (Result) -> Void) in + Future { promise in Task { do { let result = try await self.getEnergySiteInfo(siteID: siteID) - subscriber(.success(result)) + promise(.success(result)) } catch let error { - subscriber(.failure(error)) + promise(.failure(error)) } } } - return future } public func getEnergySiteHistory(siteID: String, period: EnergySiteHistory.Period) -> Future { - let future = Future { (subscriber: @escaping (Result) -> Void) in + Future { promise in Task { do { let result = try await self.getEnergySiteHistory(siteID: siteID, period: period) - subscriber(.success(result)) + promise(.success(result)) } catch let error { - subscriber(.failure(error)) + promise(.failure(error)) } } } - return future } public func getBatteryStatus(batteryID: String) -> Future { - let future = Future { (subscriber: @escaping (Result) -> Void) in + Future { promise in Task { do { let result = try await self.getBatteryStatus(batteryID: batteryID) - subscriber(.success(result)) + promise(.success(result)) } catch let error { - subscriber(.failure(error)) + promise(.failure(error)) } } } - return future } public func getBatteryData(batteryID: String) -> Future { - let future = Future { (subscriber: @escaping (Result) -> Void) in + Future { promise in Task { do { let result = try await self.getBatteryData(batteryID: batteryID) - subscriber(.success(result)) + promise(.success(result)) } catch let error { - subscriber(.failure(error)) + promise(.failure(error)) } } } - return future } public func getBatteryPowerHistory(batteryID: String) -> Future { - let future = Future { (subscriber: @escaping (Result) -> Void) in + Future { promise in Task { do { let result = try await self.getBatteryPowerHistory(batteryID: batteryID) - subscriber(.success(result)) + promise(.success(result)) } catch let error { - subscriber(.failure(error)) + promise(.failure(error)) } } } - return future } } diff --git a/Sources/TeslaSwift/Model/EnergySite.swift b/Sources/TeslaSwift/Model/EnergySite.swift index 02d9d64..b90fa18 100644 --- a/Sources/TeslaSwift/Model/EnergySite.swift +++ b/Sources/TeslaSwift/Model/EnergySite.swift @@ -19,14 +19,14 @@ open class EnergySite: Codable { // Also available in EnergySiteStatus open var resourceType: String - open var siteName: String - open var gatewayID: String - open var energyLeft: Double - open var totalPackEnergy: Double - open var percentageCharged: Double - open var batteryType: String - open var backupCapable: Bool - open var batteryPower: Double + open var siteName: String? + open var gatewayID: String? + open var energyLeft: Double? + open var totalPackEnergy: Double? + open var percentageCharged: Double? + open var batteryType: String? + open var backupCapable: Bool? + open var batteryPower: Double? open var syncGridAlertEnabled: Bool open var breakerAlertEnabled: Bool @@ -51,9 +51,9 @@ open class EnergySite: Codable { // MARK: - Components open class Components: Codable { open var battery: Bool - open var batteryType: String + open var batteryType: String? open var solar: Bool - open var solarType: String + open var solarType: String? open var grid: Bool open var loadMeter: Bool open var marketType: String diff --git a/Sources/TeslaSwift/Model/EnergySiteInfo.swift b/Sources/TeslaSwift/Model/EnergySiteInfo.swift index 570a50b..ea5b0b8 100644 --- a/Sources/TeslaSwift/Model/EnergySiteInfo.swift +++ b/Sources/TeslaSwift/Model/EnergySiteInfo.swift @@ -11,19 +11,19 @@ import Foundation // MARK: - EnergySiteInfo open class EnergySiteInfo: Codable { open var id: String - open var siteName: String - open var backupReservePercent: Double - open var defaultRealMode: String + open var siteName: String? + open var backupReservePercent: Double? + open var defaultRealMode: String? open var installationDate: Date - open var version: String - open var batteryCount: Int - open var nameplatePower: Double - open var nameplateEnergy: Double + open var version: String? + open var batteryCount: Int? + open var nameplatePower: Double? + open var nameplateEnergy: Double? open var installationTimeZone: String - open var offGridVehicleChargingReservePercent: Double + open var offGridVehicleChargingReservePercent: Double? open var userSettings: UserSettings - open var touSettings: TOUSettings + open var touSettings: TOUSettings? open var components: Components enum CodingKeys: String, CodingKey { @@ -45,25 +45,25 @@ open class EnergySiteInfo: Codable { // MARK: - Components open class Components: Codable { open var solar: Bool - open var solarType: String + open var solarType: String? open var battery: Bool open var grid: Bool open var backup: Bool open var gateway: String open var loadMeter: Bool - open var touCapable: Bool - open var stormModeCapable: Bool + open var touCapable: Bool? + open var stormModeCapable: Bool? open var flexEnergyRequestCapable: Bool open var carChargingDataSupported: Bool open var offGridVehicleChargingReserveSupported: Bool open var vehicleChargingPerformanceViewEnabled: Bool open var vehicleChargingSolarOffsetViewEnabled: Bool open var batterySolarOffsetViewEnabled: Bool - open var setIslandingModeEnabled: Bool - open var backupTimeRemainingEnabled: Bool - open var batteryType: String - open var configurable: Bool - open var gridServicesEnabled: Bool + open var setIslandingModeEnabled: Bool? + open var backupTimeRemainingEnabled: Bool? + open var batteryType: String? + open var configurable: Bool? + open var gridServicesEnabled: Bool? enum CodingKeys: String, CodingKey { case solar @@ -114,7 +114,7 @@ open class EnergySiteInfo: Codable { // MARK: - UserSettings open class UserSettings: Codable { - open var stormModeEnabled: Bool + open var stormModeEnabled: Bool? open var syncGridAlertEnabled: Bool open var breakerAlertEnabled: Bool