Skip to content
This repository has been archived by the owner on Nov 27, 2020. It is now read-only.

Commit

Permalink
Add save preset action, and new white sat threshold feature.
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Skinner <[email protected]>
  • Loading branch information
Skinah committed Oct 1, 2020
1 parent af610f7 commit aeb8783
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 20 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# WLed Binding
# WLED Binding

This openHAB binding allows you to auto discover and use LED strings from the WLED project found here:
<https://github.com/Aircoookie/WLED>

To watch what the binding does, enter this in to the openHAB console log:set TRACE org.openhab.binding.wled
## Fault Finding

To watch what the binding does you can enter this in to the openHAB console, `log:set TRACE org.openhab.binding.wled` which will allow you to test the same commands in a web browser to determine if it is a bug in the binding, or in the firmware for WLED.

## Supported Things

Expand All @@ -23,6 +25,7 @@ The full example section gives everything needed to quickly setup using textual
|-|-|
| `address`| The URL to your WLED device. Example is `http://192.168.0.2:80` |
| `pollTime`| How often you want the states of the LED fetched in case you make changes with a non openHAB app or web browser or the light is auto changing FX. |
| `saturationThreshold` | Allows you to use a colorpicker linked to the masterControls channel to trigger the pure white LEDs when your using RGBW strings. Try setting the value to 12 or for RGB strings leave this on 0. TIP: set to 33 if you want Google devices to select the real white LEDS when you ask for white. |

## Channels

Expand Down
62 changes: 62 additions & 0 deletions src/main/java/org/openhab/binding/wled/internal/WLedActions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright (c) 2010-2020 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.wled.internal;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.thing.binding.ThingActions;
import org.eclipse.smarthome.core.thing.binding.ThingActionsScope;
import org.eclipse.smarthome.core.thing.binding.ThingHandler;
import org.openhab.core.automation.annotation.ActionInput;
import org.openhab.core.automation.annotation.RuleAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The {@link WLedActions} is responsible for Actions.
*
* @author Matthew Skinner - Initial contribution
*/

@ThingActionsScope(name = "wled")
@NonNullByDefault
public class WLedActions implements ThingActions {
public final Logger logger = LoggerFactory.getLogger(getClass());
private @Nullable WLedHandler handler;

@Override
public void setThingHandler(@Nullable ThingHandler handler) {
this.handler = (WLedHandler) handler;
}

@Override
public @Nullable ThingHandler getThingHandler() {
return handler;
}

@RuleAction(label = "Save Preset", description = "Save a WLED state to a preset location")
public void savePreset(
@ActionInput(name = "presetNumber", label = "Save Preset Number", description = "Enter the number of the preset you wish to save") int presetNumber) {
if (presetNumber > 0 && handler != null) {
handler.savePreset(presetNumber);
}
}

public static void savePreset(@Nullable ThingActions actions, int presetNumber) {
if (actions instanceof WLedActions) {
((WLedActions) actions).savePreset(presetNumber);
} else {
throw new IllegalArgumentException("Instance is not a WLED class.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class WLedBindingConstants {
// Configs
public static final String CONFIG_ADDRESS = "address";
public static final String CONFIG_POLL_TIME = "pollTime";
public static final String CONFIG_SAT_THRESHOLD = "saturationThreshold";

// Channels
public static final String CHANNEL_MASTER_CONTROLS = "masterControls";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
public class WLedConfiguration {
public String address = "";
public int pollTime;
public int saturationThreshold;
}
20 changes: 18 additions & 2 deletions src/main/java/org/openhab/binding/wled/internal/WLedHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
Expand All @@ -42,6 +44,7 @@
import org.eclipse.smarthome.core.thing.ThingStatus;
import org.eclipse.smarthome.core.thing.ThingStatusDetail;
import org.eclipse.smarthome.core.thing.binding.BaseThingHandler;
import org.eclipse.smarthome.core.thing.binding.ThingHandlerService;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.RefreshType;
import org.eclipse.smarthome.core.types.StateOption;
Expand Down Expand Up @@ -277,8 +280,12 @@ public void handleCommand(ChannelUID channelUID, Command command) {
masterBrightness = new BigDecimal((((HSBType) command).getBrightness()).toString())
.multiply(new BigDecimal(2.55));
primaryColor = new HSBType(command.toString());
sendGetRequest(
"/win&TT=1000&FX=0&CY=0&CL=" + createColorHex(primaryColor) + "&A=" + masterBrightness);
if (primaryColor.getSaturation().intValue() < config.saturationThreshold) {
sendGetRequest("/win&TT=1000&FX=0&CY=0&CL=h000000&W=255" + "&A=" + masterBrightness);
} else {
sendGetRequest(
"/win&TT=1000&FX=0&CY=0&CL=" + createColorHex(primaryColor) + "&A=" + masterBrightness);
}
} else {// should only be PercentType left
masterBrightness = new BigDecimal(command.toString()).multiply(new BigDecimal(2.55));
sendGetRequest("/win&TT=2000&A=" + masterBrightness);
Expand Down Expand Up @@ -386,6 +393,10 @@ public void handleCommand(ChannelUID channelUID, Command command) {
*
*/
public void savePreset(int presetIndex) {
if (presetIndex > 16) {
logger.warn("Presets above 16 do not exist, and the action sent {}", presetIndex);
return;
}
sendGetRequest("/win&PS=" + presetIndex);
}

Expand All @@ -406,6 +417,11 @@ public void dispose() {
}
}

@Override
public Collection<Class<? extends ThingHandlerService>> getServices() {
return Collections.singleton(WLedActions.class);
}

/**
* @return A string that starts after finding the element and terminates when it finds the first occurence of the
* end char after the element.
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/ESH-INF/binding/binding.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
xmlns:binding="https://openhab.org/schemas/binding/v1.0.0"
xsi:schemaLocation="https://openhab.org/schemas/binding/v1.0.0 https://openhab.org/schemas/binding-1.0.0.xsd">

<name>WLed Binding</name>
<description>This is the binding for WLed.</description>
<name>WLED Binding</name>
<description>This is the binding for WLED</description>
<author>Matthew Skinner</author>

</binding:binding>
8 changes: 7 additions & 1 deletion src/main/resources/ESH-INF/thing/thing-types.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xsi:schemaLocation="https://openhab.org/schemas/thing-description/v1.0.0 https://openhab.org/schemas/thing-description-1.0.0.xsd">

<thing-type id="wled">
<label>WLed String</label>
<label>WLED String</label>
<description>A WLED string of LEDs</description>
<category>Lightbulb</category>
<channels>
Expand Down Expand Up @@ -35,6 +35,12 @@
<description>Time in seconds of how often to fetch the state of the LEDs.</description>
<default>10</default>
</parameter>
<parameter name="saturationThreshold" type="integer" required="true" min="0" max="99">
<label>Saturation Threshold</label>
<description>This feature allows you to specify a number that if the saturation drops below, will trigger white.
</description>
<default>0</default>
</parameter>
</config-description>
</thing-type>

Expand Down
4 changes: 2 additions & 2 deletions target/classes/ESH-INF/binding/binding.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
xmlns:binding="https://openhab.org/schemas/binding/v1.0.0"
xsi:schemaLocation="https://openhab.org/schemas/binding/v1.0.0 https://openhab.org/schemas/binding-1.0.0.xsd">

<name>WLed Binding</name>
<description>This is the binding for WLed.</description>
<name>WLED Binding</name>
<description>This is the binding for WLED</description>
<author>Matthew Skinner</author>

</binding:binding>
8 changes: 7 additions & 1 deletion target/classes/ESH-INF/thing/thing-types.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xsi:schemaLocation="https://openhab.org/schemas/thing-description/v1.0.0 https://openhab.org/schemas/thing-description-1.0.0.xsd">

<thing-type id="wled">
<label>WLed String</label>
<label>WLED String</label>
<description>A WLED string of LEDs</description>
<category>Lightbulb</category>
<channels>
Expand Down Expand Up @@ -35,6 +35,12 @@
<description>Time in seconds of how often to fetch the state of the LEDs.</description>
<default>10</default>
</parameter>
<parameter name="saturationThreshold" type="integer" required="true" min="0" max="99">
<label>Saturation Threshold</label>
<description>This feature allows you to specify a number that if the saturation drops below, will trigger white.
</description>
<default>0</default>
</parameter>
</config-description>
</thing-type>

Expand Down
21 changes: 11 additions & 10 deletions target/classes/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Manifest-Version: 1.0
Automatic-Module-Name: org.openhab.binding.wled
Bnd-LastModified: 1601294873574
Bnd-LastModified: 1601535602378
Bundle-Description: This project contains the official add-ons of open
HAB
Bundle-Developers: openhab;email="[email protected]";name=openHAB;organ
Expand All @@ -20,16 +20,17 @@ Bundle-SCM: url="https://github.com/openhab/openhab-addons/org.openhab
tor.bundles/org.openhab.binding.wled",tag=HEAD
Bundle-SymbolicName: org.openhab.binding.wled
Bundle-Vendor: openHAB.org
Bundle-Version: 2.5.9.202009281207
Bundle-Version: 2.5.9.202010010700
Created-By: 1.8.0_231 (Oracle Corporation)
Import-Package: org.eclipse.smarthome.config.discovery,org.eclipse.sma
rthome.config.discovery.mdns,org.eclipse.smarthome.core.library.types
,org.eclipse.smarthome.core.thing,org.eclipse.smarthome.core.thing.bi
nding,org.eclipse.smarthome.core.thing.i18n,org.eclipse.smarthome.cor
e.thing.type,org.eclipse.smarthome.core.types,org.eclipse.smarthome.i
o.net.http,javax.jmdns;version="[3.5,4)",org.eclipse.jetty.client;ver
sion="[9.3,10)",org.eclipse.jetty.client.api;version="[9.3,10)",org.e
clipse.jetty.http;version="[9.3,10)",org.slf4j;version="[1.7,2)"
Import-Package: org.openhab.core.automation.annotation;resolution:=opt
ional,org.eclipse.smarthome.config.discovery,org.eclipse.smarthome.co
nfig.discovery.mdns,org.eclipse.smarthome.core.library.types,org.ecli
pse.smarthome.core.thing,org.eclipse.smarthome.core.thing.binding,org
.eclipse.smarthome.core.thing.i18n,org.eclipse.smarthome.core.thing.t
ype,org.eclipse.smarthome.core.types,org.eclipse.smarthome.io.net.htt
p,javax.jmdns;version="[3.5,4)",org.eclipse.jetty.client;version="[9.
3,10)",org.eclipse.jetty.client.api;version="[9.3,10)",org.eclipse.je
tty.http;version="[9.3,10)",org.slf4j;version="[1.7,2)"
Private-Package: ESH-INF.binding,ESH-INF.thing,org.openhab.binding.wle
d.internal
Provide-Capability: osgi.service;objectClass:List<String>="org.eclipse
Expand Down
Binary file not shown.
Binary file not shown.
Binary file modified target/classes/org/openhab/binding/wled/internal/WLedHandler.class
Binary file not shown.

0 comments on commit aeb8783

Please sign in to comment.