Skip to content

Commit

Permalink
Fixed a few apis related to energy sites
Browse files Browse the repository at this point in the history
Added new fields from products vehicle API
Added wallconnectos objects
fixed site_info api call
  • Loading branch information
jonasman committed Apr 5, 2024
1 parent bff66ee commit 36acb0c
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 24 deletions.
2 changes: 0 additions & 2 deletions Sources/TeslaSwift/Model/BatteryData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Foundation

// MARK: - Welcome
open class BatteryData: Codable {
open var siteName: String
open var energyLeft: Double
open var totalPackEnergy: Double
open var gridStatus: String
Expand All @@ -24,7 +23,6 @@ open class BatteryData: Codable {
open var powerReading: [PowerReading]

enum CodingKeys: String, CodingKey {
case siteName = "site_name"
case energyLeft = "energy_left"
case totalPackEnergy = "total_pack_energy"
case gridStatus = "grid_status"
Expand Down
33 changes: 30 additions & 3 deletions Sources/TeslaSwift/Model/EnergySite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ open class EnergySite: Codable {
}
open var assetSiteID: String?
open var components: Components?

open var goOffGridTestBannerEnabled: Bool?
open var stormModeEnabled: Bool?
open var powerwallOnboardingSettingsSet: String?
open var powerwallTeslaElectricInterestedIn: String?
open var vppTourEnabled: Bool?

// Also available in EnergySiteStatus
open var resourceType: String
open var siteName: String?
open var warpSiteNumber: String?
open var gatewayID: String?
open var energyLeft: Double?
open var totalPackEnergy: Double?
Expand All @@ -41,7 +46,7 @@ open class EnergySite: Codable {
enum CodingKeys: String, CodingKey {
case energySiteID = "energy_site_id"
case resourceType = "resource_type"
case siteName = "site_name"
case warpSiteNumber = "warp_site_number"
case id
case gatewayID = "gateway_id"
case assetSiteID = "asset_site_id"
Expand All @@ -54,6 +59,11 @@ open class EnergySite: Codable {
case syncGridAlertEnabled = "sync_grid_alert_enabled"
case breakerAlertEnabled = "breaker_alert_enabled"
case components
case goOffGridTestBannerEnabled = "go_off_grid_test_banner_enabled"
case stormModeEnabled = "storm_mode_enabled"
case powerwallOnboardingSettingsSet = "powerwall_onboarding_settings_set"
case powerwallTeslaElectricInterestedIn = "powerwall_tesla_electric_interested_in"
case vppTourEnabled = "vpp_tour_enabled"
}

// MARK: - Components
Expand All @@ -65,6 +75,7 @@ open class EnergySite: Codable {
open var grid: Bool
open var loadMeter: Bool
open var marketType: String
open var wallConnectors: [WallConnectors]?

enum CodingKeys: String, CodingKey {
case battery
Expand All @@ -74,6 +85,22 @@ open class EnergySite: Codable {
case grid
case loadMeter = "load_meter"
case marketType = "market_type"
case wallConnectors = "wall_connectors"
}
}
}

open class WallConnectors: Codable {
open var deviceId: String?
open var din: String?
open var partNumber: String?
open var isActive: Bool?

enum CodingKeys: String, CodingKey {
case deviceId = "device_id"
case din
case partNumber = "part_number"
case isActive = "is_active"
}

}
2 changes: 2 additions & 0 deletions Sources/TeslaSwift/Model/EnergySiteInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ open class EnergySiteInfo: Codable {
open var batteryType: String?
open var configurable: Bool?
open var gridServicesEnabled: Bool?
open var wallConnectors: [WallConnectors]?

enum CodingKeys: String, CodingKey {
case solar
Expand All @@ -83,6 +84,7 @@ open class EnergySiteInfo: Codable {
case batteryType = "battery_type"
case configurable
case gridServicesEnabled = "grid_services_enabled"
case wallConnectors = "wall_connectors"
}
}

Expand Down
35 changes: 32 additions & 3 deletions Sources/TeslaSwift/Model/Vehicle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ open class Vehicle: Codable {
}
set {
guard let newValue = newValue?.id else { idInt = nil; return }
idInt = Int64(newValue)
idInt = newValue
}
}
open var idInt: Int64?
Expand All @@ -33,7 +33,16 @@ open class Vehicle: Codable {
open var tokens: [String]?
open var vehicleID: Int64?
open var vin: String?


// fields via /api/1/products
open var userId: Int64?
open var accessType: String?
open var cachedData: String?
open var apiVersion: Int?
open var bleAutopairEnrolled: Bool?
open var commandSigning: Bool?
open var releaseNotesSupported: Bool?

// MARK: Codable protocol

enum CodingKeys: String, CodingKey {
Expand All @@ -51,6 +60,13 @@ open class Vehicle: Codable {
case tokens = "tokens"
case vehicleID = "vehicle_id"
case vin = "vin"
case userId = "user_id"
case accessType = "access_type"
case cachedData = "cached_data"
case apiVersion = "api_version"
case bleAutopairEnrolled = "ble_autopair_enrolled"
case commandSigning = "command_signing"
case releaseNotesSupported = "release_notes_supported"

}

Expand Down Expand Up @@ -86,6 +102,13 @@ open class Vehicle: Codable {
tokens = try? container.decode([String].self, forKey: .tokens)
vehicleID = try? container.decode(Int64.self, forKey: .vehicleID)
vin = try? container.decode(String.self, forKey: .vin)
userId = try? container.decode(Int64.self, forKey: .userId)
accessType = try? container.decode(String.self, forKey: .accessType)
cachedData = try? container.decode(String.self, forKey: .cachedData)
apiVersion = try? container.decode(Int.self, forKey: .apiVersion)
bleAutopairEnrolled = try? container.decode(Bool.self, forKey: .bleAutopairEnrolled)
commandSigning = try? container.decode(Bool.self, forKey: .commandSigning)
releaseNotesSupported = try? container.decode(Bool.self, forKey: .releaseNotesSupported)
}

public func encode(to encoder: Encoder) throws {
Expand All @@ -104,7 +127,13 @@ open class Vehicle: Codable {
try container.encodeIfPresent(tokens, forKey: .tokens)
try container.encodeIfPresent(vehicleID, forKey: .vehicleID)
try container.encodeIfPresent(vin, forKey: .vin)

try container.encodeIfPresent(userId, forKey: .userId)
try container.encodeIfPresent(accessType, forKey: .accessType)
try container.encodeIfPresent(cachedData, forKey: .cachedData)
try container.encodeIfPresent(apiVersion, forKey: .apiVersion)
try container.encodeIfPresent(bleAutopairEnrolled, forKey: .bleAutopairEnrolled)
try container.encodeIfPresent(commandSigning, forKey: .commandSigning)
try container.encodeIfPresent(releaseNotesSupported, forKey: .releaseNotesSupported)
}
}

Expand Down
3 changes: 0 additions & 3 deletions Sources/TeslaSwift/Model/VehicleExtended.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import Foundation

open class VehicleExtended: Vehicle {
open var userId: Int64?
open var chargeState: ChargeState?
open var climateState: ClimateState?
open var driveState: DriveState?
Expand All @@ -31,7 +30,6 @@ open class VehicleExtended: Vehicle {

required public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
userId = try container.decodeIfPresent(Int64.self, forKey: .userId)
chargeState = try container.decodeIfPresent(ChargeState.self, forKey: .chargeState)
climateState = try container.decodeIfPresent(ClimateState.self, forKey: .climateState)
driveState = try container.decodeIfPresent(DriveState.self, forKey: .driveState)
Expand All @@ -47,7 +45,6 @@ open class VehicleExtended: Vehicle {

override open func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(userId, forKey: .userId)
try container.encodeIfPresent(chargeState, forKey: .chargeState)
try container.encodeIfPresent(climateState, forKey: .climateState)
try container.encodeIfPresent(driveState, forKey: .driveState)
Expand Down
2 changes: 1 addition & 1 deletion Sources/TeslaSwift/TeslaEndpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ extension Endpoint {
case .getEnergySiteLiveStatus(let siteID):
return "/api/1/energy_sites/\(siteID.id)/live_status"
case .getEnergySiteInfo(let siteID):
return "/api/1/energy_sites/\(siteID)/site_info"
return "/api/1/energy_sites/\(siteID.id)/site_info"
case .getEnergySiteHistory(let siteID, _):
return "/api/1/energy_sites/\(siteID.id)/history"
case .getBatteryStatus(let batteryID):
Expand Down
22 changes: 12 additions & 10 deletions TeslaSwiftDemo/FirstViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class FirstViewController: UIViewController, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!

var data:[Vehicle]?
var data: [Product]?

override func viewDidLoad() {
super.viewDidLoad()

Expand All @@ -38,8 +38,8 @@ class FirstViewController: UIViewController, UITableViewDataSource {

func getVehicles() {
Task { @MainActor in
let vehicles = try await api.getVehicles()
self.data = vehicles
let products = try await api.getProducts()
self.data = products
self.tableView.reloadData()
}
}
Expand All @@ -52,11 +52,11 @@ class FirstViewController: UIViewController, UITableViewDataSource {

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

let vehicle = data![(indexPath as NSIndexPath).row]
cell.textLabel?.text = vehicle.displayName
cell.detailTextLabel?.text = vehicle.vin
let product = data![(indexPath as NSIndexPath).row]

cell.textLabel?.text = product.vehicle?.displayName ?? ""
cell.detailTextLabel?.text = product.vehicle?.vin ?? product.energySite?.id


return cell
}
Expand All @@ -68,7 +68,9 @@ class FirstViewController: UIViewController, UITableViewDataSource {

if let indexPath = tableView.indexPathForSelectedRow {
let vc = segue.destination as! VehicleViewController
vc.vehicle = data![indexPath.row]
if let vehicle = data![indexPath.row].vehicle {
vc.vehicle = vehicle
}
}

}
Expand Down
2 changes: 1 addition & 1 deletion TeslaSwiftDemo/SecondViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class SecondViewController: UIViewController, UITableViewDataSource {
cell.textLabel?.text = vehicle.displayName
cell.detailTextLabel?.text = vehicle.vin
} else if let energySite = product.energySite {
cell.textLabel?.text = energySite.siteName
cell.textLabel?.text = energySite.id
cell.detailTextLabel?.text = energySite.resourceType
} else {
cell.textLabel?.text = "Unknown"
Expand Down
2 changes: 1 addition & 1 deletion TeslaSwiftDemo/VehicleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class VehicleViewController: UIViewController {
guard let vehicle = vehicle else { return }
Task { @MainActor in
let vehicle2 = try await api.getVehicle(vehicle)
self.textView.text = "Inside temp: \(vehicle2.jsonString!)"
self.textView.text = "Vehicle: \(vehicle2.jsonString!)"
}
}

Expand Down

0 comments on commit 36acb0c

Please sign in to comment.