[openuv] Fix internationalization of discovery result (#11500)

* [openuv] Fix internationalization of discovery result

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
lolodomo 2021-12-05 09:39:51 +01:00 committed by GitHub
parent 3d2663cbf7
commit 0a997ec5d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 4 deletions

View File

@ -20,8 +20,10 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.openuv.internal.handler.OpenUVBridgeHandler;
import org.openhab.binding.openuv.internal.handler.OpenUVReportHandler;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.LocationProvider;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID;
@ -48,12 +50,17 @@ import com.google.gson.JsonDeserializer;
public class OpenUVHandlerFactory extends BaseThingHandlerFactory {
private final LocationProvider locationProvider;
private final TranslationProvider i18nProvider;
private final LocaleProvider localeProvider;
private final Gson gson;
@Activate
public OpenUVHandlerFactory(@Reference TimeZoneProvider timeZoneProvider,
@Reference LocationProvider locationProvider) {
@Reference LocationProvider locationProvider, @Reference TranslationProvider i18nProvider,
@Reference LocaleProvider localeProvider) {
this.locationProvider = locationProvider;
this.i18nProvider = i18nProvider;
this.localeProvider = localeProvider;
this.gson = new GsonBuilder()
.registerTypeAdapter(ZonedDateTime.class,
(JsonDeserializer<ZonedDateTime>) (json, type, jsonDeserializationContext) -> ZonedDateTime
@ -72,7 +79,7 @@ public class OpenUVHandlerFactory extends BaseThingHandlerFactory {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
return APIBRIDGE_THING_TYPE.equals(thingTypeUID)
? new OpenUVBridgeHandler((Bridge) thing, locationProvider, gson)
? new OpenUVBridgeHandler((Bridge) thing, locationProvider, i18nProvider, localeProvider, gson)
: LOCATION_REPORT_THING_TYPE.equals(thingTypeUID) ? new OpenUVReportHandler(thing) : null;
}
}

View File

@ -50,7 +50,10 @@ public class OpenUVDiscoveryService extends AbstractDiscoveryService implements
@Override
public void setThingHandler(ThingHandler handler) {
if (handler instanceof OpenUVBridgeHandler) {
this.bridgeHandler = (OpenUVBridgeHandler) handler;
OpenUVBridgeHandler localHandler = (OpenUVBridgeHandler) handler;
this.bridgeHandler = localHandler;
this.i18nProvider = localHandler.getI18nProvider();
this.localeProvider = localHandler.getLocaleProvider();
}
}

View File

@ -29,7 +29,9 @@ import org.openhab.binding.openuv.internal.config.BridgeConfiguration;
import org.openhab.binding.openuv.internal.discovery.OpenUVDiscoveryService;
import org.openhab.binding.openuv.internal.json.OpenUVResponse;
import org.openhab.binding.openuv.internal.json.OpenUVResult;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.LocationProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.io.net.http.HttpUtil;
import org.openhab.core.library.types.PointType;
import org.openhab.core.thing.Bridge;
@ -61,13 +63,18 @@ public class OpenUVBridgeHandler extends BaseBridgeHandler {
private final Properties header = new Properties();
private final Gson gson;
private final LocationProvider locationProvider;
private final TranslationProvider i18nProvider;
private final LocaleProvider localeProvider;
private @Nullable ScheduledFuture<?> reconnectJob;
public OpenUVBridgeHandler(Bridge bridge, LocationProvider locationProvider, Gson gson) {
public OpenUVBridgeHandler(Bridge bridge, LocationProvider locationProvider, TranslationProvider i18nProvider,
LocaleProvider localeProvider, Gson gson) {
super(bridge);
this.gson = gson;
this.locationProvider = locationProvider;
this.i18nProvider = i18nProvider;
this.localeProvider = localeProvider;
}
@Override
@ -149,4 +156,12 @@ public class OpenUVBridgeHandler extends BaseBridgeHandler {
public @Nullable PointType getLocation() {
return locationProvider.getLocation();
}
public TranslationProvider getI18nProvider() {
return i18nProvider;
}
public LocaleProvider getLocaleProvider() {
return localeProvider;
}
}