[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
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();
}
}

View 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() {

View 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) {