From 333d9088d4785dd341a3ae43fd59f96ca190e393 Mon Sep 17 00:00:00 2001 From: lolodomo Date: Mon, 8 Nov 2021 13:53:42 +0100 Subject: [PATCH] [hue] Internationalization of discovery results (#11501) Signed-off-by: Laurent Garnier Signed-off-by: Michael Schmidt --- .../hue/internal/HueThingHandlerFactory.java | 11 +++++++++-- .../discovery/HueDeviceDiscoveryService.java | 15 ++++++++++++--- .../hue/internal/handler/HueBridgeHandler.java | 17 ++++++++++++++++- .../main/resources/OH-INF/i18n/hue.properties | 4 ++++ 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/HueThingHandlerFactory.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/HueThingHandlerFactory.java index fb13efd35140a..e82cdec945439 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/HueThingHandlerFactory.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/HueThingHandlerFactory.java @@ -33,6 +33,8 @@ import org.openhab.binding.hue.internal.handler.sensors.TapSwitchHandler; import org.openhab.binding.hue.internal.handler.sensors.TemperatureHandler; import org.openhab.core.config.core.Configuration; +import org.openhab.core.i18n.LocaleProvider; +import org.openhab.core.i18n.TranslationProvider; import org.openhab.core.thing.Bridge; import org.openhab.core.thing.Thing; import org.openhab.core.thing.ThingTypeUID; @@ -68,10 +70,15 @@ public class HueThingHandlerFactory extends BaseThingHandlerFactory { .flatMap(i -> i).collect(Collectors.toSet())); private final HueStateDescriptionProvider stateDescriptionProvider; + private final TranslationProvider i18nProvider; + private final LocaleProvider localeProvider; @Activate - public HueThingHandlerFactory(final @Reference HueStateDescriptionProvider stateDescriptionProvider) { + public HueThingHandlerFactory(final @Reference HueStateDescriptionProvider stateDescriptionProvider, + final @Reference TranslationProvider i18nProvider, final @Reference LocaleProvider localeProvider) { this.stateDescriptionProvider = stateDescriptionProvider; + this.i18nProvider = i18nProvider; + this.localeProvider = localeProvider; } @Override @@ -142,7 +149,7 @@ private ThingUID getThingUID(ThingTypeUID thingTypeUID, String id, @Nullable Thi @Override protected @Nullable ThingHandler createHandler(Thing thing) { if (HueBridgeHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) { - return new HueBridgeHandler((Bridge) thing, stateDescriptionProvider); + return new HueBridgeHandler((Bridge) thing, stateDescriptionProvider, i18nProvider, localeProvider); } else if (HueLightHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) { return new HueLightHandler(thing, stateDescriptionProvider); } else if (DimmerSwitchHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) { diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/discovery/HueDeviceDiscoveryService.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/discovery/HueDeviceDiscoveryService.java index 019eb36393f42..b86639169f34b 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/discovery/HueDeviceDiscoveryService.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/discovery/HueDeviceDiscoveryService.java @@ -111,8 +111,11 @@ public HueDeviceDiscoveryService() { @Override public void setThingHandler(@Nullable ThingHandler handler) { if (handler instanceof HueBridgeHandler) { - hueBridgeHandler = (HueBridgeHandler) handler; + HueBridgeHandler localHandler = (HueBridgeHandler) handler; + hueBridgeHandler = localHandler; bridgeUID = handler.getThing().getUID(); + i18nProvider = localHandler.getI18nProvider(); + localeProvider = localHandler.getLocaleProvider(); } } @@ -275,8 +278,14 @@ public void addGroupDiscovery(FullGroup group) { Map properties = new HashMap<>(); properties.put(GROUP_ID, group.getId()); - String name = String.format("%s (%s)", "0".equals(group.getId()) ? "All lights" : group.getName(), - group.getType()); + String name; + if ("0".equals(group.getId())) { + name = "@text/discovery.group.all_lights.label"; + } else if ("Room".equals(group.getType())) { + name = group.getName(); + } else { + name = String.format("%s (%s)", group.getName(), group.getType()); + } DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withThingType(THING_TYPE_GROUP) .withProperties(properties).withBridge(localBridgeUID).withRepresentationProperty(GROUP_ID) .withLabel(name).build(); diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueBridgeHandler.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueBridgeHandler.java index 0a9a577c9e8b1..19f377e52a3bc 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueBridgeHandler.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueBridgeHandler.java @@ -54,6 +54,8 @@ import org.openhab.binding.hue.internal.exceptions.UnauthorizedException; import org.openhab.core.config.core.Configuration; import org.openhab.core.config.core.status.ConfigStatusMessage; +import org.openhab.core.i18n.LocaleProvider; +import org.openhab.core.i18n.TranslationProvider; import org.openhab.core.library.types.HSBType; import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.StringType; @@ -98,6 +100,8 @@ public class HueBridgeHandler extends ConfigStatusBridgeHandler implements HueCl private final Logger logger = LoggerFactory.getLogger(HueBridgeHandler.class); private final HueStateDescriptionProvider stateDescriptionOptionProvider; + private final TranslationProvider i18nProvider; + private final LocaleProvider localeProvider; private final Map lastLightStates = new ConcurrentHashMap<>(); private final Map lastSensorStates = new ConcurrentHashMap<>(); @@ -403,9 +407,12 @@ private void setBridgeSceneChannelStateOptions(List scenes, Map consoleScenesList = new ArrayList<>(); - public HueBridgeHandler(Bridge bridge, HueStateDescriptionProvider stateDescriptionOptionProvider) { + public HueBridgeHandler(Bridge bridge, HueStateDescriptionProvider stateDescriptionOptionProvider, + TranslationProvider i18nProvider, LocaleProvider localeProvider) { super(bridge); this.stateDescriptionOptionProvider = stateDescriptionOptionProvider; + this.i18nProvider = i18nProvider; + this.localeProvider = localeProvider; } @Override @@ -1040,4 +1047,12 @@ public Collection getConfigStatus() { return List.of(); } } + + public TranslationProvider getI18nProvider() { + return i18nProvider; + } + + public LocaleProvider getLocaleProvider() { + return localeProvider; + } } diff --git a/bundles/org.openhab.binding.hue/src/main/resources/OH-INF/i18n/hue.properties b/bundles/org.openhab.binding.hue/src/main/resources/OH-INF/i18n/hue.properties index a7609ea22cb2f..fa842293b4317 100644 --- a/bundles/org.openhab.binding.hue/src/main/resources/OH-INF/i18n/hue.properties +++ b/bundles/org.openhab.binding.hue/src/main/resources/OH-INF/i18n/hue.properties @@ -225,3 +225,7 @@ actionInputFadeTimeLabel = FadeTime actionInputFadeTimeDesc = The fade time to use for the light command in ms. actionLabel = send a light command with a custom fade time actionDesc = Send a light command with a custom fade time. + +# discovery results + +discovery.group.all_lights.label = All lights