2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.lifx.internal;
15 import static org.openhab.binding.lifx.internal.LifxBindingConstants.*;
17 import java.util.Locale;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.i18n.LocaleProvider;
22 import org.openhab.core.i18n.TranslationProvider;
23 import org.openhab.core.thing.Channel;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.ThingUID;
26 import org.openhab.core.thing.binding.builder.ChannelBuilder;
27 import org.osgi.framework.Bundle;
28 import org.osgi.service.component.ComponentContext;
29 import org.osgi.service.component.annotations.Activate;
30 import org.osgi.service.component.annotations.Component;
31 import org.osgi.service.component.annotations.Deactivate;
32 import org.osgi.service.component.annotations.Reference;
35 * The {@link LifxChannelFactoryImpl} creates dynamic LIFX channels.
37 * @author Wouter Born - Initial contribution
40 @Component(service = LifxChannelFactory.class)
41 public class LifxChannelFactoryImpl implements LifxChannelFactory {
43 private static final String COLOR_ZONE_LABEL_KEY = "channel-type.lifx.colorzone.label";
44 private static final String COLOR_ZONE_DESCRIPTION_KEY = "channel-type.lifx.colorzone.description";
46 private static final String TEMPERATURE_ZONE_LABEL_KEY = "channel-type.lifx.temperaturezone.label";
47 private static final String TEMPERATURE_ZONE_DESCRIPTION_KEY = "channel-type.lifx.temperaturezone.description";
49 private @NonNullByDefault({}) Bundle bundle;
50 private @NonNullByDefault({}) TranslationProvider i18nProvider;
51 private @NonNullByDefault({}) LocaleProvider localeProvider;
54 public Channel createColorZoneChannel(ThingUID thingUID, int index) {
55 String label = getText(COLOR_ZONE_LABEL_KEY, index);
56 String description = getText(COLOR_ZONE_DESCRIPTION_KEY, index);
57 return ChannelBuilder.create(new ChannelUID(thingUID, CHANNEL_COLOR_ZONE + index), "Color")
58 .withType(CHANNEL_TYPE_COLOR_ZONE).withLabel(label).withDescription(description).build();
62 public Channel createTemperatureZoneChannel(ThingUID thingUID, int index) {
63 String label = getText(TEMPERATURE_ZONE_LABEL_KEY, index);
64 String description = getText(TEMPERATURE_ZONE_DESCRIPTION_KEY, index);
65 return ChannelBuilder.create(new ChannelUID(thingUID, CHANNEL_TEMPERATURE_ZONE + index), "Dimmer")
66 .withType(CHANNEL_TYPE_TEMPERATURE_ZONE).withLabel(label).withDescription(description).build();
69 private @Nullable String getDefaultText(String key) {
70 return i18nProvider.getText(bundle, key, key, Locale.ENGLISH);
73 private String getText(String key, Object... arguments) {
74 Locale locale = localeProvider != null ? localeProvider.getLocale() : Locale.ENGLISH;
75 if (i18nProvider == null) {
79 String text = i18nProvider.getText(bundle, key, getDefaultText(key), locale, arguments);
80 return text != null ? text : key;
84 protected void activate(ComponentContext componentContext) {
85 this.bundle = componentContext.getBundleContext().getBundle();
89 protected void deactivate(ComponentContext componentContext) {
94 protected void setTranslationProvider(TranslationProvider i18nProvider) {
95 this.i18nProvider = i18nProvider;
98 protected void unsetTranslationProvider(TranslationProvider i18nProvider) {
99 this.i18nProvider = null;
103 protected void setLocaleProvider(LocaleProvider localeProvider) {
104 this.localeProvider = localeProvider;
107 protected void unsetLocaleProvider(LocaleProvider localeProvider) {
108 this.localeProvider = null;