Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[miio] i18n translation handling for basic channels #11576

Merged
merged 5 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("thing." + 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 @@ -428,6 +431,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 @@ -451,7 +455,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 @@ -481,7 +485,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 @@ -490,7 +495,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("ch." + 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