diff --git a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/FreeplugManager.java b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/FreeplugManager.java index 5591135e2a..f3ab7f46a1 100644 --- a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/FreeplugManager.java +++ b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/FreeplugManager.java @@ -15,9 +15,9 @@ package org.openhab.binding.freeboxos.internal.api.rest; import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.THING_FREEPLUG; import java.util.List; -import java.util.Optional; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.freeboxos.internal.api.FreeboxException; import org.openhab.binding.freeboxos.internal.api.Response; @@ -73,8 +73,8 @@ public class FreeplugManager extends RestManager { return get(Networks.class).stream().map(Network::members).flatMap(List::stream).toList(); } - public Optional getPlug(MACAddress mac) throws FreeboxException { - return getPlugs().stream().filter(plug -> plug.id.equals(mac)).findFirst(); + public @Nullable Freeplug getPlug(MACAddress mac) throws FreeboxException { + return getPlugs().stream().filter(plug -> plug.id.equals(mac)).findFirst().orElse(null); } public void reboot(MACAddress mac) throws FreeboxException { diff --git a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/FreeplugHandler.java b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/FreeplugHandler.java index be4939da8b..c16a64b158 100644 --- a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/FreeplugHandler.java +++ b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/FreeplugHandler.java @@ -25,10 +25,13 @@ import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.binding.freeboxos.internal.action.FreeplugActions; import org.openhab.binding.freeboxos.internal.api.FreeboxException; import org.openhab.binding.freeboxos.internal.api.rest.FreeplugManager; +import org.openhab.binding.freeboxos.internal.api.rest.FreeplugManager.Freeplug; import org.openhab.core.library.types.QuantityType; import org.openhab.core.library.unit.Units; import org.openhab.core.thing.Channel; import org.openhab.core.thing.Thing; +import org.openhab.core.thing.ThingStatus; +import org.openhab.core.thing.ThingStatusDetail; import org.openhab.core.thing.binding.ThingHandlerService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,17 +50,21 @@ public class FreeplugHandler extends ApiConsumerHandler { public FreeplugHandler(Thing thing) { super(thing); + statusDrivenByBridge = false; + } + + private MACAddress getCheckMac() throws FreeboxException { + MACAddress mac = getMac(); + if (mac == null) { + throw new FreeboxException("MAC address is undefined for the thing " + thing.getUID()); + } + return mac; } @Override void initializeProperties(Map properties) throws FreeboxException { - MACAddress mac = getMac(); - if (mac == null) { - throw new FreeboxException( - "initializeProperties is not possible because MAC address is undefined for the thing " - + thing.getUID()); - } - getManager(FreeplugManager.class).getPlug(mac).ifPresent(plug -> { + MACAddress mac = getCheckMac(); + if (getManager(FreeplugManager.class).getPlug(mac) instanceof Freeplug plug) { properties.put(Thing.PROPERTY_MODEL_ID, plug.model()); properties.put(ROLE, plug.netRole().name()); properties.put(NET_ID, plug.netId()); @@ -73,17 +80,15 @@ public class FreeplugHandler extends ApiConsumerHandler { updateThing(editThing().withChannels(channels).build()); } } - }); + } else { + throw new FreeboxException("Freeplug is absent"); + } } @Override protected void internalPoll() throws FreeboxException { - MACAddress mac = getMac(); - if (mac == null) { - throw new FreeboxException( - "internalPoll is not possible because MAC address is undefined for the thing " + thing.getUID()); - } - getManager(FreeplugManager.class).getPlug(mac).ifPresent(plug -> { + MACAddress mac = getCheckMac(); + if (getManager(FreeplugManager.class).getPlug(mac) instanceof Freeplug plug) { updateChannelDateTimeState(LAST_SEEN, Instant.now().minusSeconds(plug.inactive())); updateChannelString(LINE_STATUS, plug.ethPortStatus()); @@ -91,7 +96,14 @@ public class FreeplugHandler extends ApiConsumerHandler { updateRateChannel(RATE + "-down", plug.rxRate()); updateRateChannel(RATE + "-up", plug.txRate()); - }); + if (plug.hasNetwork()) { + updateStatus(ThingStatus.ONLINE); + } else { + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "@text/info-plug-not-reachable"); + } + } else { + throw new FreeboxException("Freeplug is absent"); + } } private void updateRateChannel(String channel, int rate) { @@ -100,17 +112,12 @@ public class FreeplugHandler extends ApiConsumerHandler { } public void reset() { - MACAddress mac = getMac(); - if (mac == null) { - logger.warn("Freeplug restart is not possible because MAC address is undefined for the thing {}", - thing.getUID()); - return; - } try { + MACAddress mac = getCheckMac(); getManager(FreeplugManager.class).reboot(mac); - logger.debug("Freeplug {} succesfully restarted", mac); + logger.debug("Freeplug {} successfully restarted", mac); } catch (FreeboxException e) { - logger.warn("Error restarting freeplug {}: {}", mac, e.getMessage()); + logger.warn("Error restarting freeplug {}: {}", thing.getUID(), e.getMessage()); } } diff --git a/bundles/org.openhab.binding.freeboxos/src/main/resources/OH-INF/i18n/freeboxos.properties b/bundles/org.openhab.binding.freeboxos/src/main/resources/OH-INF/i18n/freeboxos.properties index 7b6fee4b5d..8d8d43d119 100644 --- a/bundles/org.openhab.binding.freeboxos/src/main/resources/OH-INF/i18n/freeboxos.properties +++ b/bundles/org.openhab.binding.freeboxos/src/main/resources/OH-INF/i18n/freeboxos.properties @@ -424,6 +424,7 @@ channel-type.freeboxos.xdsl-status.state.option.DISABLED = Disabled info-conf-pending = Please accept pairing request directly on your freebox info-host-not-reachable = Host is not reachable +info-plug-not-reachable = Freeplug is not reachable info-player-not-reachable = Player is not reachable info-vm-not-running = VM is not running