Skip to content

Commit

Permalink
[miio] i18n translation handling for basic channels (openhab#11576)
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Verpaalen <marcel@verpaalen.com>
Signed-off-by: Nick Waterton <n.waterton@outlook.com>
  • Loading branch information
marcelrv authored and NickWaterton committed Dec 30, 2021
1 parent 71e5c85 commit 1f5ac60
Show file tree
Hide file tree
Showing 9 changed files with 2,640 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,8 @@ public final class MiIoBindingConstants {
+ File.separator + BINDING_ID;
public static final String BINDING_USERDATA_PATH = OpenHAB.getUserDataFolder() + File.separator
+ MiIoBindingConstants.BINDING_ID;

public static final String I18N_THING_PREFIX = "thing.";
public static final String I18N_CHANNEL_PREFIX = "ch.";
public static final String I18N_OPTION_PREFIX = "option.";
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.openhab.binding.miio.internal.handler.MiIoUnsupportedHandler;
import org.openhab.binding.miio.internal.handler.MiIoVacuumHandler;
import org.openhab.core.common.ThreadPoolManager;
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.Thing;
import org.openhab.core.thing.ThingTypeUID;
Expand Down Expand Up @@ -60,18 +62,23 @@ public class MiIoHandlerFactory extends BaseThingHandlerFactory {
private CloudConnector cloudConnector;
private ChannelTypeRegistry channelTypeRegistry;
private BasicChannelTypeProvider basicChannelTypeProvider;
private final TranslationProvider i18nProvider;
private final LocaleProvider localeProvider;
private @Nullable Future<Boolean> scheduledTask;
private final Logger logger = LoggerFactory.getLogger(MiIoHandlerFactory.class);

@Activate
public MiIoHandlerFactory(@Reference HttpClientFactory httpClientFactory,
@Reference ChannelTypeRegistry channelTypeRegistry,
@Reference MiIoDatabaseWatchService miIoDatabaseWatchService, @Reference CloudConnector cloudConnector,
@Reference BasicChannelTypeProvider basicChannelTypeProvider, Map<String, Object> properties) {
@Reference BasicChannelTypeProvider basicChannelTypeProvider, @Reference TranslationProvider i18nProvider,
@Reference LocaleProvider localeProvider, Map<String, Object> properties) {
this.httpClientFactory = httpClientFactory;
this.miIoDatabaseWatchService = miIoDatabaseWatchService;
this.channelTypeRegistry = channelTypeRegistry;
this.basicChannelTypeProvider = basicChannelTypeProvider;
this.i18nProvider = i18nProvider;
this.localeProvider = localeProvider;
this.cloudConnector = cloudConnector;
@Nullable
String username = (String) properties.get("username");
Expand Down Expand Up @@ -108,16 +115,18 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
protected @Nullable ThingHandler createHandler(Thing thing) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (thingTypeUID.equals(THING_TYPE_MIIO)) {
return new MiIoGenericHandler(thing, miIoDatabaseWatchService, cloudConnector);
return new MiIoGenericHandler(thing, miIoDatabaseWatchService, cloudConnector, i18nProvider,
localeProvider);
}
if (thingTypeUID.equals(THING_TYPE_BASIC)) {
return new MiIoBasicHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry,
basicChannelTypeProvider);
basicChannelTypeProvider, i18nProvider, localeProvider);
}
if (thingTypeUID.equals(THING_TYPE_VACUUM)) {
return new MiIoVacuumHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry);
return new MiIoVacuumHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry,
i18nProvider, localeProvider);
}
return new MiIoUnsupportedHandler(thing, miIoDatabaseWatchService, cloudConnector,
httpClientFactory.getCommonHttpClient());
httpClientFactory.getCommonHttpClient(), i18nProvider, localeProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import org.openhab.core.cache.ExpiringCache;
import org.openhab.core.common.NamedThreadFactory;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.thing.ChannelUID;
Expand All @@ -60,6 +62,8 @@
import org.openhab.core.thing.binding.BaseThingHandler;
import org.openhab.core.thing.binding.builder.ThingBuilder;
import org.openhab.core.types.Command;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -81,6 +85,9 @@ public abstract class MiIoAbstractHandler extends BaseThingHandler implements Mi
protected static final int MAX_QUEUE = 5;
protected static final Gson GSON = new GsonBuilder().create();
protected static final String TIMESTAMP = "timestamp";
protected final Bundle bundle;
protected final TranslationProvider i18nProvider;
protected final LocaleProvider localeProvider;

protected ScheduledExecutorService miIoScheduler = new ScheduledThreadPoolExecutor(3,
new NamedThreadFactory("binding-" + getThing().getUID().getAsString(), true));
Expand Down Expand Up @@ -114,10 +121,13 @@ public abstract class MiIoAbstractHandler extends BaseThingHandler implements Mi
protected MiIoDatabaseWatchService miIoDatabaseWatchService;

public MiIoAbstractHandler(Thing thing, MiIoDatabaseWatchService miIoDatabaseWatchService,
CloudConnector cloudConnector) {
CloudConnector cloudConnector, TranslationProvider i18nProvider, LocaleProvider localeProvider) {
super(thing);
this.miIoDatabaseWatchService = miIoDatabaseWatchService;
this.cloudConnector = cloudConnector;
this.i18nProvider = i18nProvider;
this.localeProvider = localeProvider;
this.bundle = FrameworkUtil.getBundle(this.getClass());
}

@Override
Expand Down Expand Up @@ -590,7 +600,8 @@ private void changeType(final String modelId) {
String label = getThing().getLabel();
if (label == null || label.startsWith("Xiaomi Mi Device")) {
ThingBuilder thingBuilder = editThing();
thingBuilder.withLabel(miDevice.getDescription());
label = getLocalText(I18N_THING_PREFIX + modelId, miDevice.getDescription());
thingBuilder.withLabel(label);
updateThing(thingBuilder.build());
}
logger.info("Mi Device model {} identified as: {}. Does not match thingtype {}. Changing thingtype to {}",
Expand Down Expand Up @@ -644,4 +655,13 @@ public void onMessageReceived(MiIoSendCommand response) {
logger.debug("Error while handing message {}", response.getResponse(), e);
}
}

protected String getLocalText(String key, String defaultText) {
try {
String text = i18nProvider.getText(bundle, key, defaultText, localeProvider.getLocale());
return text != null ? text : defaultText;
} catch (IllegalArgumentException e) {
return defaultText;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
import org.openhab.binding.miio.internal.cloud.CloudConnector;
import org.openhab.binding.miio.internal.transport.MiIoAsyncCommunication;
import org.openhab.core.cache.ExpiringCache;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.HSBType;
import org.openhab.core.library.types.OnOffType;
Expand Down Expand Up @@ -106,8 +108,9 @@ public class MiIoBasicHandler extends MiIoAbstractHandler {

public MiIoBasicHandler(Thing thing, MiIoDatabaseWatchService miIoDatabaseWatchService,
CloudConnector cloudConnector, ChannelTypeRegistry channelTypeRegistry,
BasicChannelTypeProvider basicChannelTypeProvider) {
super(thing, miIoDatabaseWatchService, cloudConnector);
BasicChannelTypeProvider basicChannelTypeProvider, TranslationProvider i18nProvider,
LocaleProvider localeProvider) {
super(thing, miIoDatabaseWatchService, cloudConnector, i18nProvider, localeProvider);
this.channelTypeRegistry = channelTypeRegistry;
this.basicChannelTypeProvider = basicChannelTypeProvider;
}
Expand Down Expand Up @@ -452,6 +455,7 @@ private boolean buildChannelStructure(String deviceName) {
try {
JsonObject deviceMapping = Utils.convertFileToJSON(fn);
logger.debug("Using device database: {} for device {}", fn.getFile(), deviceName);
String key = fn.getFile().replaceFirst("/database/", "").split("json")[0];
Gson gson = new GsonBuilder().serializeNulls().create();
miioDevice = gson.fromJson(deviceMapping, MiIoBasicDevice.class);
for (Channel ch : getThing().getChannels()) {
Expand All @@ -475,7 +479,7 @@ private boolean buildChannelStructure(String deviceName) {
logger.debug("properties {}", miChannel);
if (!miChannel.getType().isEmpty()) {
basicChannelTypeProvider.addChannelType(miChannel, deviceName);
ChannelUID channelUID = addChannel(thingBuilder, miChannel, deviceName);
ChannelUID channelUID = addChannel(thingBuilder, miChannel, deviceName, key);
if (channelUID != null) {
actions.put(channelUID, miChannel);
channelsAdded++;
Expand Down Expand Up @@ -505,7 +509,8 @@ private boolean buildChannelStructure(String deviceName) {
return false;
}

private @Nullable ChannelUID addChannel(ThingBuilder thingBuilder, MiIoBasicChannel miChannel, String model) {
private @Nullable ChannelUID addChannel(ThingBuilder thingBuilder, MiIoBasicChannel miChannel, String model,
String key) {
String channel = miChannel.getChannel();
String dataType = miChannel.getType();
if (channel.isEmpty() || dataType.isEmpty()) {
Expand All @@ -514,7 +519,8 @@ private boolean buildChannelStructure(String deviceName) {
return null;
}
ChannelUID channelUID = new ChannelUID(getThing().getUID(), channel);
ChannelBuilder newChannel = ChannelBuilder.create(channelUID, dataType).withLabel(miChannel.getFriendlyName());
String label = getLocalText(I18N_CHANNEL_PREFIX + key + channel, miChannel.getFriendlyName());
ChannelBuilder newChannel = ChannelBuilder.create(channelUID, dataType).withLabel(label);
boolean useGeneratedChannelType = false;
if (!miChannel.getChannelType().isBlank()) {
ChannelTypeUID channelTypeUID = new ChannelTypeUID(miChannel.getChannelType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.miio.internal.basic.MiIoDatabaseWatchService;
import org.openhab.binding.miio.internal.cloud.CloudConnector;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.types.Command;
Expand All @@ -33,8 +35,8 @@ public class MiIoGenericHandler extends MiIoAbstractHandler {
private final Logger logger = LoggerFactory.getLogger(MiIoGenericHandler.class);

public MiIoGenericHandler(Thing thing, MiIoDatabaseWatchService miIoDatabaseWatchService,
CloudConnector cloudConnector) {
super(thing, miIoDatabaseWatchService, cloudConnector);
CloudConnector cloudConnector, TranslationProvider i18nProvider, LocaleProvider localeProvider) {
super(thing, miIoDatabaseWatchService, cloudConnector, i18nProvider, localeProvider);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import org.openhab.binding.miio.internal.cloud.CloudConnector;
import org.openhab.binding.miio.internal.miot.MiotParser;
import org.openhab.core.cache.ExpiringCache;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
Expand Down Expand Up @@ -87,8 +89,9 @@ public class MiIoUnsupportedHandler extends MiIoAbstractHandler {
});

public MiIoUnsupportedHandler(Thing thing, MiIoDatabaseWatchService miIoDatabaseWatchService,
CloudConnector cloudConnector, HttpClient httpClientFactory) {
super(thing, miIoDatabaseWatchService, cloudConnector);
CloudConnector cloudConnector, HttpClient httpClientFactory, TranslationProvider i18nProvider,
LocaleProvider localeProvider) {
super(thing, miIoDatabaseWatchService, cloudConnector, i18nProvider, localeProvider);
this.httpClient = httpClientFactory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
import org.openhab.binding.miio.internal.robot.VacuumErrorType;
import org.openhab.binding.miio.internal.transport.MiIoAsyncCommunication;
import org.openhab.core.cache.ExpiringCache;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.OnOffType;
Expand Down Expand Up @@ -113,8 +115,9 @@ public class MiIoVacuumHandler extends MiIoAbstractHandler {
private RRMapDrawOptions mapDrawOptions = new RRMapDrawOptions();

public MiIoVacuumHandler(Thing thing, MiIoDatabaseWatchService miIoDatabaseWatchService,
CloudConnector cloudConnector, ChannelTypeRegistry channelTypeRegistry) {
super(thing, miIoDatabaseWatchService, cloudConnector);
CloudConnector cloudConnector, ChannelTypeRegistry channelTypeRegistry, TranslationProvider i18nProvider,
LocaleProvider localeProvider) {
super(thing, miIoDatabaseWatchService, cloudConnector, i18nProvider, localeProvider);
this.channelTypeRegistry = channelTypeRegistry;
mapChannelUid = new ChannelUID(thing.getUID(), CHANNEL_VACUUM_MAP);
status = new ExpiringCache<>(CACHE_EXPIRY, () -> {
Expand Down
Loading

0 comments on commit 1f5ac60

Please sign in to comment.