Skip to content

Commit

Permalink
[netatmo] Room-Thing offline after restart (openhab#13467)
Browse files Browse the repository at this point in the history
* Correcting issue by late loading of the capability.
* Handle MAXIMUM_USAGE_REACHED at ApiBridgeHandler level.

Signed-off-by: clinique <gael@lhopital.org>
  • Loading branch information
clinique authored and psmedley committed Feb 23, 2023
1 parent 1cd19ab commit d9eeeb8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,24 @@ public class RoomActions implements ThingActions {
SetpointMode.HOME);

private @Nullable CommonInterface handler;
private Optional<EnergyCapability> energy = Optional.empty();

public RoomActions() {
logger.debug("Netatmo RoomActions service created");
}

private Optional<EnergyCapability> getEnergyCapability() {
CommonInterface localHandler = handler;
if (localHandler != null) {
return localHandler.getHomeCapability(EnergyCapability.class);
}
return Optional.empty();
}

@Override
public void setThingHandler(@Nullable ThingHandler handler) {
if (handler instanceof CommonInterface) {
CommonInterface commonHandler = (CommonInterface) handler;
this.handler = commonHandler;
energy = commonHandler.getHomeCapability(EnergyCapability.class);
}
}

Expand All @@ -77,7 +83,8 @@ public void setThermRoomTempSetpoint(
logger.info("Temperature provided but no endtime given, action ignored");
return;
}
energy.ifPresent(cap -> cap.setRoomThermTemp(roomHandler.getId(), temp, endTime, SetpointMode.MANUAL));
getEnergyCapability()
.ifPresent(cap -> cap.setRoomThermTemp(roomHandler.getId(), temp, endTime, SetpointMode.MANUAL));
}

@RuleAction(label = "@text/actionSetThermRoomModeSetpointLabel", description = "@text/actionSetThermRoomModeSetpointDesc")
Expand Down Expand Up @@ -116,7 +123,7 @@ public void setThermRoomModeSetpoint(

long setpointEnd = targetEndTime;
SetpointMode setpointMode = targetMode;
energy.ifPresent(cap -> cap.setRoomThermTemp(roomHandler.getId(), 0, setpointEnd, setpointMode));
getEnergyCapability().ifPresent(cap -> cap.setRoomThermTemp(roomHandler.getId(), 0, setpointEnd, setpointMode));
}

public static void setThermRoomTempSetpoint(ThingActions actions, @Nullable Double temp, @Nullable Long endTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.openhab.binding.netatmo.internal.api.RestManager;
import org.openhab.binding.netatmo.internal.api.SecurityApi;
import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.Scope;
import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.ServiceError;
import org.openhab.binding.netatmo.internal.config.ApiHandlerConfiguration;
import org.openhab.binding.netatmo.internal.config.BindingConfiguration;
import org.openhab.binding.netatmo.internal.config.ConfigurationLevel;
Expand Down Expand Up @@ -245,6 +246,12 @@ public synchronized <T> T executeUri(URI uri, HttpMethod method, Class<T> clazz,
throw new NetatmoException(error);
}
return deserializer.deserialize(clazz, responseBody);
} catch (NetatmoException e) {
if (e.getStatusCode() == ServiceError.MAXIMUM_USAGE_REACHED) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
prepareReconnection(null, null);
}
throw e;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
Expand Down

0 comments on commit d9eeeb8

Please sign in to comment.