Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
Add i18n support to LIFX (#3312)
Browse files Browse the repository at this point in the history
Signed-off-by: Wouter Born <eclipse@maindrain.net>
  • Loading branch information
wborn authored and kaikreuzer committed Apr 26, 2017
1 parent dafbc9a commit dc49b62
Show file tree
Hide file tree
Showing 16 changed files with 230 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# binding
binding.lifx.name = LIFX Binding
binding.lifx.description = This is the binding for LIFX lights.

# thing types
thing-type.lifx.colorlight.label = LIFX Color Light
thing-type.lifx.colorirlight.label = LIFX Color IR Light
thing-type.lifx.colormzlight.label = LIFX Color MultiZone Light
thing-type.lifx.whitelight.label = LIFX White Light

# thing type configuration
thing-type.config.lifx.light.deviceId.label = LIFX device ID
thing-type.config.lifx.light.fadetime.label = Fade time
thing-type.config.lifx.light.fadetime.description = The time to fade to the new color value (in ms).

# channel types
channel-type.lifx.brightness.label = Brightness
channel-type.lifx.brightness.description = Sets the brightness of the light
channel-type.lifx.color.label = Color
channel-type.lifx.color.description = Selects the color of the light
channel-type.lifx.colorzone.label = Color zone {0}
channel-type.lifx.colorzone.description = Selects the zone {0} color of the light
channel-type.lifx.infrared.label = Infrared
channel-type.lifx.infrared.description = Sets the infrared of the light
channel-type.lifx.temperature.label = Temperature
channel-type.lifx.temperature.description = Sets the temperature of the light
channel-type.lifx.temperaturezone.label = Temperature zone {0}
channel-type.lifx.temperaturezone.description = Sets the zone {0} temperature of the light

# channel type configuration
channel-type.config.lifx.brightness.powerOnBrightness.label = Power on brightness
channel-type.config.lifx.brightness.powerOnBrightness.description = Brightness level used when switching on the light. Use empty value to leave brightness as is.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# binding
binding.lifx.name = LIFX Binding
binding.lifx.description = Dit is de binding voor LIFX lampen.

# thing types
thing-type.lifx.colorlight.label = LIFX Kleuren Lamp
thing-type.lifx.colorirlight.label = LIFX Kleuren IR Lamp
thing-type.lifx.colormzlight.label = LIFX Kleuren MultiZone Lamp
thing-type.lifx.whitelight.label = LIFX Wittinten Lamp

# thing type configuration
thing-type.config.lifx.light.deviceId.label = LIFX apparaat ID
thing-type.config.lifx.light.fadetime.label = Vervagingsduur
thing-type.config.lifx.light.fadetime.description = De tijdsduur van het vervagen naar een nieuwe kleur (in ms).

# channel types
channel-type.lifx.brightness.label = Helderheid
channel-type.lifx.brightness.description = Bepaalt de helderheid van de lamp
channel-type.lifx.color.label = Kleur
channel-type.lifx.color.description = Bepaalt de kleur van de lamp
channel-type.lifx.colorzone.label = Kleur zone {0}
channel-type.lifx.colorzone.description = Bepaalt de kleur van lampzone {0}
channel-type.lifx.infrared.label = Infrarood
channel-type.lifx.infrared.description = Bepaalt het infraroodniveau van de lamp
channel-type.lifx.temperature.label = Temperatuur
channel-type.lifx.temperature.description = Bepaalt de kleurtemperatuur van de lamp
channel-type.lifx.temperaturezone.label = Temperatuur zone {0}
channel-type.lifx.temperaturezone.description = Bepaalt de kleurtemperatuur van lampzone {0}

# channel type configuration
channel-type.config.lifx.brightness.powerOnBrightness.label = Inschakelhelderheid
channel-type.config.lifx.brightness.powerOnBrightness.description = Het helderheidsniveau bij inschakeling van de lamp. Gebruik een lege waarde om de helderheid ongewijzigd te laten.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ Import-Package: com.google.common.collect,
org.eclipse.smarthome.config.core,
org.eclipse.smarthome.config.discovery,
org.eclipse.smarthome.core.common,
org.eclipse.smarthome.core.i18n,
org.eclipse.smarthome.core.library.types,
org.eclipse.smarthome.core.thing,
org.eclipse.smarthome.core.thing.binding,
org.eclipse.smarthome.core.thing.binding.builder,
org.eclipse.smarthome.core.thing.type,
org.eclipse.smarthome.core.types,
org.osgi.framework,
org.osgi.service.component,
org.slf4j
Service-Component: OSGI-INF/*.xml
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2014-2017 by the respective copyright holders.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/epl-v10.html
-->
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" immediate="true" name="org.eclipse.smarthome.binding.lifx.internal.LifxChannelFactory">

<implementation class="org.eclipse.smarthome.binding.lifx.internal.LifxChannelFactoryImpl"/>

<service>
<provide interface="org.eclipse.smarthome.binding.lifx.internal.LifxChannelFactory"/>
</service>

<reference bind="setI18nProvider" cardinality="1..1" interface="org.eclipse.smarthome.core.i18n.I18nProvider" name="I18nprovider" policy="dynamic" unbind="unsetI18nProvider"/>
<reference bind="setLocaleProvider" cardinality="1..1" interface="org.eclipse.smarthome.core.i18n.LocaleProvider" name="LocaleProvider" policy="static" unbind="unsetLocaleProvider"/>

</scr:component>
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@
<provide interface="org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory"/>
</service>

<reference bind="setChannelFactory" cardinality="1..1" interface="org.eclipse.smarthome.binding.lifx.internal.LifxChannelFactory" name="ChannelFactory" policy="dynamic" unbind="unsetChannelFactory"/>

</scr:component>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.concurrent.locks.ReentrantLock;

import org.eclipse.smarthome.binding.lifx.LifxBindingConstants;
import org.eclipse.smarthome.binding.lifx.internal.LifxChannelFactory;
import org.eclipse.smarthome.binding.lifx.internal.LifxLightCommunicationHandler;
import org.eclipse.smarthome.binding.lifx.internal.LifxLightCurrentStateUpdater;
import org.eclipse.smarthome.binding.lifx.internal.LifxLightOnlineStateUpdater;
Expand All @@ -42,7 +43,6 @@
import org.eclipse.smarthome.core.thing.ThingStatus;
import org.eclipse.smarthome.core.thing.ThingStatusDetail;
import org.eclipse.smarthome.core.thing.binding.BaseThingHandler;
import org.eclipse.smarthome.core.thing.binding.builder.ChannelBuilder;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.RefreshType;
import org.eclipse.smarthome.core.types.State;
Expand All @@ -66,6 +66,7 @@ public class LifxLightHandler extends BaseThingHandler {
private static final long FADE_TIME_DEFAULT = 300;
private static final int MAX_STATE_CHANGE_DURATION = 4000;

private LifxChannelFactory channelFactory;
private Products product;

private long fadeTime = FADE_TIME_DEFAULT;
Expand Down Expand Up @@ -180,8 +181,9 @@ private void updateZoneChannels(PowerState powerState, HSBK[] colors) {

}

public LifxLightHandler(Thing thing) {
public LifxLightHandler(Thing thing, LifxChannelFactory channelFactory) {
super(thing);
this.channelFactory = channelFactory;
}

@Override
Expand Down Expand Up @@ -300,12 +302,8 @@ private void addRemoveZoneChannels(int zones) {

// add zone channels
for (int i = 0; i < zones; i++) {
newChannels
.add(ChannelBuilder.create(new ChannelUID(getThing().getUID(), CHANNEL_COLOR_ZONE + i), "ColorItem")
.withType(CHANNEL_TYPE_COLOR_ZONE).withLabel("Color zone " + i).build());
newChannels.add(ChannelBuilder
.create(new ChannelUID(getThing().getUID(), CHANNEL_TEMPERATURE_ZONE + i), "DimmerItem")
.withType(CHANNEL_TYPE_TEMPERATURE_ZONE).withLabel("Temperature zone " + i).build());
newChannels.add(channelFactory.createColorZoneChannel(getThing().getUID(), i));
newChannels.add(channelFactory.createTemperatureZoneChannel(getThing().getUID(), i));
}

updateThing(editThing().withChannels(newChannels).build());
Expand Down Expand Up @@ -506,4 +504,5 @@ private void updateStateIfChanged(String channel, State newState) {
channelStates.put(channel, newState);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (c) 2014-2017 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.smarthome.binding.lifx.internal;

import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.ThingUID;

/**
* The {@link LifxChannelFactory} creates dynamic LIFX channels.
*
* @author Wouter Born - Add i18n support
*/
public interface LifxChannelFactory {

Channel createColorZoneChannel(ThingUID thingUID, int index);

Channel createTemperatureZoneChannel(ThingUID thingUID, int index);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Copyright (c) 2014-2017 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.smarthome.binding.lifx.internal;

import static org.eclipse.smarthome.binding.lifx.LifxBindingConstants.*;

import java.util.Locale;

import org.eclipse.smarthome.core.i18n.I18nProvider;
import org.eclipse.smarthome.core.i18n.LocaleProvider;
import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.eclipse.smarthome.core.thing.binding.builder.ChannelBuilder;
import org.osgi.framework.Bundle;
import org.osgi.service.component.ComponentContext;

/**
* The {@link LifxChannelFactoryImpl} creates dynamic LIFX channels.
*
* @author Wouter Born - Add i18n support
*/
public class LifxChannelFactoryImpl implements LifxChannelFactory {

private static final String COLOR_ZONE_LABEL_KEY = "channel-type.lifx.colorzone.label";
private static final String COLOR_ZONE_DESCRIPTION_KEY = "channel-type.lifx.colorzone.description";

private static final String TEMPERATURE_ZONE_LABEL_KEY = "channel-type.lifx.temperaturezone.label";
private static final String TEMPERATURE_ZONE_DESCRIPTION_KEY = "channel-type.lifx.temperaturezone.description";

private Bundle bundle;
private I18nProvider i18nProvider;
private LocaleProvider localeProvider;

@Override
public Channel createColorZoneChannel(ThingUID thingUID, int index) {
String label = getText(COLOR_ZONE_LABEL_KEY, index);
String description = getText(COLOR_ZONE_DESCRIPTION_KEY, index);
return ChannelBuilder.create(new ChannelUID(thingUID, CHANNEL_COLOR_ZONE + index), "ColorItem")
.withType(CHANNEL_TYPE_COLOR_ZONE).withLabel(label).withDescription(description).build();
}

@Override
public Channel createTemperatureZoneChannel(ThingUID thingUID, int index) {
String label = getText(TEMPERATURE_ZONE_LABEL_KEY, index);
String description = getText(TEMPERATURE_ZONE_DESCRIPTION_KEY, index);
return ChannelBuilder.create(new ChannelUID(thingUID, CHANNEL_TEMPERATURE_ZONE + index), "DimmerItem")
.withType(CHANNEL_TYPE_TEMPERATURE_ZONE).withLabel(label).withDescription(description).build();
}

private String getDefaultText(String key) {
return i18nProvider.getText(bundle, key, key, Locale.ENGLISH);
}

private String getText(String key, Object... arguments) {
Locale locale = localeProvider != null ? localeProvider.getLocale() : Locale.ENGLISH;
return i18nProvider != null ? i18nProvider.getText(bundle, key, getDefaultText(key), locale, arguments) : key;
}

protected void activate(ComponentContext componentContext) {
this.bundle = componentContext.getBundleContext().getBundle();
}

protected void deactivate(ComponentContext componentContext) {
this.bundle = null;
}

protected void setI18nProvider(I18nProvider i18nProvider) {
this.i18nProvider = i18nProvider;
}

protected void unsetI18nProvider(I18nProvider i18nProvider) {
this.i18nProvider = null;
}

protected void setLocaleProvider(LocaleProvider localeProvider) {
this.localeProvider = localeProvider;
}

protected void unsetLocaleProvider(LocaleProvider localeProvider) {
this.localeProvider = null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
import org.osgi.service.component.ComponentContext;

/**
* The {@link LifxHandlerFactory} is responsible for creating things and thing
* handlers.
* The {@link LifxHandlerFactory} is responsible for creating things and thing handlers.
*
* @author Dennis Nobel - Initial contribution
* @author Karel Goderis - Remove dependency on external libraries
*/
public class LifxHandlerFactory extends BaseThingHandlerFactory {

private LifxChannelFactory channelFactory;

@Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
return SUPPORTED_THING_TYPES.contains(thingTypeUID);
Expand All @@ -38,7 +39,7 @@ protected void activate(ComponentContext componentContext) {
@Override
protected ThingHandler createHandler(Thing thing) {
if (supportsThingType(thing.getThingTypeUID())) {
return new LifxLightHandler(thing);
return new LifxLightHandler(thing, channelFactory);
}

return null;
Expand All @@ -48,4 +49,13 @@ protected ThingHandler createHandler(Thing thing) {
protected void deactivate(ComponentContext componentContext) {
super.deactivate(componentContext);
}

protected void setChannelFactory(LifxChannelFactory channelFactory) {
this.channelFactory = channelFactory;
}

protected void unsetChannelFactory(LifxChannelFactory channelFactory) {
this.channelFactory = null;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2014-2016 by the respective copyright holders.
* Copyright (c) 2014-2017 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2014-2016 by the respective copyright holders.
* Copyright (c) 2014-2017 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2014-2016 by the respective copyright holders.
* Copyright (c) 2014-2017 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2014-2016 by the respective copyright holders.
* Copyright (c) 2014-2017 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2014-2016 by the respective copyright holders.
* Copyright (c) 2014-2017 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2014-2016 by the respective copyright holders.
* Copyright (c) 2014-2017 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2014-2016 by the respective copyright holders.
* Copyright (c) 2014-2017 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down

0 comments on commit dc49b62

Please sign in to comment.