Skip to content

Commit

Permalink
Adding Pause-Resume Endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: fifipil909 <[email protected]>
  • Loading branch information
fifipil909 committed Mar 5, 2023
1 parent e119bb3 commit ecf7d29
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bundles/org.openhab.binding.renault/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ You require your MyRenault credential, locale and VIN for your MyRenault registe
| batterylevel | Number | State of the battery in % | Yes |
| batterystatusupdated | DateTime | Timestamp of the last battery status update | Yes |
| chargingmode | String | Charging mode. always_charging or schedule_mode | No |
| pausemode | String | Pause mode. Pause or Resume | No |
| chargingstatus | String | Charging status | Yes |
| chargingremainingtime | Number:Time | Charging time remaining | Yes |
| plugstatus | String | Status of charging plug | Yes |
Expand Down Expand Up @@ -77,6 +78,7 @@ sitemap renaultcar label="Renault Car" {
Default icon="poweroutlet" item=RenaultCar_PlugStatus
Default icon="switch" item=RenaultCar_ChargingStatus
Selection icon="switch" item=RenaultCar_ChargingMode mappings=[SCHEDULE_MODE="Schedule mode",ALWAYS_CHARGING="Instant charge"]
Selection item=RenaultCar_PauseMode mappings=[RESUME="Resume",PAUSE="Pause"] icon="switch"
Default item=RenaultCar_ChargingTimeRemaining
Default icon="pressure" item=RenaultCar_EstimatedRange
Default icon="pressure" item=RenaultCar_Odometer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class RenaultBindingConstants {
public static final String CHANNEL_BATTERY_LEVEL = "batterylevel";
public static final String CHANNEL_BATTERY_STATUS_UPDATED = "batterystatusupdated";
public static final String CHANNEL_CHARGING_MODE = "chargingmode";
public static final String CHANNEL_PAUSE_MODE = "pausemode";
public static final String CHANNEL_CHARGING_STATUS = "chargingstatus";
public static final String CHANNEL_CHARGING_REMAINING_TIME = "chargingremainingtime";
public static final String CHANNEL_ESTIMATED_RANGE = "estimatedrange";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class Car {
private boolean disableHvac = false;
private boolean disableLockStatus = false;

private PauseMode pausemode = PauseMode.UNKNOWN;
private ChargingStatus chargingStatus = ChargingStatus.UNKNOWN;
private ChargingMode chargingMode = ChargingMode.UNKNOWN;
private PlugStatus plugStatus = PlugStatus.UNKNOWN;
Expand All @@ -69,6 +70,12 @@ public enum ChargingMode {
ALWAYS_CHARGING
}

public enum PauseMode {
RESUME,
PAUSE,
UNKNOWN
}

public enum ChargingStatus {
NOT_IN_CHARGE,
WAITING_FOR_A_PLANNED_CHARGE,
Expand Down Expand Up @@ -151,6 +158,19 @@ public void setHVACStatus(JsonObject responseJson) {
}
}

public void setPauseStatus(JsonObject responseJson) {
try {
JsonObject attributes = getAttributes(responseJson);
if (attributes != null) {
if (attributes.get("action") != null) {
pausemode = PauseMode.valueOf(attributes.get("action").getAsString());
}
}
} catch (IllegalStateException | ClassCastException e) {
logger.warn("Error {} parsing Pause Status: {}", e.getMessage(), responseJson);
}
}

public void setCockpit(JsonObject responseJson) {
try {
JsonObject attributes = getAttributes(responseJson);
Expand Down Expand Up @@ -298,6 +318,10 @@ public ChargingMode getChargingMode() {
return chargingMode;
}

public PauseMode getPauseMode() {
return pausemode;
}

public @Nullable Integer getChargingRemainingTime() {
return chargingRemainingTime;
}
Expand Down Expand Up @@ -346,6 +370,10 @@ public void setChargeMode(ChargingMode mode) {
}
}

public void setPauseMode(PauseMode pausemode) {
this.pausemode = pausemode;
}

private @Nullable JsonObject getAttributes(JsonObject responseJson)
throws IllegalStateException, ClassCastException {
if (responseJson.get("data") != null && responseJson.get("data").getAsJsonObject().get("attributes") != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.jetty.util.Fields;
import org.openhab.binding.renault.internal.RenaultConfiguration;
import org.openhab.binding.renault.internal.api.Car.ChargingMode;
import org.openhab.binding.renault.internal.api.Car.PauseMode;
import org.openhab.binding.renault.internal.api.exceptions.RenaultActionException;
import org.openhab.binding.renault.internal.api.exceptions.RenaultException;
import org.openhab.binding.renault.internal.api.exceptions.RenaultForbiddenException;
Expand Down Expand Up @@ -274,6 +275,15 @@ public void actionChargeMode(ChargingMode mode)
"{\"data\":{\"type\":\"ChargeMode\",\"attributes\":{\"action\":\"" + apiMode + "\"}}}");
}

public void actionPause(PauseMode mode)
throws RenaultForbiddenException, RenaultNotImplementedException, RenaultActionException {
final String apiMode = PauseMode.PAUSE.equals(mode) ? "pause" : "resume";
final String path = "/commerce/v1/accounts/" + kamereonaccountId + "/kamereon/kcm/v1/vehicles/" + config.vin
+ "/charge/pause-resume?country=" + getCountry(config);
postKamereonRequest(path,
"{\"data\":{\"type\":\"ChargePauseResume\",\"attributes\":{\"action\":\"" + apiMode + "\"}}}");
}

private void postKamereonRequest(final String path, final String content)
throws RenaultForbiddenException, RenaultNotImplementedException, RenaultActionException {
Request request = httpClient.newRequest(this.constants.getKamereonRootUrl() + path).method(HttpMethod.POST)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.openhab.binding.renault.internal.RenaultConfiguration;
import org.openhab.binding.renault.internal.api.Car;
import org.openhab.binding.renault.internal.api.Car.ChargingMode;
import org.openhab.binding.renault.internal.api.Car.PauseMode;
import org.openhab.binding.renault.internal.api.MyRenaultHttpSession;
import org.openhab.binding.renault.internal.api.exceptions.RenaultActionException;
import org.openhab.binding.renault.internal.api.exceptions.RenaultException;
Expand Down Expand Up @@ -205,6 +206,30 @@ public void handleCommand(ChannelUID channelUID, Command command) {
}
}
break;
case RenaultBindingConstants.CHANNEL_PAUSE_MODE:
if (command instanceof RefreshType) {
reschedulePollingJob();
} else if (command instanceof StringType) {
try {
PauseMode newMode = PauseMode.valueOf(command.toString());
if (!PauseMode.UNKNOWN.equals(newMode)) {
MyRenaultHttpSession httpSession = new MyRenaultHttpSession(this.config, httpClient);
try {
httpSession.initSesssion(car);
httpSession.actionPause(newMode);
car.setPauseMode(newMode);
updateState(CHANNEL_PAUSE_MODE, new StringType(newMode.toString()));
} catch (Exception e) {
logger.warn("Error My Renault Http Session.", e);
Thread.currentThread().interrupt();
}
}
} catch (IllegalArgumentException e) {
logger.warn("Invalid Pause Mode {}.", command.toString());
return;
}
}
break;
default:
if (command instanceof RefreshType) {
reschedulePollingJob();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ channel-type.renault.chargingmode.label = Charging Mode
channel-type.renault.chargingmode.state.option.UNKNOWN = Unknown
channel-type.renault.chargingmode.state.option.SCHEDULE_MODE = Schedule mode
channel-type.renault.chargingmode.state.option.ALWAYS_CHARGING = Instant charge
channel-type.renault.pausemode.label = Pause Mode
channel-type.renault.pausemode.state.option.UNKNOWN = Unknown
channel-type.renault.pausemode.state.option.RESUME = Resume
channel-type.renault.pausemode.state.option.PAUSE = Pause
channel-type.renault.chargingremainingtime.label = Charging Time Remaining
channel-type.renault.chargingstatus.label = Charging Status
channel-type.renault.chargingstatus.state.option.NOT_IN_CHARGE = Not charging
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ channel-type.renault.chargingmode.label = Mode Charge
channel-type.renault.chargingmode.state.option.UNKNOWN = Inconnu
channel-type.renault.chargingmode.state.option.SCHEDULE_MODE = Mode planification
channel-type.renault.chargingmode.state.option.ALWAYS_CHARGING = Charge instantanée
channel-type.renault.pausemode.label = Pause Mode
channel-type.renault.pausemode.state.option.UNKNOWN = Inconnu
channel-type.renault.pausemode.state.option.RESUME = Démarrer
channel-type.renault.pausemode.state.option.PAUSE = Pause
channel-type.renault.chargingremainingtime.label = Temps Recharge Restant
channel-type.renault.chargingstatus.label = Statut Charge
channel-type.renault.chargingstatus.state.option.NOT_IN_CHARGE = Pas en charge
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<channel id="plugstatus" typeId="plugstatus"/>
<channel id="chargingstatus" typeId="chargingstatus"/>
<channel id="chargingmode" typeId="chargingmode"/>
<channel id="pausemode" typeId="pausemode"/>
<channel id="chargingremainingtime" typeId="chargingremainingtime"/>
<channel id="estimatedrange" typeId="estimatedrange"/>
<channel id="odometer" typeId="odometer"/>
Expand Down Expand Up @@ -148,6 +149,17 @@
</options>
</state>
</channel-type>
<channel-type id="pausemode">
<item-type>String</item-type>
<label>Pause Mode</label>
<state readOnly="false">
<options>
<option value="UNKNOWN">Unknown</option>
<option value="PAUSE">Pause</option>
<option value="RESUME">Resume</option>
</options>
</state>
</channel-type>
<channel-type id="chargingremainingtime">
<item-type>Number:Time</item-type>
<label>Charging Time Remaining</label>
Expand Down

0 comments on commit ecf7d29

Please sign in to comment.