[netatmo] Room-Thing offline after restart (#13467)

* Correcting issue by late loading of the capability.
* Handle MAXIMUM_USAGE_REACHED at ApiBridgeHandler level.

Signed-off-by: clinique <gael@lhopital.org>
This commit is contained in:
Gaël L'hopital 2022-10-01 12:50:22 +02:00 committed by GitHub
parent 7b8ae535e9
commit cedf6d9035
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 4 deletions

View File

@ -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);
}
}
@ -77,7 +83,8 @@ public class RoomActions implements ThingActions {
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")
@ -116,7 +123,7 @@ public class RoomActions implements ThingActions {
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) {

View File

@ -46,6 +46,7 @@ import org.openhab.binding.netatmo.internal.api.NetatmoException;
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;
@ -245,6 +246,12 @@ public class ApiBridgeHandler extends BaseBridgeHandler {
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());