[openuv] Fix time channels not being updated (#12558)

* [openuv] time channels not filled

Signed-off-by: clinique <gael@lhopital.org>
This commit is contained in:
Gaël L'hopital 2022-04-01 23:23:54 +02:00 committed by GitHub
parent 77c765085e
commit 2652ab647b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 59 deletions

View File

@ -34,15 +34,12 @@ import org.slf4j.LoggerFactory;
*/ */
@NonNullByDefault @NonNullByDefault
public class OpenUVDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService { public class OpenUVDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
private final Logger logger = LoggerFactory.getLogger(OpenUVDiscoveryService.class);
private static final int DISCOVER_TIMEOUT_SECONDS = 2; private static final int DISCOVER_TIMEOUT_SECONDS = 2;
private final Logger logger = LoggerFactory.getLogger(OpenUVDiscoveryService.class);
private @Nullable OpenUVBridgeHandler bridgeHandler; private @Nullable OpenUVBridgeHandler bridgeHandler;
/**
* Creates a OpenUVDiscoveryService with enabled autostart.
*/
public OpenUVDiscoveryService() { public OpenUVDiscoveryService() {
super(SUPPORTED_THING_TYPES_UIDS, DISCOVER_TIMEOUT_SECONDS); super(SUPPORTED_THING_TYPES_UIDS, DISCOVER_TIMEOUT_SECONDS);
} }
@ -51,9 +48,9 @@ public class OpenUVDiscoveryService extends AbstractDiscoveryService implements
public void setThingHandler(ThingHandler handler) { public void setThingHandler(ThingHandler handler) {
if (handler instanceof OpenUVBridgeHandler) { if (handler instanceof OpenUVBridgeHandler) {
OpenUVBridgeHandler localHandler = (OpenUVBridgeHandler) handler; OpenUVBridgeHandler localHandler = (OpenUVBridgeHandler) handler;
this.bridgeHandler = localHandler; bridgeHandler = localHandler;
this.i18nProvider = localHandler.getI18nProvider(); i18nProvider = localHandler.getI18nProvider();
this.localeProvider = localHandler.getLocaleProvider(); localeProvider = localHandler.getLocaleProvider();
} }
} }

View File

@ -17,8 +17,8 @@ import java.time.Duration;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.Properties; import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -152,7 +152,7 @@ public class OpenUVBridgeHandler extends BaseBridgeHandler {
@Override @Override
public Collection<Class<? extends ThingHandlerService>> getServices() { public Collection<Class<? extends ThingHandlerService>> getServices() {
return Collections.singleton(OpenUVDiscoveryService.class); return Set.of(OpenUVDiscoveryService.class);
} }
public @Nullable PointType getLocation() { public @Nullable PointType getLocation() {

View File

@ -40,7 +40,6 @@ import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail; import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.ThingStatusInfo; import org.openhab.core.thing.ThingStatusInfo;
import org.openhab.core.thing.binding.BaseThingHandler; 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.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;
@ -145,9 +144,8 @@ public class OpenUVReportHandler extends BaseThingHandler {
location.getLongitude().toString(), location.getAltitude().toString()); location.getLongitude().toString(), location.getAltitude().toString());
if (openUVData != null) { if (openUVData != null) {
scheduleUVMaxEvent(openUVData); scheduleUVMaxEvent(openUVData);
getThing().getChannels().forEach(channel -> { getThing().getChannels().stream().filter(channel -> isLinked(channel.getUID().getId()))
updateChannel(channel.getUID(), openUVData); .forEach(channel -> updateState(channel.getUID(), getState(channel, openUVData)));
});
updateStatus(ThingStatus.ONLINE); updateStatus(ThingStatus.ONLINE);
} else { } else {
updateStatus(ThingStatus.OFFLINE, bridgeStatusInfo.getStatusDetail(), updateStatus(ThingStatus.OFFLINE, bridgeStatusInfo.getStatusDetail(),
@ -191,52 +189,31 @@ public class OpenUVReportHandler extends BaseThingHandler {
} }
} }
/** private State getState(Channel channel, OpenUVResult openUVData) {
* Update the channel from the last OpenUV data retrieved ChannelUID uid = channel.getUID();
* switch (uid.getId()) {
* @param channelUID the id identifying the channel to be updated case UV_INDEX:
* @param openUVData return new DecimalType(openUVData.getUv());
* case ALERT_LEVEL:
*/ return asAlertLevel(openUVData.getUv());
private void updateChannel(ChannelUID channelUID, OpenUVResult openUVData) { case UV_COLOR:
Channel channel = getThing().getChannel(channelUID.getId()); return ALERT_COLORS.getOrDefault(asAlertLevel(openUVData.getUv()), ALERT_UNDEF);
if (channel != null && isLinked(channelUID)) { case UV_MAX:
ChannelTypeUID channelTypeUID = channel.getChannelTypeUID(); return new DecimalType(openUVData.getUvMax());
if (channelTypeUID != null) { case OZONE:
switch (channelTypeUID.getId()) { return new QuantityType<>(openUVData.getOzone(), Units.DOBSON_UNIT);
case UV_INDEX: case OZONE_TIME:
updateState(channelUID, new DecimalType(openUVData.getUv())); return openUVData.getOzoneTime();
break; case UV_MAX_TIME:
case ALERT_LEVEL: return openUVData.getUVMaxTime();
updateState(channelUID, asAlertLevel(openUVData.getUv())); case UV_TIME:
break; return openUVData.getUVTime();
case UV_COLOR: case SAFE_EXPOSURE:
updateState(channelUID, SafeExposureConfiguration configuration = channel.getConfiguration()
ALERT_COLORS.getOrDefault(asAlertLevel(openUVData.getUv()), ALERT_UNDEF)); .as(SafeExposureConfiguration.class);
break; return openUVData.getSafeExposureTime(configuration.index);
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;
}
}
} }
return UnDefType.NULL;
} }
private State asAlertLevel(double uv) { private State asAlertLevel(double uv) {