[Netatmo] Avoid double refresh of weather channels (#15686)

* Solves issue #15684
* Removing excessive usage of inline typecast

---------

Signed-off-by: clinique <gael@lhopital.org>
This commit is contained in:
Gaël L'hopital 2023-10-08 09:35:41 +02:00 committed by GitHub
parent f24a4305b8
commit 7e53167967
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 20 deletions

View File

@ -47,7 +47,6 @@ import org.openhab.core.types.Command;
*/
@NonNullByDefault
public class Capability {
protected final Thing thing;
protected final CommonInterface handler;
protected final ModuleType moduleType;

View File

@ -25,6 +25,7 @@ import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.netatmo.internal.api.NetatmoException;
import org.openhab.binding.netatmo.internal.api.WeatherApi;
import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.MeasureClass;
import org.openhab.binding.netatmo.internal.api.dto.Device;
import org.openhab.binding.netatmo.internal.api.dto.NAObject;
import org.openhab.binding.netatmo.internal.config.MeasureConfiguration;
import org.openhab.binding.netatmo.internal.handler.CommonInterface;
@ -56,6 +57,13 @@ public class MeasureCapability extends CacheWeatherCapability {
measureChannelHelper.setMeasures(measures);
}
@Override
protected void updateNADevice(Device newData) {
// Resolution of issue #15684 :
// Do not transfer newData to superclass - MeasureCapability pulls its own data based on measurement channels
// configuration and store them in 'measures' for the channel helper.
}
private void updateMeasures(WeatherApi api, String deviceId, @Nullable String moduleId) {
measures.clear();
handler.getActiveChannels().filter(channel -> !channel.getConfiguration().getProperties().isEmpty())

View File

@ -40,14 +40,12 @@ public class HumidityChannelHelper extends ChannelHelper {
@Override
protected @Nullable State internalGetDashboard(String channelId, Dashboard dashboard) {
switch (channelId) {
case CHANNEL_HUMIDEX:
return new DecimalType(humidex(dashboard.getTemperature(), dashboard.getHumidity()));
case CHANNEL_HUMIDEX_SCALE:
return new DecimalType(humidexScale(humidex(dashboard.getTemperature(), dashboard.getHumidity())));
case CHANNEL_VALUE:
return toQuantityType(dashboard.getHumidity(), MeasureClass.HUMIDITY);
}
return null;
return switch (channelId) {
case CHANNEL_HUMIDEX -> new DecimalType(humidex(dashboard.getTemperature(), dashboard.getHumidity()));
case CHANNEL_HUMIDEX_SCALE ->
new DecimalType(humidexScale(humidex(dashboard.getTemperature(), dashboard.getHumidity())));
case CHANNEL_VALUE -> toQuantityType(dashboard.getHumidity(), MeasureClass.HUMIDITY);
default -> null;
};
}
}

View File

@ -25,7 +25,6 @@ import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.Measure;
import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.MeasureClass;
import org.openhab.core.io.net.http.HttpUtil;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.RawType;
import org.openhab.core.library.types.StringType;
@ -75,16 +74,12 @@ public class ChannelTypeUtils {
return zonedDateTime.map(zdt -> (State) new DateTimeType(zdt)).orElse(UnDefType.NULL);
}
public static State toQuantityType(@Nullable Double value, @Nullable MeasureClass measureClass) {
public static State toQuantityType(@Nullable Double value, MeasureClass measureClass) {
if (value != null && !value.isNaN()) {
if (measureClass != null) {
Measure measureDef = measureClass.measureDefinition;
BigDecimal measure = new BigDecimal(Math.min(measureDef.maxValue, Math.max(measureDef.minValue, value)))
.setScale(measureDef.scale, RoundingMode.HALF_UP);
return new QuantityType<>(measure, measureDef.unit);
} else {
return new DecimalType(value);
}
Measure measureDef = measureClass.measureDefinition;
BigDecimal measure = new BigDecimal(Math.min(measureDef.maxValue, Math.max(measureDef.minValue, value)))
.setScale(measureDef.scale, RoundingMode.HALF_UP);
return new QuantityType<>(measure, measureDef.unit);
}
return UnDefType.NULL;
}