]> git.basschouten.com Git - openhab-addons.git/blob
0e81eb687e32cd7d4b512296f1635c4a7d4f4537
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 import java.util.stream.Collectors;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.netatmo.internal.api.data.ModuleType;
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)
66                 .collect(Collectors.toList());
67     }
68
69     @Override
70     public @Nullable ThingType getThingType(ThingTypeUID thingTypeUID, @Nullable Locale locale) {
71         if (BINDING_ID.equalsIgnoreCase(thingTypeUID.getBindingId())) {
72             try {
73                 ModuleType moduleType = ModuleType.from(thingTypeUID);
74
75                 ThingTypeBuilder thingTypeBuilder = ThingTypeBuilder.instance(thingTypeUID, thingTypeUID.toString())
76                         .withRepresentationProperty(EQUIPMENT_ID).withExtensibleChannelTypeIds(moduleType.extensions)
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.groupTypes.stream().map(groupTypeName -> new ChannelGroupDefinition(toGroupName(groupTypeName),
96                 new ChannelGroupTypeUID(BINDING_ID, groupTypeName))).collect(Collectors.toList());
97     }
98
99     public static String toGroupName(String groupeTypeName) {
100         return groupeTypeName.replace(OPTION_EXTENDED, "").replace(OPTION_OUTSIDE, "");
101     }
102 }