diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/HDPowerViewHandlerFactory.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/HDPowerViewHandlerFactory.java index 8b4dc8aeb8be1..65083143bed56 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/HDPowerViewHandlerFactory.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/HDPowerViewHandlerFactory.java @@ -21,6 +21,8 @@ import org.openhab.binding.hdpowerview.internal.handler.HDPowerViewHubHandler; import org.openhab.binding.hdpowerview.internal.handler.HDPowerViewShadeHandler; import org.openhab.core.config.discovery.DiscoveryService; +import org.openhab.core.i18n.LocaleProvider; +import org.openhab.core.i18n.TranslationProvider; import org.openhab.core.io.net.http.HttpClientFactory; import org.openhab.core.thing.Bridge; import org.openhab.core.thing.Thing; @@ -28,6 +30,7 @@ import org.openhab.core.thing.binding.BaseThingHandlerFactory; import org.openhab.core.thing.binding.ThingHandler; import org.openhab.core.thing.binding.ThingHandlerFactory; +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; @@ -43,10 +46,16 @@ public class HDPowerViewHandlerFactory extends BaseThingHandlerFactory { private final HttpClient httpClient; + private final HDPowerViewTranslationProvider translationProvider; @Activate - public HDPowerViewHandlerFactory(@Reference HttpClientFactory httpClientFactory) { + public HDPowerViewHandlerFactory(@Reference HttpClientFactory httpClientFactory, + final @Reference TranslationProvider i18nProvider, final @Reference LocaleProvider localeProvider, + ComponentContext componentContext) { + super.activate(componentContext); this.httpClient = httpClientFactory.getCommonHttpClient(); + this.translationProvider = new HDPowerViewTranslationProvider(getBundleContext().getBundle(), i18nProvider, + localeProvider); } @Override @@ -59,7 +68,7 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) { ThingTypeUID thingTypeUID = thing.getThingTypeUID(); if (thingTypeUID.equals(HDPowerViewBindingConstants.THING_TYPE_HUB)) { - HDPowerViewHubHandler handler = new HDPowerViewHubHandler((Bridge) thing, httpClient); + HDPowerViewHubHandler handler = new HDPowerViewHubHandler((Bridge) thing, httpClient, translationProvider); registerService(new HDPowerViewShadeDiscoveryService(handler)); return handler; } else if (thingTypeUID.equals(HDPowerViewBindingConstants.THING_TYPE_SHADE)) { diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/HDPowerViewTranslationProvider.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/HDPowerViewTranslationProvider.java new file mode 100644 index 0000000000000..adf16057a153a --- /dev/null +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/HDPowerViewTranslationProvider.java @@ -0,0 +1,47 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.hdpowerview.internal; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.i18n.LocaleProvider; +import org.openhab.core.i18n.TranslationProvider; +import org.osgi.framework.Bundle; + +/** + * {@link HDPowerViewTranslationProvider} provides i18n message lookup + * + * @author Jacob Laursen - Initial contribution + */ +@NonNullByDefault +public class HDPowerViewTranslationProvider { + + private final Bundle bundle; + private final TranslationProvider i18nProvider; + private final LocaleProvider localeProvider; + + public HDPowerViewTranslationProvider(Bundle bundle, TranslationProvider i18nProvider, + LocaleProvider localeProvider) { + this.bundle = bundle; + this.i18nProvider = i18nProvider; + this.localeProvider = localeProvider; + } + + public String getText(String key, @Nullable Object... arguments) { + String text = i18nProvider.getText(bundle, key, key, localeProvider.getLocale(), arguments); + if (text != null) { + return text; + } + return key; + } +} diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/handler/HDPowerViewHubHandler.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/handler/HDPowerViewHubHandler.java index 1c9d757111e8a..3240646f0f825 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/handler/HDPowerViewHubHandler.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/handler/HDPowerViewHubHandler.java @@ -26,6 +26,7 @@ import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jetty.client.HttpClient; import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants; +import org.openhab.binding.hdpowerview.internal.HDPowerViewTranslationProvider; import org.openhab.binding.hdpowerview.internal.HDPowerViewWebTargets; import org.openhab.binding.hdpowerview.internal.HubMaintenanceException; import org.openhab.binding.hdpowerview.internal.HubProcessingException; @@ -68,6 +69,7 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler { private final Logger logger = LoggerFactory.getLogger(HDPowerViewHubHandler.class); private final HttpClient httpClient; + private final HDPowerViewTranslationProvider translationProvider; private long refreshInterval; private long hardRefreshPositionInterval; @@ -84,9 +86,11 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler { private final ChannelTypeUID sceneCollectionChannelTypeUID = new ChannelTypeUID( HDPowerViewBindingConstants.BINDING_ID, HDPowerViewBindingConstants.CHANNELTYPE_SCENE_COLLECTION_ACTIVATE); - public HDPowerViewHubHandler(Bridge bridge, HttpClient httpClient) { + public HDPowerViewHubHandler(Bridge bridge, HttpClient httpClient, + HDPowerViewTranslationProvider translationProvider) { super(bridge); this.httpClient = httpClient; + this.translationProvider = translationProvider; } @Override @@ -130,7 +134,8 @@ public void initialize() { String host = config.host; if (host == null || host.isEmpty()) { - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Host address must be set"); + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, + "@text/offline.conf-error-no-host-address"); return; } @@ -292,8 +297,10 @@ private void pollScenes() throws JsonParseException, HubProcessingException, Hub } else { // create a new scene channel ChannelUID channelUID = new ChannelUID(getThing().getUID(), sceneId); + String description = translationProvider.getText("channel-type.hdpowerview.scene-activate.description", + scene.getName()); Channel channel = ChannelBuilder.create(channelUID, "Switch").withType(sceneChannelTypeUID) - .withLabel(scene.getName()).withDescription("Activates the scene " + scene.getName()).build(); + .withLabel(scene.getName()).withDescription(description).build(); updateThing(editThing().withChannel(channel).build()); logger.debug("Creating new channel for scene '{}'", sceneId); } @@ -327,17 +334,18 @@ private void pollSceneCollections() throws JsonParseException, HubProcessingExce Map idChannelMap = getIdSceneCollectionChannelMap(); for (SceneCollection sceneCollection : sceneCollectionData) { - // remove existing scene channel from the map + // remove existing scene collection channel from the map String sceneCollectionId = Integer.toString(sceneCollection.id); if (idChannelMap.containsKey(sceneCollectionId)) { idChannelMap.remove(sceneCollectionId); logger.debug("Keeping channel for existing scene collection '{}'", sceneCollectionId); } else { - // create a new scene channel + // create a new scene collection channel ChannelUID channelUID = new ChannelUID(getThing().getUID(), sceneCollectionId); + String description = translationProvider.getText( + "channel-type.hdpowerview.scene-collection-activate.description", sceneCollection.getName()); Channel channel = ChannelBuilder.create(channelUID, "Switch").withType(sceneCollectionChannelTypeUID) - .withLabel(sceneCollection.getName()) - .withDescription("Activates the scene collection " + sceneCollection.getName()).build(); + .withLabel(sceneCollection.getName()).withDescription(description).build(); updateThing(editThing().withChannel(channel).build()); logger.debug("Creating new channel for scene collection '{}'", sceneCollectionId); } diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/i18n/hdpowerview.properties b/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/i18n/hdpowerview.properties index 58f282b2effe9..8f2571c2de825 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/i18n/hdpowerview.properties +++ b/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/i18n/hdpowerview.properties @@ -27,11 +27,13 @@ thing-type.config.hdpowerview.shade.id.description = The numeric ID of the Power channel-type.hdpowerview.battery-voltage.label = Battery Voltage channel-type.hdpowerview.battery-voltage.description = Battery voltage reported by the shade -channel-type.hdpowerview.scene-activate.label = Activate -channel-type.hdpowerview.scene-activate.description = Activates the scene -channel-type.hdpowerview.scene-collection-activate.label = Activate -channel-type.hdpowerview.scene-collection-activate.description = Activates the scene collection +channel-type.hdpowerview.scene-activate.description = Activates the scene ''{0}'' +channel-type.hdpowerview.scene-collection-activate.description = Activates the scene collection ''{0}'' channel-type.hdpowerview.shade-position.label = Position channel-type.hdpowerview.shade-position.description = The vertical position of the shade channel-type.hdpowerview.shade-vane.label = Vane channel-type.hdpowerview.shade-vane.description = The opening of the slats in the shade + +# thing status descriptions + +offline.conf-error-no-host-address = Host address must be set diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/thing/thing-types.xml b/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/thing/thing-types.xml index c0ac6313d0571..ebca3d9dad628 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/thing/thing-types.xml +++ b/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/thing/thing-types.xml @@ -89,13 +89,11 @@ Switch - Activates the scene Switch - Activates the scene collection