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