Skip to content

Commit

Permalink
Now deleting timer if set to less than 60 seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
seime committed Oct 5, 2019
1 parent f463344 commit 8823247
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 13 deletions.
2 changes: 1 addition & 1 deletion bundles/org.openhab.binding.sensibo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Or you can find it during discovery.
| mode | R/W | String | Current mode (cool, heat, etc, actual modes provided provided by the API) being active |
| fanLevel | R/W | String | Current fan level (low, auto etc, actual levels provided provided by the API |
| swingMode | R/W | String | Current swing mode (actual modes provided provided by the API |
| timer | R/W | Number | Number of seconds until AC is switched off automatically |
| timer | R/W | Number | Number of seconds until AC is switched off automatically. Setting to a value less than 60 seconds will cancel timer |

## Full Example

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* 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.sensibo.internal.dto.deletetimer;

/**
* @author Arne Seime - Initial contribution
*/
public class DeleteTimerReponse {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 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.sensibo.internal.dto.deletetimer;

import org.eclipse.jetty.http.HttpMethod;
import org.openhab.binding.sensibo.internal.dto.AbstractRequest;

/**
* @author Arne Seime - Initial contribution
*/
public class DeleteTimerRequest extends AbstractRequest {
public DeleteTimerRequest(String podId) {
this.podId = podId;
}

public transient String podId; // Transient fields are ignored by gson

@Override
public String getRequestUrl() {
return String.format("/v1/pods/%s/timer/", podId);
}

@Override
public String getMethod() {
return HttpMethod.DELETE.asString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.openhab.binding.sensibo.internal.client.RequestLogger;
import org.openhab.binding.sensibo.internal.config.SensiboAccountConfiguration;
import org.openhab.binding.sensibo.internal.dto.AbstractRequest;
import org.openhab.binding.sensibo.internal.dto.deletetimer.DeleteTimerRequest;
import org.openhab.binding.sensibo.internal.dto.poddetails.GetPodsDetailsRequest;
import org.openhab.binding.sensibo.internal.dto.poddetails.PodDetails;
import org.openhab.binding.sensibo.internal.dto.pods.GetPodsRequest;
Expand Down Expand Up @@ -278,6 +279,11 @@ private Request buildSetTimerRequest(SetTimerRequest setTimerRequest) {
return req;
}

private Request buildDeleteTimerRequest(DeleteTimerRequest deleteTimerRequest) {
final Request req = buildRequest(deleteTimerRequest);
return req;
}

private Request buildRequest(final AbstractRequest req) {
Request request = httpClient.newRequest(API_ENDPOINT + req.getRequestUrl()).param("apiKey", config.apiKey)
.method(req.getMethod());
Expand Down Expand Up @@ -314,22 +320,32 @@ public void updateSensiboSkyAcState(@Nullable final String macAddress, String pr
}
}

public void updateSensiboSkyTimer(@Nullable final String macAddress, int secondsFromNow) {
public void updateSensiboSkyTimer(@Nullable final String macAddress, @Nullable Integer secondsFromNow) {
Optional<SensiboSky> optionalHeater = model.findSensiboSkyByMacAddress(macAddress);
if (optionalHeater.isPresent()) {
SensiboSky sensiboSky = optionalHeater.get();
try {
if (secondsFromNow != null && secondsFromNow >= 60) {
org.openhab.binding.sensibo.internal.dto.poddetails.AcState offState = new org.openhab.binding.sensibo.internal.dto.poddetails.AcState(
sensiboSky.getAcState());
offState.on = false;

SetTimerRequest setTimerRequest = new SetTimerRequest(sensiboSky.getId(), secondsFromNow / 60,
offState);
Request request = buildSetTimerRequest(setTimerRequest);
// No data in response
sendRequest(request, setTimerRequest, new TypeToken<SetTimerReponse>() {
}.getType());

org.openhab.binding.sensibo.internal.dto.poddetails.AcState offState = new org.openhab.binding.sensibo.internal.dto.poddetails.AcState(
sensiboSky.getAcState());
offState.on = false;
} else {
DeleteTimerRequest setTimerRequest = new DeleteTimerRequest(sensiboSky.getId());
Request request = buildDeleteTimerRequest(setTimerRequest);
// No data in response
sendRequest(request, setTimerRequest, new TypeToken<SetTimerReponse>() {
}.getType());

}

SetTimerRequest setAcStatePropertyRequest = new SetTimerRequest(sensiboSky.getId(), secondsFromNow / 60,
offState);
Request request = buildSetTimerRequest(setAcStatePropertyRequest);
// No data in response
sendRequest(request, setAcStatePropertyRequest, new TypeToken<SetTimerReponse>() {
}.getType());
} catch (SensiboCommunicationException e) {
logger.debug("Error setting timer for {}", macAddress, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private void updateAcState(SensiboSky sensiboSky, String property, Object value)
}
}

private void updateTimer(SensiboSky sensiboSky, int secondsFromNowUntilSwitchOff) {
private void updateTimer(SensiboSky sensiboSky, @Nullable Integer secondsFromNowUntilSwitchOff) {
final SensiboAccountHandler accountHandler = getAccountHandler();
if (accountHandler != null) {
accountHandler.updateSensiboSkyTimer(config.macAddress, secondsFromNowUntilSwitchOff);
Expand Down Expand Up @@ -169,14 +169,16 @@ protected void handleCommand(@NonNull final ChannelUID channelUID, @NonNull fina
}
} else if (CHANNEL_TIMER.equals(channelUID.getId())) {
if (command instanceof RefreshType) {
if (unit.getTimer() != null && unit.getTimer().secondsRemaining >= 60) {
if (unit.getTimer() != null && unit.getTimer().secondsRemaining > 0) {
updateState(channelUID, new DecimalType(unit.getTimer().secondsRemaining));
} else {
updateState(channelUID, UnDefType.UNDEF);
}
} else if (command instanceof DecimalType) {
final DecimalType newValue = (DecimalType) command;
updateTimer(unit, newValue.intValue());
} else {
updateTimer(unit, null);
}
}
} else {
Expand Down

0 comments on commit 8823247

Please sign in to comment.