[tado] Automatically reconnect offline Things if possible (#12868)

* [tado] reconnect offline things if possible & allowed
* [tado] bridge should ping the server every polling cycle

Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
This commit is contained in:
Andrew Fiddian-Green 2022-06-04 20:37:30 +02:00 committed by GitHub
parent 44b1d0c0c1
commit 3f4d1f041d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -34,9 +34,11 @@ import org.openhab.binding.tado.internal.config.TadoHomeConfig;
import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.OnOffType;
import org.openhab.core.thing.Bridge; import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus; import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail; import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.binding.BaseBridgeHandler; import org.openhab.core.thing.binding.BaseBridgeHandler;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.types.Command; import org.openhab.core.types.Command;
import org.openhab.core.types.RefreshType; import org.openhab.core.types.RefreshType;
import org.openhab.core.types.State; import org.openhab.core.types.State;
@ -85,35 +87,44 @@ public class TadoHomeHandler extends BaseBridgeHandler {
} }
private void initializeBridgeStatusAndPropertiesIfOffline() { private void initializeBridgeStatusAndPropertiesIfOffline() {
Bridge bridge = getBridge(); if (getThing().getStatus() == ThingStatus.ONLINE) {
if (bridge != null && bridge.getStatus() == ThingStatus.ONLINE) { for (Thing thing : getThing().getThings()) {
return; ThingHandler handler = thing.getHandler();
if ((handler instanceof BaseHomeThingHandler) && (thing.getStatus() == ThingStatus.OFFLINE)
&& (thing.getStatusInfo().getStatusDetail() == ThingStatusDetail.COMMUNICATION_ERROR)) {
scheduler.submit(() -> handler.bridgeStatusChanged(getThing().getStatusInfo()));
}
}
} }
try { try {
// Get user info to verify successful authentication and connection to server // if we are already online, don't make unnecessary calls on the server
User user = api.showUser(); if (getThing().getStatus() != ThingStatus.ONLINE) {
if (user == null) { // Get user info to verify successful authentication and connection to server
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, User user = api.showUser();
"Cannot connect to server. Username and/or password might be invalid"); if (user == null) {
return; updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Cannot connect to server. Username and/or password might be invalid");
return;
}
List<UserHomes> homes = user.getHomes();
if (homes == null || homes.isEmpty()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"User does not have access to any home");
return;
}
Integer firstHomeId = homes.get(0).getId();
if (firstHomeId == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Missing Home Id");
return;
}
homeId = firstHomeId.longValue();
} }
List<UserHomes> homes = user.getHomes(); // but always make one server call as a 'ping' to confirm we are really still online
if (homes == null || homes.isEmpty()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"User does not have access to any home");
return;
}
Integer firstHomeId = homes.get(0).getId();
if (firstHomeId == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Missing Home Id");
return;
}
homeId = firstHomeId.longValue();
HomeInfo homeInfo = api.showHome(homeId); HomeInfo homeInfo = api.showHome(homeId);
TemperatureUnit temperatureUnit = org.openhab.binding.tado.internal.api.model.TemperatureUnit.FAHRENHEIT == homeInfo TemperatureUnit temperatureUnit = org.openhab.binding.tado.internal.api.model.TemperatureUnit.FAHRENHEIT == homeInfo
.getTemperatureUnit() ? TemperatureUnit.FAHRENHEIT : TemperatureUnit.CELSIUS; .getTemperatureUnit() ? TemperatureUnit.FAHRENHEIT : TemperatureUnit.CELSIUS;