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.nanoleaf.internal.commanddescription;
15 import java.util.List;
16 import java.util.stream.Collectors;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.nanoleaf.internal.NanoleafBindingConstants;
21 import org.openhab.binding.nanoleaf.internal.NanoleafControllerListener;
22 import org.openhab.binding.nanoleaf.internal.handler.NanoleafControllerHandler;
23 import org.openhab.binding.nanoleaf.internal.model.ControllerInfo;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.ThingUID;
26 import org.openhab.core.thing.binding.BaseDynamicCommandDescriptionProvider;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.openhab.core.thing.binding.ThingHandlerService;
29 import org.openhab.core.thing.type.DynamicCommandDescriptionProvider;
30 import org.openhab.core.types.CommandOption;
31 import org.osgi.service.component.annotations.Component;
34 * This class provides the available effects as dynamic options as they are read from the Nanoleaf controller.
36 * @author Kai Kreuzer - Initial contribution
40 @Component(service = { DynamicCommandDescriptionProvider.class })
41 public class NanoleafCommandDescriptionProvider extends BaseDynamicCommandDescriptionProvider
42 implements NanoleafControllerListener, ThingHandlerService {
44 private @Nullable ChannelUID effectChannelUID;
46 private @Nullable NanoleafControllerHandler bridgeHandler;
49 public void setThingHandler(ThingHandler handler) {
50 this.bridgeHandler = (NanoleafControllerHandler) handler;
51 NanoleafControllerHandler localHandler = this.bridgeHandler;
52 if (localHandler != null) {
53 localHandler.registerControllerListener(this);
56 effectChannelUID = new ChannelUID(handler.getThing().getUID(), NanoleafBindingConstants.CHANNEL_EFFECT);
60 public @Nullable ThingHandler getThingHandler() {
65 public void deactivate() {
66 NanoleafControllerHandler localHandler = this.bridgeHandler;
67 if (localHandler != null) {
68 localHandler.unregisterControllerListener(this);
74 public void onControllerInfoFetched(ThingUID bridge, ControllerInfo controllerInfo) {
75 List<String> effects = controllerInfo.getEffects().getEffectsList();
76 ChannelUID uid = effectChannelUID;
77 if (effects != null && uid != null && uid.getThingUID().equals(bridge)) {
78 List<CommandOption> commandOptions = effects.stream() //
79 .map(effect -> new CommandOption(effect, effect)) //
80 .collect(Collectors.toList());
81 setCommandOptions(uid, commandOptions);