Skip to content

Commit

Permalink
Adds querying vehicle config endpoint for model identifications, fixe…
Browse files Browse the repository at this point in the history
…s small bug to add modely to supported things. (openhab#5)

Signed-off-by: digitaldan <dan@digitaldan.com>
  • Loading branch information
digitaldan authored and kaikreuzer committed Nov 10, 2019
1 parent 9113643 commit 4707495
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class TeslaBindingConstants {
public static final String GUI_STATE = "gui_settings";
public static final String MOBILE_ENABLED_STATE = "mobile_enabled";
public static final String VEHICLE_STATE = "vehicle_state";
public static final String VEHICLE_CONFIG = "vehicle_config";

public static final String BINDING_ID = "tesla";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
public class TeslaHandlerFactory extends BaseThingHandlerFactory {

public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
.of(THING_TYPE_ACCOUNT, THING_TYPE_MODELS, THING_TYPE_MODEL3, THING_TYPE_MODELX)
.of(THING_TYPE_ACCOUNT, THING_TYPE_MODELS, THING_TYPE_MODEL3, THING_TYPE_MODELX, THING_TYPE_MODELY)
.collect(Collectors.toSet());

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
package org.openhab.binding.tesla.internal.discovery;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNull;
Expand All @@ -30,7 +29,10 @@
import org.openhab.binding.tesla.internal.handler.TeslaAccountHandler;
import org.openhab.binding.tesla.internal.handler.VehicleListener;
import org.openhab.binding.tesla.internal.protocol.Vehicle;
import org.openhab.binding.tesla.internal.protocol.VehicleConfig;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This service is used by {@link TeslaAccountHandler} instances in order to
Expand All @@ -42,17 +44,7 @@
@Component(service = ThingHandlerService.class)
public class TeslaVehicleDiscoveryService extends AbstractDiscoveryService
implements DiscoveryService, VehicleListener, ThingHandlerService {

private static final Map<String, ThingTypeUID> modelMap = new HashMap<>();
static {
// see https://tesla-api.timdorr.com/vehicle/optioncodes
modelMap.put("MDLS", TeslaBindingConstants.THING_TYPE_MODELS);
modelMap.put("MS03", TeslaBindingConstants.THING_TYPE_MODELS);
modelMap.put("MS04", TeslaBindingConstants.THING_TYPE_MODELS);
modelMap.put("MDLX", TeslaBindingConstants.THING_TYPE_MODELX);
modelMap.put("MDL3", TeslaBindingConstants.THING_TYPE_MODEL3);
modelMap.put("MDLY", TeslaBindingConstants.THING_TYPE_MODELY);
}
private final Logger logger = LoggerFactory.getLogger(TeslaVehicleDiscoveryService.class);

public TeslaVehicleDiscoveryService() throws IllegalArgumentException {
super(TeslaHandlerFactory.SUPPORTED_THING_TYPES_UIDS, 10, true);
Expand Down Expand Up @@ -90,21 +82,27 @@ public void deactivate() {
}

@Override
public void vehicleFound(Vehicle vehicle) {
ThingTypeUID type = identifyModel(vehicle);
public void vehicleFound(Vehicle vehicle, VehicleConfig vehicleConfig) {
ThingTypeUID type = identifyModel(vehicleConfig);
ThingUID thingUID = new ThingUID(type, handler.getThing().getUID(), vehicle.vin);
DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withLabel(vehicle.display_name)
.withBridge(handler.getThing().getUID()).withProperty(TeslaBindingConstants.VIN, vehicle.vin).build();
thingDiscovered(discoveryResult);
}

private ThingTypeUID identifyModel(Vehicle vehicle) {
String options = vehicle.option_codes;
for (Map.Entry<String, ThingTypeUID> entry : modelMap.entrySet()) {
if (options.contains(entry.getKey())) {
return entry.getValue();
}
private ThingTypeUID identifyModel(VehicleConfig vehicleConfig) {
logger.debug("Found a {} vehicle", vehicleConfig.car_type);
switch (vehicleConfig.car_type) {
case "models":
return TeslaBindingConstants.THING_TYPE_MODELS;
case "modelx":
return TeslaBindingConstants.THING_TYPE_MODELX;
case "model3":
return TeslaBindingConstants.THING_TYPE_MODEL3;
case "modely":
return TeslaBindingConstants.THING_TYPE_MODELY;
default:
return null;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.openhab.binding.tesla.internal.protocol.TokenRequestRefreshToken;
import org.openhab.binding.tesla.internal.protocol.TokenResponse;
import org.openhab.binding.tesla.internal.protocol.Vehicle;
import org.openhab.binding.tesla.internal.protocol.VehicleConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -215,8 +216,13 @@ protected Vehicle[] queryVehicles() {
Vehicle[] vehicleArray = gson.fromJson(jsonObject.getAsJsonArray("response"), Vehicle[].class);

for (Vehicle vehicle : vehicleArray) {
String responseString = invokeAndParse(vehicle.id, VEHICLE_CONFIG, null, dataRequestTarget);
if (StringUtils.isBlank(responseString)) {
continue;
}
VehicleConfig vehicleConfig = gson.fromJson(responseString, VehicleConfig.class);
for (VehicleListener listener : vehicleListeners) {
listener.vehicleFound(vehicle);
listener.vehicleFound(vehicle, vehicleConfig);
}
for (Thing vehicleThing : getThing().getThings()) {
if (vehicle.vin.equals(vehicleThing.getConfiguration().get(VIN))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.openhab.binding.tesla.internal.handler;

import org.openhab.binding.tesla.internal.protocol.Vehicle;
import org.openhab.binding.tesla.internal.protocol.VehicleConfig;

/**
* The {@link VehicleListener} interface can be implemented by classes that want to be informed about
Expand All @@ -27,5 +28,5 @@ public interface VehicleListener {
*
* @param vehicle a vehicle that was found within an account.
*/
void vehicleFound(Vehicle vehicle);
void vehicleFound(Vehicle vehicle, VehicleConfig vehicleConfig);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright (c) 2010-2019 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.tesla.internal.protocol;

/**
* The {@link VehicleConfig} is a data structure to capture
* vehicle configuration variables sent by the Tesla Vehicle
*
* @author Dan Cunningham - Initial contribution
*/
public class VehicleConfig {
public boolean can_accept_navigation_requests;
public boolean can_actuate_trunks;
public boolean eu_vehicle;
public boolean has_air_suspension;
public boolean has_ludicrous_mode;
public boolean motorized_charge_port;
public boolean plg;
public boolean rhd;
public boolean use_range_badging;
public int rear_seat_heaters;
public int rear_seat_type;
public int sun_roof_installed;
public long timestamp;
public String car_special_type;
public String car_type;
public String charge_port_type;
public String exterior_color;
public String roof_color;
public String spoiler_type;
public String third_row_seats;
public String trim_badging;
public String wheel_type;

VehicleConfig() {

}
}

0 comments on commit 4707495

Please sign in to comment.