Skip to content

Commit 6d9cc49

Browse files
authored
[unifi] Provide LED channel for access point (openhab#17702)
* Provide LED channel for access Signed-off-by: Jacob Laursen <[email protected]>
1 parent f37f39c commit 6d9cc49

File tree

8 files changed

+48
-3
lines changed

8 files changed

+48
-3
lines changed

bundles/org.openhab.binding.unifi/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ The `accessPoint` information that is retrieved is available as these channels:
249249
| uptime | Number:Time | Uptime of the device (in seconds) | Read |
250250
| lastSeen | DateTime | Date and Time the device was last seen | Read |
251251
| experience | Number:Dimensionless | The average health indication of the connected clients | Read |
252+
| led | Switch | Switch the LED on or off | Read, Write |
252253

253254
## Rule Actions
254255

bundles/org.openhab.binding.unifi/src/main/java/org/openhab/binding/unifi/internal/UniFiBindingConstants.java

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public final class UniFiBindingConstants {
100100
// List of access point device channels
101101
public static final String CHANNEL_AP_ENABLE = "enable";
102102
public static final String CHANNEL_AP_STATE = "state";
103+
public static final String CHANNEL_AP_LED = "led";
103104

104105
// List of all Parameters
105106
public static final String PARAMETER_HOST = "host";

bundles/org.openhab.binding.unifi/src/main/java/org/openhab/binding/unifi/internal/api/UniFiController.java

+11
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,17 @@ public void disableAccessPoint(final UniFiDevice device, final boolean disable)
224224
refresh();
225225
}
226226

227+
public void setLedOverride(final UniFiDevice device, final String override) throws UniFiException {
228+
final UniFiControllerRequest<Void> req = newRequest(Void.class, HttpMethod.PUT, gson);
229+
req.setAPIPath(String.format("/api/s/%s/rest/device/%s", device.getSite().getName(), device.getId()));
230+
req.setBodyParameter("_id", device.getId());
231+
if (!override.isEmpty()) {
232+
req.setBodyParameter("led_override", override);
233+
}
234+
executeRequest(req);
235+
refresh();
236+
}
237+
227238
public void generateVouchers(final UniFiSite site, final int count, final int expiration, final int users,
228239
@Nullable Integer upLimit, @Nullable Integer downLimit, @Nullable Integer dataQuota) throws UniFiException {
229240
final UniFiControllerRequest<Void> req = newRequest(Void.class, HttpMethod.POST, gson);

bundles/org.openhab.binding.unifi/src/main/java/org/openhab/binding/unifi/internal/api/dto/UniFiDevice.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public class UniFiDevice implements HasId {
6969

7070
private Boolean disabled;
7171

72+
private String ledOverride;
73+
7274
public UniFiDevice(final UniFiControllerCache cache) {
7375
this.cache = cache;
7476
}
@@ -138,10 +140,14 @@ public Boolean isDisabled() {
138140
return disabled;
139141
}
140142

143+
public String getLedOverride() {
144+
return ledOverride;
145+
}
146+
141147
@Override
142148
public String toString() {
143149
return String.format(
144-
"UniFiDevice{mac: '%s', name: '%s', type: '%s', model: '%s', version: '%s', experience: %d, disabled: %b, uptime: %d, site: %s}",
145-
mac, name, type, model, version, experience, disabled, uptime, getSite());
150+
"UniFiDevice{mac: '%s', name: '%s', type: '%s', model: '%s', version: '%s', experience: %d, disabled: %b, led: %s, uptime: %d, site: %s}",
151+
mac, name, type, model, version, experience, disabled, ledOverride, uptime, getSite());
146152
}
147153
}

bundles/org.openhab.binding.unifi/src/main/java/org/openhab/binding/unifi/internal/handler/UniFiAccessPointThingHandler.java

+13
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ protected State getChannelState(final UniFiDevice device, final String channelId
152152
state = new QuantityType<>(device.getExperience(), Units.PERCENT);
153153
}
154154
break;
155+
case CHANNEL_AP_LED:
156+
String override = device.getLedOverride();
157+
state = "default".equals(override) ? UnDefType.UNDEF : OnOffType.from(override);
158+
break;
155159
}
156160
return state;
157161
}
@@ -171,6 +175,8 @@ protected boolean handleCommand(final UniFiController controller, final UniFiDev
171175

172176
if (CHANNEL_AP_ENABLE.equals(channelID) && command instanceof OnOffType onOffCommand) {
173177
return handleEnableCommand(controller, device, channelUID, onOffCommand);
178+
} else if (CHANNEL_AP_LED.equals(channelID) && command instanceof OnOffType onOffCommand) {
179+
return handleLedCommand(controller, device, channelUID, onOffCommand);
174180
}
175181
return false;
176182
}
@@ -181,4 +187,11 @@ private boolean handleEnableCommand(final UniFiController controller, final UniF
181187
refresh();
182188
return true;
183189
}
190+
191+
private boolean handleLedCommand(final UniFiController controller, final UniFiDevice device,
192+
final ChannelUID channelUID, final OnOffType command) throws UniFiException {
193+
controller.setLedOverride(device, command == OnOffType.ON ? "on" : "off");
194+
refresh();
195+
return true;
196+
}
184197
}

bundles/org.openhab.binding.unifi/src/main/resources/OH-INF/i18n/unifi.properties

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ thing-type.unifi.accessPoint.description = An access point managed by a UniFi co
1010
thing-type.unifi.accessPoint.channel.experience.description = The average experience of the connected clients
1111
thing-type.unifi.accessPoint.channel.ipAddress.description = IP address of the device
1212
thing-type.unifi.accessPoint.channel.lastSeen.description = Timestamp of when the device was last seen
13+
thing-type.unifi.accessPoint.channel.led.label = LED
14+
thing-type.unifi.accessPoint.channel.led.description = Switches the LED on or off
1315
thing-type.unifi.accessPoint.channel.name.description = Name of the device
1416
thing-type.unifi.accessPoint.channel.online.description = Online status of the device
1517
thing-type.unifi.accessPoint.channel.uptime.description = Uptime of the device (in seconds)

bundles/org.openhab.binding.unifi/src/main/resources/OH-INF/thing/thing-types.xml

+5-1
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,15 @@
171171
<channel id="experience" typeId="experience">
172172
<description>The average experience of the connected clients</description>
173173
</channel>
174+
<channel id="led" typeId="system.power">
175+
<label>LED</label>
176+
<description>Switches the LED on or off</description>
177+
</channel>
174178
</channels>
175179

176180
<properties>
177181
<property name="vendor">Ubiquiti Networks</property>
178-
<property name="thingTypeVersion">1</property>
182+
<property name="thingTypeVersion">2</property>
179183
</properties>
180184

181185
<config-description-ref uri="thing-type:unifi:accessPoint"/>

bundles/org.openhab.binding.unifi/src/main/resources/OH-INF/update/thing-updates.xml

+7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@
3636
<description>The average experience of the connected clients</description>
3737
</add-channel>
3838
</instruction-set>
39+
<instruction-set targetVersion="2">
40+
<add-channel id="led">
41+
<type>system:power</type>
42+
<label>LED</label>
43+
<description>Switches the LED on or off</description>
44+
</add-channel>
45+
</instruction-set>
3946
</thing-type>
4047

4148
</update:update-descriptions>

0 commit comments

Comments
 (0)