]> git.basschouten.com Git - openhab-addons.git/blob
20ec3c11b20b3b7729925ae0d1d3e44f84b1a280
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.pushsafer.internal.config;
14
15 import static org.openhab.binding.pushsafer.internal.PushsaferBindingConstants.*;
16
17 import java.net.URI;
18 import java.util.Collection;
19 import java.util.Comparator;
20 import java.util.List;
21 import java.util.Locale;
22 import java.util.stream.Collectors;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.pushsafer.internal.dto.Icon;
27 import org.openhab.binding.pushsafer.internal.dto.Sound;
28 import org.openhab.binding.pushsafer.internal.handler.PushsaferAccountHandler;
29 import org.openhab.core.config.core.ConfigOptionProvider;
30 import org.openhab.core.config.core.ParameterOption;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerService;
33 import org.osgi.service.component.annotations.Component;
34
35 /**
36  * The {@link PushsaferConfigOptionProvider} class contains fields mapping thing configuration parameters.
37  *
38  * @author Kevin Siml - Initial contribution, forked from Christoph Weitkamp
39  */
40 @Component(service = ConfigOptionProvider.class)
41 @NonNullByDefault
42 public class PushsaferConfigOptionProvider implements ConfigOptionProvider, ThingHandlerService {
43
44     private @Nullable PushsaferAccountHandler accountHandler;
45
46     @Override
47     public @Nullable Collection<ParameterOption> getParameterOptions(URI uri, String param, @Nullable String context,
48             @Nullable Locale locale) {
49         PushsaferAccountHandler localAccountHandler = accountHandler;
50         if (localAccountHandler != null) {
51             if (PUSHSAFER_ACCOUNT.getAsString().equals(uri.getSchemeSpecificPart()) && CONFIG_SOUND.equals(param)) {
52                 List<Sound> sounds = localAccountHandler.getSounds();
53                 if (!sounds.isEmpty()) {
54                     return sounds.stream().map(Sound::getAsParameterOption)
55                             .sorted(Comparator.comparing(ParameterOption::getLabel))
56                             .collect(Collectors.toUnmodifiableList());
57                 }
58             }
59             if (PUSHSAFER_ACCOUNT.getAsString().equals(uri.getSchemeSpecificPart()) && CONFIG_ICON.equals(param)) {
60                 List<Icon> icons = localAccountHandler.getIcons();
61                 if (!icons.isEmpty()) {
62                     return icons.stream().map(Icon::getAsParameterOption)
63                             .sorted(Comparator.comparing(ParameterOption::getLabel))
64                             .collect(Collectors.toUnmodifiableList());
65                 }
66             }
67         }
68         return null;
69     }
70
71     @Override
72     public void setThingHandler(@Nullable ThingHandler handler) {
73         this.accountHandler = (PushsaferAccountHandler) handler;
74     }
75
76     @Override
77     public @Nullable ThingHandler getThingHandler() {
78         return accountHandler;
79     }
80 }