2 * Copyright (c) 2010-2024 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;
21 import java.util.Optional;
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;
43 * Extends the ThingTypeProvider to generated Thing types based on {@link ModuleType} enum.
45 * @author Gaƫl L'hopital - Initial contribution
49 @Component(service = ThingTypeProvider.class)
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;
57 public NetatmoThingTypeProvider(final @Reference ThingTypeI18nLocalizationService localizationService,
58 ComponentContext componentContext) {
59 this.bundle = componentContext.getBundleContext().getBundle();
60 this.localizationService = localizationService;
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();
70 public @Nullable ThingType getThingType(ThingTypeUID thingTypeUID, @Nullable Locale locale) {
71 if (BINDING_ID.equalsIgnoreCase(thingTypeUID.getBindingId())) {
73 ModuleType moduleType = ModuleType.from(thingTypeUID);
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());
82 ThingTypeUID bridgeType = moduleType.getBridge().thingTypeUID;
83 if (!ModuleType.UNKNOWN.thingTypeUID.equals(bridgeType)) {
84 thingTypeBuilder.withSupportedBridgeTypeUIDs(List.of(bridgeType.getAsString()));
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());
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();
101 public static String toGroupName(String groupeTypeName) {
102 String result = groupeTypeName;
103 for (String variation : GROUP_VARIATIONS) {
104 result = result.replace(variation, "");