Skip to content

Commit

Permalink
Internationalization of dynamic scene/scene collection channels.
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur committed Nov 12, 2021
1 parent 1b5bea3 commit 3b27e46
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
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;
import org.openhab.core.thing.ThingTypeUID;
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;
Expand All @@ -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
Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -327,17 +334,18 @@ private void pollSceneCollections() throws JsonParseException, HubProcessingExce

Map<String, Channel> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,11 @@
<channel-type id="scene-activate">
<item-type>Switch</item-type>
<label>Activate</label>
<description>Activates the scene</description>
</channel-type>

<channel-type id="scene-collection-activate">
<item-type>Switch</item-type>
<label>Activate</label>
<description>Activates the scene collection</description>
</channel-type>

<channel-type id="battery-voltage" advanced="true">
Expand Down

0 comments on commit 3b27e46

Please sign in to comment.