From 1d766bb824bb467e8c31c4b9c85d227bd144998d Mon Sep 17 00:00:00 2001 From: lolodomo Date: Mon, 27 Jun 2022 18:09:37 +0200 Subject: [PATCH] [netatmo] Localized label/pattern for dynamic channel types (#13012) * [netatmo] Localized label/pattern for dynamic channel types * Review comment about key naming * Fix label for timestamp extensible channel types Signed-off-by: Laurent Garnier --- .../internal/api/data/NetatmoConstants.java | 4 ++-- .../providers/NetatmoChannelTypeProvider.java | 20 +++++++++++++++---- .../resources/OH-INF/i18n/netatmo.properties | 15 ++++++++++++++ 3 files changed, 33 insertions(+), 6 deletions(-) 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 fdd7d2340..737e6b842 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 @@ -107,8 +107,8 @@ public class NetatmoConstants { new MeasureChannelDetails(confFragment, String.join(":", NUMBER, dimension), String.format("%%.%df %s", measureDefinition.scale, UnitUtils.UNIT_PLACEHOLDER))); if (canScale) { - channels.put(String.join("-", apiDescriptor, GROUP_TIMESTAMP), - new MeasureChannelDetails(GROUP_TIMESTAMP, DATETIME, "%1$tA, %1$td.%1$tm. %1$tH:%1$tM")); + 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/providers/NetatmoChannelTypeProvider.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/providers/NetatmoChannelTypeProvider.java index 42fe411ce..e8972cf3c 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/providers/NetatmoChannelTypeProvider.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/providers/NetatmoChannelTypeProvider.java @@ -22,17 +22,24 @@ import java.util.Locale; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.MeasureClass; +import org.openhab.core.i18n.LocaleProvider; +import org.openhab.core.thing.i18n.ChannelTypeI18nLocalizationService; import org.openhab.core.thing.type.ChannelType; import org.openhab.core.thing.type.ChannelTypeBuilder; import org.openhab.core.thing.type.ChannelTypeProvider; import org.openhab.core.thing.type.ChannelTypeUID; import org.openhab.core.thing.type.StateChannelTypeBuilder; +import org.osgi.framework.Bundle; +import org.osgi.service.component.ComponentContext; +import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; /** * Extends the ChannelTypeProvider generating Channel Types based on {@link MeasureClass} enum. * * @author Gaƫl L'hopital - Initial contribution + * @author Laurent Garnier - Localizing the extensible channel types * */ @NonNullByDefault @@ -40,15 +47,20 @@ import org.osgi.service.component.annotations.Component; public class NetatmoChannelTypeProvider implements ChannelTypeProvider { private final Collection channelTypes = new HashSet<>(); - public NetatmoChannelTypeProvider() { + @Activate + public NetatmoChannelTypeProvider(final @Reference ChannelTypeI18nLocalizationService localizationService, + final @Reference LocaleProvider localeProvider, ComponentContext componentContext) { + final Bundle bundle = componentContext.getBundleContext().getBundle(); MeasureClass.AS_SET.forEach(mc -> mc.channels.forEach((measureChannel, channelDetails) -> { StateChannelTypeBuilder channelTypeBuilder = ChannelTypeBuilder - .state(new ChannelTypeUID(BINDING_ID, measureChannel), measureChannel.replace("-", " "), + .state(new ChannelTypeUID(BINDING_ID, measureChannel), + String.format("@text/extensible-channel-type.%s.label", measureChannel), channelDetails.itemType) .withStateDescriptionFragment(channelDetails.stateDescriptionFragment) .withConfigDescriptionURI(channelDetails.configURI); - - channelTypes.add(channelTypeBuilder.build()); + ChannelType channelType = localizationService.createLocalizedChannelType(bundle, channelTypeBuilder.build(), + localeProvider.getLocale()); + channelTypes.add(channelType); })); } diff --git a/bundles/org.openhab.binding.netatmo/src/main/resources/OH-INF/i18n/netatmo.properties b/bundles/org.openhab.binding.netatmo/src/main/resources/OH-INF/i18n/netatmo.properties index c0f5a6877..51f17f9b9 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/resources/OH-INF/i18n/netatmo.properties +++ b/bundles/org.openhab.binding.netatmo/src/main/resources/OH-INF/i18n/netatmo.properties @@ -354,6 +354,21 @@ config.period.option.1day = This day config.period.option.1week = This week config.period.option.1month = This month +# extensible channel types + +extensible-channel-type.co2-measurement.label = CO2 Measurement +extensible-channel-type.co2-timestamp.label = CO2 Measurement Timestamp +extensible-channel-type.hum-measurement.label = Humidity Measurement +extensible-channel-type.hum-timestamp.label = Humidity Measurement Timestamp +extensible-channel-type.noise-measurement.label = Noise Measurement +extensible-channel-type.noise-timestamp.label = Noise Measurement Timestamp +extensible-channel-type.pressure-measurement.label = Pressure Measurement +extensible-channel-type.pressure-timestamp.label = Pressure Measurement Timestamp +extensible-channel-type.sum_rain-measurement.label = Summing Rain Measurement +extensible-channel-type.temp-measurement.label = Temperature Measurement +extensible-channel-type.temp-timestamp.label = Temperature Measurement Timestamp +extensible-channel-type.timestamp.pattern = %1$tA, %1$td.%1$tm. %1$tH:%1$tM + # thing types thing-type.netatmo.account.label = Netatmo Account