2 * Copyright (c) 2010-2023 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.netatmo.internal.providers;
15 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.Locale;
20 import java.util.Optional;
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;
42 * Extends the ThingTypeProvider to generated Thing types based on {@link ModuleType} enum.
44 * @author Gaƫl L'hopital - Initial contribution
48 @Component(service = ThingTypeProvider.class)
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;
56 public NetatmoThingTypeProvider(final @Reference ThingTypeI18nLocalizationService localizationService,
57 ComponentContext componentContext) {
58 this.bundle = componentContext.getBundleContext().getBundle();
59 this.localizationService = localizationService;
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();
69 public @Nullable ThingType getThingType(ThingTypeUID thingTypeUID, @Nullable Locale locale) {
70 if (BINDING_ID.equalsIgnoreCase(thingTypeUID.getBindingId())) {
72 ModuleType moduleType = ModuleType.from(thingTypeUID);
74 ThingTypeBuilder thingTypeBuilder = ThingTypeBuilder.instance(thingTypeUID, thingTypeUID.toString())
75 .withRepresentationProperty(NAThingConfiguration.ID)
76 .withExtensibleChannelTypeIds(moduleType.getExtensions())
77 .withChannelGroupDefinitions(getGroupDefinitions(moduleType))
78 .withConfigDescriptionURI(moduleType.getConfigDescription());
80 ThingTypeUID bridgeType = moduleType.getBridge().thingTypeUID;
81 if (!ModuleType.UNKNOWN.thingTypeUID.equals(bridgeType)) {
82 thingTypeBuilder.withSupportedBridgeTypeUIDs(List.of(bridgeType.getAsString()));
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());
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();
99 public static String toGroupName(String groupeTypeName) {
100 String result = groupeTypeName;
101 for (String variation : GROUP_VARIATIONS) {
102 result = result.replace(variation, "");