]> git.basschouten.com Git - openhab-addons.git/blob
cb66f0e220775351481176b79d1a21c225ba9902
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.netatmo.internal.providers;
14
15 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
16
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.Locale;
20 import java.util.Optional;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.netatmo.internal.api.data.ModuleType;
25 import org.openhab.binding.netatmo.internal.config.NAThingConfiguration;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.binding.ThingTypeProvider;
28 import org.openhab.core.thing.i18n.ThingTypeI18nLocalizationService;
29 import org.openhab.core.thing.type.ChannelGroupDefinition;
30 import org.openhab.core.thing.type.ChannelGroupTypeUID;
31 import org.openhab.core.thing.type.ThingType;
32 import org.openhab.core.thing.type.ThingTypeBuilder;
33 import org.osgi.framework.Bundle;
34 import org.osgi.service.component.ComponentContext;
35 import org.osgi.service.component.annotations.Activate;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Reference;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * Extends the ThingTypeProvider to generated Thing types based on {@link ModuleType} enum.
43  *
44  * @author GaĆ«l L'hopital - Initial contribution
45  *
46  */
47
48 @Component(service = ThingTypeProvider.class)
49 @NonNullByDefault
50 public class NetatmoThingTypeProvider implements ThingTypeProvider {
51     private final Logger logger = LoggerFactory.getLogger(NetatmoThingTypeProvider.class);
52     private final ThingTypeI18nLocalizationService localizationService;
53     private final Bundle bundle;
54
55     @Activate
56     public NetatmoThingTypeProvider(final @Reference ThingTypeI18nLocalizationService localizationService,
57             ComponentContext componentContext) {
58         this.bundle = componentContext.getBundleContext().getBundle();
59         this.localizationService = localizationService;
60     }
61
62     @Override
63     public Collection<ThingType> getThingTypes(@Nullable Locale locale) {
64         return ModuleType.AS_SET.stream().filter(mt -> mt != ModuleType.UNKNOWN)
65                 .map(mt -> Optional.ofNullable(getThingType(mt.thingTypeUID, locale))).map(Optional::get).toList();
66     }
67
68     @Override
69     public @Nullable ThingType getThingType(ThingTypeUID thingTypeUID, @Nullable Locale locale) {
70         if (BINDING_ID.equalsIgnoreCase(thingTypeUID.getBindingId())) {
71             try {
72                 ModuleType moduleType = ModuleType.from(thingTypeUID);
73
74                 ThingTypeBuilder thingTypeBuilder = ThingTypeBuilder.instance(thingTypeUID, thingTypeUID.toString())
75                         .withRepresentationProperty(NAThingConfiguration.ID)
76                         .withExtensibleChannelTypeIds(moduleType.getExtensions())
77                         .withChannelGroupDefinitions(getGroupDefinitions(moduleType))
78                         .withConfigDescriptionURI(moduleType.getConfigDescription());
79
80                 ThingTypeUID bridgeType = moduleType.getBridge().thingTypeUID;
81                 if (!ModuleType.UNKNOWN.thingTypeUID.equals(bridgeType)) {
82                     thingTypeBuilder.withSupportedBridgeTypeUIDs(List.of(bridgeType.getAsString()));
83                 }
84
85                 return localizationService.createLocalizedThingType(bundle,
86                         moduleType.isABridge() ? thingTypeBuilder.buildBridge() : thingTypeBuilder.build(), locale);
87             } catch (IllegalArgumentException e) {
88                 logger.warn("Unable to define ModuleType for thingType {} : {}", thingTypeUID.getId(), e.getMessage());
89             }
90         }
91         return null;
92     }
93
94     private List<ChannelGroupDefinition> getGroupDefinitions(ModuleType thingType) {
95         return thingType.getGroupTypes().stream().map(groupType -> new ChannelGroupDefinition(toGroupName(groupType),
96                 new ChannelGroupTypeUID(BINDING_ID, groupType))).toList();
97     }
98
99     public static String toGroupName(String groupeTypeName) {
100         String result = groupeTypeName;
101         for (String variation : GROUP_VARIATIONS) {
102             result = result.replace(variation, "");
103         }
104         return result;
105     }
106 }