diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/data/NetatmoConstants.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/data/NetatmoConstants.java index 1b01be80d02..c2e325361dc 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/data/NetatmoConstants.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/data/NetatmoConstants.java @@ -105,7 +105,7 @@ public class NetatmoConstants { channels.put(String.join("-", apiDescriptor, "measurement"), new MeasureChannelDetails(confFragment, String.join(":", NUMBER, dimension), - String.format("%%.%df %s", measureDefinition.scale, UnitUtils.UNIT_PLACEHOLDER))); + "%%.%df %s".formatted(measureDefinition.scale, UnitUtils.UNIT_PLACEHOLDER))); if (canScale) { channels.put(String.join("-", apiDescriptor, GROUP_TIMESTAMP), new MeasureChannelDetails( GROUP_TIMESTAMP, DATETIME, "@text/extensible-channel-type.timestamp.pattern")); diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/dto/NAThing.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/dto/NAThing.java index dfa0e23f914..c327e759c56 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/dto/NAThing.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/dto/NAThing.java @@ -59,8 +59,8 @@ public class NAThing extends NAObject implements NAModule { return dashboardData; } - public @Nullable String getFirmware() { - return firmware; + public Optional getFirmware() { + return Optional.ofNullable(firmware); } public int getRadioStatus() { diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/Capability.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/Capability.java index ec7eb2ea105..3ca3a89b0fa 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/Capability.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/Capability.java @@ -12,9 +12,6 @@ */ package org.openhab.binding.netatmo.internal.handler.capability; -import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*; -import static org.openhab.core.thing.Thing.*; - import java.util.Collection; import java.util.HashMap; import java.util.List; @@ -53,7 +50,7 @@ public class Capability { protected final ModuleType moduleType; protected final ThingUID thingUID; - protected boolean firstLaunch; + protected boolean firstLaunch = true; protected Map properties = Map.of(); protected @Nullable String statusReason; @@ -104,15 +101,6 @@ public class Capability { protected void beforeNewData() { properties = new HashMap<>(thing.getProperties()); - firstLaunch = properties.isEmpty(); - if (firstLaunch) { - properties.put(PROPERTY_THING_TYPE_VERSION, moduleType.thingTypeVersion); - if (!moduleType.isLogical()) { - String name = moduleType.apiName.isBlank() ? moduleType.name() : moduleType.apiName; - properties.put(PROPERTY_MODEL_ID, name); - properties.put(PROPERTY_VENDOR, VENDOR); - } - } statusReason = null; } @@ -120,16 +108,11 @@ public class Capability { if (!properties.equals(thing.getProperties())) { thing.setProperties(properties); } + firstLaunch = false; } protected void updateNAThing(NAThing newData) { - String firmware = newData.getFirmware(); - if (firmware != null && !firmware.isBlank()) { - properties.put(PROPERTY_FIRMWARE_VERSION, firmware); - } - if (!newData.isReachable()) { - statusReason = "@text/device-not-connected"; - } + // do nothing by default, can be overridden by subclasses } protected void updateNAMain(NAMain newData) { @@ -169,11 +152,9 @@ public class Capability { } public void expireData() { - if (!handler.getCapabilities().containsKey(RefreshCapability.class)) { - CommonInterface bridgeHandler = handler.getBridgeHandler(); - if (bridgeHandler != null) { - bridgeHandler.expireData(); - } + CommonInterface bridgeHandler = handler.getBridgeHandler(); + if (bridgeHandler != null && !handler.getCapabilities().containsKey(RefreshCapability.class)) { + bridgeHandler.expireData(); } } diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/ChannelHelperCapability.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/ChannelHelperCapability.java index f192f6044c6..16f9e44dd21 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/ChannelHelperCapability.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/ChannelHelperCapability.java @@ -12,12 +12,16 @@ */ package org.openhab.binding.netatmo.internal.handler.capability; +import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.VENDOR; +import static org.openhab.core.thing.Thing.*; + import java.util.List; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.netatmo.internal.api.dto.NAError; import org.openhab.binding.netatmo.internal.api.dto.NAObject; +import org.openhab.binding.netatmo.internal.api.dto.NAThing; import org.openhab.binding.netatmo.internal.handler.CommonInterface; import org.openhab.binding.netatmo.internal.handler.channelhelper.ChannelHelper; import org.openhab.core.config.core.Configuration; @@ -40,9 +44,17 @@ public class ChannelHelperCapability extends Capability { this.channelHelpers = channelHelpers; } + @Override + protected void beforeNewData() { + super.beforeNewData(); + if (firstLaunch && !moduleType.isLogical()) { + properties.put(PROPERTY_MODEL_ID, moduleType.apiName.isBlank() ? moduleType.name() : moduleType.apiName); + properties.put(PROPERTY_VENDOR, VENDOR); + } + } + @Override public void afterNewData(@Nullable NAObject newData) { - super.afterNewData(newData); channelHelpers.forEach(helper -> helper.setNewData(newData)); handler.getActiveChannels().forEach(channel -> { ChannelUID channelUID = channel.getUID(); @@ -57,6 +69,15 @@ public class ChannelHelperCapability extends Capability { } } }); + super.afterNewData(newData); + } + + @Override + protected void updateNAThing(NAThing newData) { + newData.getFirmware().map(fw -> properties.put(PROPERTY_FIRMWARE_VERSION, fw)); + if (!newData.isReachable()) { + statusReason = "@text/device-not-connected"; + } } @Override diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/EnergyCapability.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/EnergyCapability.java index dad6236270a..50299eff8bb 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/EnergyCapability.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/EnergyCapability.java @@ -46,9 +46,9 @@ import org.slf4j.LoggerFactory; @NonNullByDefault public class EnergyCapability extends RestCapability { private final Logger logger = LoggerFactory.getLogger(EnergyCapability.class); + private final NetatmoDescriptionProvider descriptionProvider; private int setPointDefaultDuration = -1; - private final NetatmoDescriptionProvider descriptionProvider; private String energyId = ""; EnergyCapability(CommonInterface handler, NetatmoDescriptionProvider descriptionProvider) { diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/HomeCapability.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/HomeCapability.java index a72f6aaf68d..cb4937440a2 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/HomeCapability.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/HomeCapability.java @@ -48,7 +48,7 @@ public class HomeCapability extends RestCapability { private final Logger logger = LoggerFactory.getLogger(HomeCapability.class); private final Set featureAreas = new HashSet<>(); private final NetatmoDescriptionProvider descriptionProvider; - private final Set homeIds = new HashSet<>(); + private final Set homeIds = new HashSet<>(3); public HomeCapability(CommonInterface handler, NetatmoDescriptionProvider descriptionProvider) { super(handler, HomeApi.class); @@ -66,6 +66,12 @@ public class HomeCapability extends RestCapability { if (!config.securityId.isBlank()) { homeIds.add(config.securityId); } + if (hasArea(FeatureArea.SECURITY) && !handler.getCapabilities().containsKey(SecurityCapability.class)) { + handler.getCapabilities().put(new SecurityCapability(handler)); + } + if (hasArea(FeatureArea.ENERGY) && !handler.getCapabilities().containsKey(EnergyCapability.class)) { + handler.getCapabilities().put(new EnergyCapability(handler, descriptionProvider)); + } } @Override @@ -76,12 +82,6 @@ public class HomeCapability extends RestCapability { @Override protected void updateHomeData(HomeData home) { - if (hasArea(FeatureArea.SECURITY) && !handler.getCapabilities().containsKey(SecurityCapability.class)) { - handler.getCapabilities().put(new SecurityCapability(handler)); - } - if (hasArea(FeatureArea.ENERGY) && !handler.getCapabilities().containsKey(EnergyCapability.class)) { - handler.getCapabilities().put(new EnergyCapability(handler, descriptionProvider)); - } if (firstLaunch) { home.getCountry().map(country -> properties.put(PROPERTY_COUNTRY, country)); home.getTimezone().map(tz -> properties.put(PROPERTY_TIMEZONE, tz)); @@ -93,13 +93,13 @@ public class HomeCapability extends RestCapability { @Override protected void afterNewData(@Nullable NAObject newData) { - super.afterNewData(newData); if (firstLaunch && !hasArea(FeatureArea.SECURITY)) { handler.removeChannels(thing.getChannelsOfGroup(GROUP_SECURITY)); } if (firstLaunch && !hasArea(FeatureArea.ENERGY)) { handler.removeChannels(thing.getChannelsOfGroup(GROUP_ENERGY)); } + super.afterNewData(newData); } private boolean hasArea(FeatureArea searched) { diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/channelhelper/AirQualityChannelHelper.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/channelhelper/AirQualityChannelHelper.java index 09b7a825694..a2a42b808fb 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/channelhelper/AirQualityChannelHelper.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/channelhelper/AirQualityChannelHelper.java @@ -39,12 +39,10 @@ public class AirQualityChannelHelper extends ChannelHelper { @Override protected @Nullable State internalGetDashboard(String channelId, Dashboard dashboard) { - switch (channelId) { - case CHANNEL_CO2: - return toQuantityType(dashboard.getCo2(), MeasureClass.CO2); - case CHANNEL_HEALTH_INDEX: - return new DecimalType(dashboard.getHealthIdx()); - } - return null; + return switch (channelId) { + case CHANNEL_CO2 -> toQuantityType(dashboard.getCo2(), MeasureClass.CO2); + case CHANNEL_HEALTH_INDEX -> new DecimalType(dashboard.getHealthIdx()); + default -> null; + }; } } diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/channelhelper/SecurityChannelHelper.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/channelhelper/SecurityChannelHelper.java index 79e217ddca9..8b85b161349 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/channelhelper/SecurityChannelHelper.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/channelhelper/SecurityChannelHelper.java @@ -62,14 +62,12 @@ public class SecurityChannelHelper extends ChannelHelper { @Override protected @Nullable State internalGetOther(String channelId) { - switch (channelId) { - case CHANNEL_PERSON_COUNT: - return persons != -1 ? new DecimalType(persons) : UnDefType.NULL; - case CHANNEL_UNKNOWN_PERSON_COUNT: - return unknowns != -1 ? new DecimalType(unknowns) : UnDefType.NULL; - case CHANNEL_UNKNOWN_PERSON_PICTURE: - return unknownSnapshot != null ? toRawType(unknownSnapshot) : UnDefType.NULL; - } - return null; + return switch (channelId) { + case CHANNEL_PERSON_COUNT -> persons != -1 ? new DecimalType(persons) : UnDefType.NULL; + case CHANNEL_UNKNOWN_PERSON_COUNT -> unknowns != -1 ? new DecimalType(unknowns) : UnDefType.NULL; + case CHANNEL_UNKNOWN_PERSON_PICTURE -> + unknownSnapshot != null ? toRawType(unknownSnapshot) : UnDefType.NULL; + default -> null; + }; } }