]> git.basschouten.com Git - openhab-addons.git/commitdiff
[openuv] Fix time channels not being updated (#12558)
authorGaël L'hopital <gael@lhopital.org>
Fri, 1 Apr 2022 21:23:54 +0000 (23:23 +0200)
committerGitHub <noreply@github.com>
Fri, 1 Apr 2022 21:23:54 +0000 (23:23 +0200)
* [openuv] time channels not filled

Signed-off-by: clinique <gael@lhopital.org>
bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/discovery/OpenUVDiscoveryService.java
bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/handler/OpenUVBridgeHandler.java
bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/handler/OpenUVReportHandler.java

index db825f6c0337c46b487fe34ba222e03253b3a682..868ae3f3c39852c37eba11690e88d9b0dfebbc3f 100644 (file)
@@ -34,15 +34,12 @@ import org.slf4j.LoggerFactory;
  */
 @NonNullByDefault
 public class OpenUVDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
-    private final Logger logger = LoggerFactory.getLogger(OpenUVDiscoveryService.class);
-
     private static final int DISCOVER_TIMEOUT_SECONDS = 2;
 
+    private final Logger logger = LoggerFactory.getLogger(OpenUVDiscoveryService.class);
+
     private @Nullable OpenUVBridgeHandler bridgeHandler;
 
-    /**
-     * Creates a OpenUVDiscoveryService with enabled autostart.
-     */
     public OpenUVDiscoveryService() {
         super(SUPPORTED_THING_TYPES_UIDS, DISCOVER_TIMEOUT_SECONDS);
     }
@@ -51,9 +48,9 @@ public class OpenUVDiscoveryService extends AbstractDiscoveryService implements
     public void setThingHandler(ThingHandler handler) {
         if (handler instanceof OpenUVBridgeHandler) {
             OpenUVBridgeHandler localHandler = (OpenUVBridgeHandler) handler;
-            this.bridgeHandler = localHandler;
-            this.i18nProvider = localHandler.getI18nProvider();
-            this.localeProvider = localHandler.getLocaleProvider();
+            bridgeHandler = localHandler;
+            i18nProvider = localHandler.getI18nProvider();
+            localeProvider = localHandler.getLocaleProvider();
         }
     }
 
index 541b53409b599da6bc145b8386d848eec00a83d0..ceb8954857e2cb8dbed0077816c42497956253ce 100644 (file)
@@ -17,8 +17,8 @@ import java.time.Duration;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.Properties;
+import java.util.Set;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
 
@@ -152,7 +152,7 @@ public class OpenUVBridgeHandler extends BaseBridgeHandler {
 
     @Override
     public Collection<Class<? extends ThingHandlerService>> getServices() {
-        return Collections.singleton(OpenUVDiscoveryService.class);
+        return Set.of(OpenUVDiscoveryService.class);
     }
 
     public @Nullable PointType getLocation() {
index 30940a74d30ee48a5e8103a34e5e3da711439fbe..7749b50a779dad940b441eb7b0d5eb2331a98fbb 100644 (file)
@@ -40,7 +40,6 @@ import org.openhab.core.thing.ThingStatus;
 import org.openhab.core.thing.ThingStatusDetail;
 import org.openhab.core.thing.ThingStatusInfo;
 import org.openhab.core.thing.binding.BaseThingHandler;
-import org.openhab.core.thing.type.ChannelTypeUID;
 import org.openhab.core.types.Command;
 import org.openhab.core.types.RefreshType;
 import org.openhab.core.types.State;
@@ -145,9 +144,8 @@ public class OpenUVReportHandler extends BaseThingHandler {
                     location.getLongitude().toString(), location.getAltitude().toString());
             if (openUVData != null) {
                 scheduleUVMaxEvent(openUVData);
-                getThing().getChannels().forEach(channel -> {
-                    updateChannel(channel.getUID(), openUVData);
-                });
+                getThing().getChannels().stream().filter(channel -> isLinked(channel.getUID().getId()))
+                        .forEach(channel -> updateState(channel.getUID(), getState(channel, openUVData)));
                 updateStatus(ThingStatus.ONLINE);
             } else {
                 updateStatus(ThingStatus.OFFLINE, bridgeStatusInfo.getStatusDetail(),
@@ -191,52 +189,31 @@ public class OpenUVReportHandler extends BaseThingHandler {
         }
     }
 
-    /**
-     * Update the channel from the last OpenUV data retrieved
-     *
-     * @param channelUID the id identifying the channel to be updated
-     * @param openUVData
-     *
-     */
-    private void updateChannel(ChannelUID channelUID, OpenUVResult openUVData) {
-        Channel channel = getThing().getChannel(channelUID.getId());
-        if (channel != null && isLinked(channelUID)) {
-            ChannelTypeUID channelTypeUID = channel.getChannelTypeUID();
-            if (channelTypeUID != null) {
-                switch (channelTypeUID.getId()) {
-                    case UV_INDEX:
-                        updateState(channelUID, new DecimalType(openUVData.getUv()));
-                        break;
-                    case ALERT_LEVEL:
-                        updateState(channelUID, asAlertLevel(openUVData.getUv()));
-                        break;
-                    case UV_COLOR:
-                        updateState(channelUID,
-                                ALERT_COLORS.getOrDefault(asAlertLevel(openUVData.getUv()), ALERT_UNDEF));
-                        break;
-                    case UV_MAX:
-                        updateState(channelUID, new DecimalType(openUVData.getUvMax()));
-                        break;
-                    case OZONE:
-                        updateState(channelUID, new QuantityType<>(openUVData.getOzone(), Units.DOBSON_UNIT));
-                        break;
-                    case OZONE_TIME:
-                        updateState(channelUID, openUVData.getOzoneTime());
-                        break;
-                    case UV_MAX_TIME:
-                        updateState(channelUID, openUVData.getUVMaxTime());
-                        break;
-                    case UV_TIME:
-                        updateState(channelUID, openUVData.getUVTime());
-                        break;
-                    case SAFE_EXPOSURE:
-                        SafeExposureConfiguration configuration = channel.getConfiguration()
-                                .as(SafeExposureConfiguration.class);
-                        updateState(channelUID, openUVData.getSafeExposureTime(configuration.index));
-                        break;
-                }
-            }
+    private State getState(Channel channel, OpenUVResult openUVData) {
+        ChannelUID uid = channel.getUID();
+        switch (uid.getId()) {
+            case UV_INDEX:
+                return new DecimalType(openUVData.getUv());
+            case ALERT_LEVEL:
+                return asAlertLevel(openUVData.getUv());
+            case UV_COLOR:
+                return ALERT_COLORS.getOrDefault(asAlertLevel(openUVData.getUv()), ALERT_UNDEF);
+            case UV_MAX:
+                return new DecimalType(openUVData.getUvMax());
+            case OZONE:
+                return new QuantityType<>(openUVData.getOzone(), Units.DOBSON_UNIT);
+            case OZONE_TIME:
+                return openUVData.getOzoneTime();
+            case UV_MAX_TIME:
+                return openUVData.getUVMaxTime();
+            case UV_TIME:
+                return openUVData.getUVTime();
+            case SAFE_EXPOSURE:
+                SafeExposureConfiguration configuration = channel.getConfiguration()
+                        .as(SafeExposureConfiguration.class);
+                return openUVData.getSafeExposureTime(configuration.index);
         }
+        return UnDefType.NULL;
     }
 
     private State asAlertLevel(double uv) {