-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[nanoleaf] Refactored code to use core features and more (#10101)
This is a bigger refactoring bringing these (breaking) changes: - System channel types are used where applicable - Obsolete channels (such as power) were removed - Some channel types were marked "advanced" - "Tap" channels were converted to a trigger channel type providing a "system button" behavior - Layout can now be requested by a console command - Command options for effect channel are dynamically provided - Log level has been reduced where appropriate - HTTP request timeouts were reduced - handleRemoval now returns quickly as expected - Fixed hanging thread / infinite loop when requesting layouts for non-square panels - Various other smaller enhancements and fixes - Documentation has been adapted accordingly Signed-off-by: Kai Kreuzer <kai@openhab.org>
- Loading branch information
1 parent
c582dda
commit 009208a
Showing
15 changed files
with
407 additions
and
403 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...src/main/java/org/openhab/binding/nanoleaf/internal/command/NanoleafCommandExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* Copyright (c) 2010-2021 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.nanoleaf.internal.command; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.openhab.binding.nanoleaf.internal.NanoleafBindingConstants; | ||
import org.openhab.binding.nanoleaf.internal.handler.NanoleafControllerHandler; | ||
import org.openhab.core.io.console.Console; | ||
import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension; | ||
import org.openhab.core.io.console.extensions.ConsoleCommandExtension; | ||
import org.openhab.core.thing.Thing; | ||
import org.openhab.core.thing.ThingRegistry; | ||
import org.openhab.core.thing.ThingUID; | ||
import org.openhab.core.thing.binding.ThingHandler; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
|
||
/** | ||
* Console commands for interacting with Nanoleaf integration | ||
* | ||
* @author Kai Kreuzer - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
@Component(service = ConsoleCommandExtension.class) | ||
public class NanoleafCommandExtension extends AbstractConsoleCommandExtension { | ||
|
||
private static final String CMD_LAYOUT = "layout"; | ||
private final ThingRegistry thingRegistry; | ||
|
||
@Activate | ||
public NanoleafCommandExtension(@Reference ThingRegistry thingRegistry) { | ||
super("nanoleaf", "Interact with the Nanoleaf integration."); | ||
this.thingRegistry = thingRegistry; | ||
} | ||
|
||
@Override | ||
public void execute(String[] args, Console console) { | ||
if (args.length > 0) { | ||
String subCommand = args[0]; | ||
switch (subCommand) { | ||
case CMD_LAYOUT: | ||
if (args.length == 1) { | ||
thingRegistry.getAll().forEach(thing -> { | ||
if (thing.getUID().getBindingId().equals(NanoleafBindingConstants.BINDING_ID)) { | ||
ThingHandler handler = thing.getHandler(); | ||
if (handler instanceof NanoleafControllerHandler) { | ||
NanoleafControllerHandler nanoleafControllerHandler = (NanoleafControllerHandler) handler; | ||
String layout = nanoleafControllerHandler.getLayout(); | ||
console.println("Layout of Nanoleaf controller '" + thing.getUID().getAsString() | ||
+ "' with label '" + thing.getLabel() + "':" + System.lineSeparator()); | ||
console.println(layout); | ||
console.println(System.lineSeparator()); | ||
} | ||
} | ||
}); | ||
} else if (args.length == 2) { | ||
String uid = args[1]; | ||
Thing thing = thingRegistry.get(new ThingUID(uid)); | ||
if (thing != null) { | ||
ThingHandler handler = thing.getHandler(); | ||
if (handler instanceof NanoleafControllerHandler) { | ||
NanoleafControllerHandler nanoleafControllerHandler = (NanoleafControllerHandler) handler; | ||
String layout = nanoleafControllerHandler.getLayout(); | ||
console.println(layout); | ||
} else { | ||
console.println("Thing with UID '" + uid.toString() | ||
+ "' is not an initialized Nanoleaf controller."); | ||
} | ||
} else { | ||
console.println("Thing with UID '" + uid.toString() + "' does not exist."); | ||
} | ||
} else { | ||
printUsage(console); | ||
} | ||
break; | ||
|
||
default: | ||
console.println("Unknown command '" + subCommand + "'"); | ||
printUsage(console); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public List<String> getUsages() { | ||
return Arrays.asList(buildCommandUsage(CMD_LAYOUT + " <thingUID>", "Prints the panel layout on the console.")); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...nhab/binding/nanoleaf/internal/commanddescription/NanoleafCommandDescriptionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Copyright (c) 2010-2021 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.nanoleaf.internal.commanddescription; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.jdt.annotation.NonNull; | ||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.binding.nanoleaf.internal.NanoleafBindingConstants; | ||
import org.openhab.binding.nanoleaf.internal.NanoleafControllerListener; | ||
import org.openhab.binding.nanoleaf.internal.handler.NanoleafControllerHandler; | ||
import org.openhab.binding.nanoleaf.internal.model.ControllerInfo; | ||
import org.openhab.core.thing.ChannelUID; | ||
import org.openhab.core.thing.ThingUID; | ||
import org.openhab.core.thing.binding.BaseDynamicCommandDescriptionProvider; | ||
import org.openhab.core.thing.binding.ThingHandler; | ||
import org.openhab.core.thing.binding.ThingHandlerService; | ||
import org.openhab.core.thing.type.DynamicCommandDescriptionProvider; | ||
import org.openhab.core.types.CommandOption; | ||
import org.osgi.service.component.annotations.Component; | ||
|
||
/** | ||
* This class provides the available effects as dynamic options as they are read from the Nanoleaf controller. | ||
* | ||
* @author Kai Kreuzer - Initial contribution | ||
* | ||
*/ | ||
@NonNullByDefault | ||
@Component(service = { DynamicCommandDescriptionProvider.class }) | ||
public class NanoleafCommandDescriptionProvider extends BaseDynamicCommandDescriptionProvider | ||
implements NanoleafControllerListener, ThingHandlerService { | ||
|
||
private @Nullable ChannelUID effectChannelUID; | ||
|
||
private @Nullable NanoleafControllerHandler bridgeHandler; | ||
|
||
@Override | ||
public void setThingHandler(ThingHandler handler) { | ||
this.bridgeHandler = (NanoleafControllerHandler) handler; | ||
bridgeHandler.registerControllerListener(this); | ||
effectChannelUID = new ChannelUID(handler.getThing().getUID(), NanoleafBindingConstants.CHANNEL_EFFECT); | ||
} | ||
|
||
@Override | ||
public @Nullable ThingHandler getThingHandler() { | ||
return bridgeHandler; | ||
} | ||
|
||
@Override | ||
public void deactivate() { | ||
if (bridgeHandler != null) { | ||
bridgeHandler.unregisterControllerListener(this); | ||
} | ||
super.deactivate(); | ||
} | ||
|
||
@Override | ||
public void onControllerInfoFetched(@NonNull ThingUID bridge, @NonNull ControllerInfo controllerInfo) { | ||
List<@NonNull String> effects = controllerInfo.getEffects().getEffectsList(); | ||
ChannelUID uid = effectChannelUID; | ||
if (effects != null && uid != null && uid.getThingUID().equals(bridge)) { | ||
List<@NonNull CommandOption> commandOptions = effects.stream() // | ||
.map(effect -> new CommandOption(effect, effect)) // | ||
.collect(Collectors.toList()); | ||
setCommandOptions(uid, commandOptions); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.