]> git.basschouten.com Git - openhab-addons.git/blob
dbda90c06ad0d5f85839f4661a9899c03ab4b37a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.lifx.internal;
14
15 import static org.openhab.binding.lifx.internal.LifxBindingConstants.*;
16
17 import java.util.Locale;
18
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;
33
34 /**
35  * The {@link LifxChannelFactoryImpl} creates dynamic LIFX channels.
36  *
37  * @author Wouter Born - Add i18n support
38  */
39 @NonNullByDefault
40 @Component(service = LifxChannelFactory.class, immediate = true)
41 public class LifxChannelFactoryImpl implements LifxChannelFactory {
42
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";
45
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";
48
49     private @NonNullByDefault({}) Bundle bundle;
50     private @NonNullByDefault({}) TranslationProvider i18nProvider;
51     private @NonNullByDefault({}) LocaleProvider localeProvider;
52
53     @Override
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();
59     }
60
61     @Override
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();
67     }
68
69     private @Nullable String getDefaultText(String key) {
70         return i18nProvider.getText(bundle, key, key, Locale.ENGLISH);
71     }
72
73     private String getText(String key, Object... arguments) {
74         Locale locale = localeProvider != null ? localeProvider.getLocale() : Locale.ENGLISH;
75         if (i18nProvider == null) {
76             return key;
77         }
78
79         String text = i18nProvider.getText(bundle, key, getDefaultText(key), locale, arguments);
80         return text != null ? text : key;
81     }
82
83     @Activate
84     protected void activate(ComponentContext componentContext) {
85         this.bundle = componentContext.getBundleContext().getBundle();
86     }
87
88     @Deactivate
89     protected void deactivate(ComponentContext componentContext) {
90         this.bundle = null;
91     }
92
93     @Reference
94     protected void setTranslationProvider(TranslationProvider i18nProvider) {
95         this.i18nProvider = i18nProvider;
96     }
97
98     protected void unsetTranslationProvider(TranslationProvider i18nProvider) {
99         this.i18nProvider = null;
100     }
101
102     @Reference
103     protected void setLocaleProvider(LocaleProvider localeProvider) {
104         this.localeProvider = localeProvider;
105     }
106
107     protected void unsetLocaleProvider(LocaleProvider localeProvider) {
108         this.localeProvider = null;
109     }
110 }