[hue] Internationalization of discovery results (#11501)

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
lolodomo 2021-11-08 13:53:42 +01:00 committed by GitHub
parent dfe577aec5
commit c79fd06256
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 6 deletions

View File

@ -33,6 +33,8 @@ import org.openhab.binding.hue.internal.handler.sensors.PresenceHandler;
import org.openhab.binding.hue.internal.handler.sensors.TapSwitchHandler;
import org.openhab.binding.hue.internal.handler.sensors.TemperatureHandler;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID;
@ -68,10 +70,15 @@ public class HueThingHandlerFactory extends BaseThingHandlerFactory {
.flatMap(i -> i).collect(Collectors.toSet()));
private final HueStateDescriptionProvider stateDescriptionProvider;
private final TranslationProvider i18nProvider;
private final LocaleProvider localeProvider;
@Activate
public HueThingHandlerFactory(final @Reference HueStateDescriptionProvider stateDescriptionProvider) {
public HueThingHandlerFactory(final @Reference HueStateDescriptionProvider stateDescriptionProvider,
final @Reference TranslationProvider i18nProvider, final @Reference LocaleProvider localeProvider) {
this.stateDescriptionProvider = stateDescriptionProvider;
this.i18nProvider = i18nProvider;
this.localeProvider = localeProvider;
}
@Override
@ -142,7 +149,7 @@ public class HueThingHandlerFactory extends BaseThingHandlerFactory {
@Override
protected @Nullable ThingHandler createHandler(Thing thing) {
if (HueBridgeHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
return new HueBridgeHandler((Bridge) thing, stateDescriptionProvider);
return new HueBridgeHandler((Bridge) thing, stateDescriptionProvider, i18nProvider, localeProvider);
} else if (HueLightHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
return new HueLightHandler(thing, stateDescriptionProvider);
} else if (DimmerSwitchHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {

View File

@ -111,8 +111,11 @@ public class HueDeviceDiscoveryService extends AbstractDiscoveryService
@Override
public void setThingHandler(@Nullable ThingHandler handler) {
if (handler instanceof HueBridgeHandler) {
hueBridgeHandler = (HueBridgeHandler) handler;
HueBridgeHandler localHandler = (HueBridgeHandler) handler;
hueBridgeHandler = localHandler;
bridgeUID = handler.getThing().getUID();
i18nProvider = localHandler.getI18nProvider();
localeProvider = localHandler.getLocaleProvider();
}
}
@ -275,8 +278,14 @@ public class HueDeviceDiscoveryService extends AbstractDiscoveryService
Map<String, Object> properties = new HashMap<>();
properties.put(GROUP_ID, group.getId());
String name = String.format("%s (%s)", "0".equals(group.getId()) ? "All lights" : group.getName(),
group.getType());
String name;
if ("0".equals(group.getId())) {
name = "@text/discovery.group.all_lights.label";
} else if ("Room".equals(group.getType())) {
name = group.getName();
} else {
name = String.format("%s (%s)", group.getName(), group.getType());
}
DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withThingType(THING_TYPE_GROUP)
.withProperties(properties).withBridge(localBridgeUID).withRepresentationProperty(GROUP_ID)
.withLabel(name).build();

View File

@ -54,6 +54,8 @@ import org.openhab.binding.hue.internal.exceptions.LinkButtonException;
import org.openhab.binding.hue.internal.exceptions.UnauthorizedException;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.config.core.status.ConfigStatusMessage;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.library.types.HSBType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.StringType;
@ -98,6 +100,8 @@ public class HueBridgeHandler extends ConfigStatusBridgeHandler implements HueCl
private final Logger logger = LoggerFactory.getLogger(HueBridgeHandler.class);
private final HueStateDescriptionProvider stateDescriptionOptionProvider;
private final TranslationProvider i18nProvider;
private final LocaleProvider localeProvider;
private final Map<String, FullLight> lastLightStates = new ConcurrentHashMap<>();
private final Map<String, FullSensor> lastSensorStates = new ConcurrentHashMap<>();
@ -403,9 +407,12 @@ public class HueBridgeHandler extends ConfigStatusBridgeHandler implements HueCl
private List<String> consoleScenesList = new ArrayList<>();
public HueBridgeHandler(Bridge bridge, HueStateDescriptionProvider stateDescriptionOptionProvider) {
public HueBridgeHandler(Bridge bridge, HueStateDescriptionProvider stateDescriptionOptionProvider,
TranslationProvider i18nProvider, LocaleProvider localeProvider) {
super(bridge);
this.stateDescriptionOptionProvider = stateDescriptionOptionProvider;
this.i18nProvider = i18nProvider;
this.localeProvider = localeProvider;
}
@Override
@ -1040,4 +1047,12 @@ public class HueBridgeHandler extends ConfigStatusBridgeHandler implements HueCl
return List.of();
}
}
public TranslationProvider getI18nProvider() {
return i18nProvider;
}
public LocaleProvider getLocaleProvider() {
return localeProvider;
}
}

View File

@ -225,3 +225,7 @@ actionInputFadeTimeLabel = FadeTime
actionInputFadeTimeDesc = The fade time to use for the light command in ms.
actionLabel = send a light command with a custom fade time
actionDesc = Send a light command with a custom fade time.
# discovery results
discovery.group.all_lights.label = All lights