[unifi] Add some standard channels to access points (#17530)

Signed-off-by: Thomas Lauterbach <thomas_lauterbach@arcor.de>
This commit is contained in:
Thomas Lauterbach 2024-10-14 22:41:00 +02:00 committed by GitHub
parent eefa6b04ae
commit 11f5cb3299
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 233 additions and 17 deletions

View File

@ -239,9 +239,16 @@ The default mode value is `auto`.
The `accessPoint` information that is retrieved is available as these channels:
| Channel ID | Item Type | Description | Permissions |
|------------|-----------|------------------------------------|-------------|
| enable | Switch | Enable or disable the access point | Read, Write |
| Channel ID | Item Type | Description | Permissions |
|------------|----------------------|----------------------------------------------------------------------|-------------|
| online | Switch | Online status of the device | Read |
| enable | Switch | Enable or disable the access point | Read, Write |
| name | String | Name of device (from the controller web UI) | Read |
| site | String | Site name (from the controller web UI) the device is associated with | Read |
| ipAddress | String | IP address of the device | Read |
| uptime | Number:Time | Uptime of the device (in seconds) | Read |
| lastSeen | DateTime | Date and Time the device was last seen | Read |
| experience | Number:Dimensionless | The average health indication of the connected clients | Read |
## Rule Actions

View File

@ -61,21 +61,23 @@ public final class UniFiBindingConstants {
public static final String CHANNEL_PASSPHRASE = "passphrase";
public static final String CHANNEL_QRCODE_ENCODING = "qrcodeEncoding";
// List of common wired + wireless client channels
// List of common wired + wireless client + device channels
public static final String CHANNEL_ONLINE = "online";
public static final String CHANNEL_NAME = "name";
public static final String CHANNEL_HOSTNAME = "hostname";
public static final String CHANNEL_SITE = "site";
public static final String CHANNEL_MAC_ADDRESS = "macAddress";
public static final String CHANNEL_IP_ADDRESS = "ipAddress";
public static final String CHANNEL_UPTIME = "uptime";
public static final String CHANNEL_LAST_SEEN = "lastSeen";
public static final String CHANNEL_EXPERIENCE = "experience";
// List of common wired + wireless client channels
public static final String CHANNEL_HOSTNAME = "hostname";
public static final String CHANNEL_MAC_ADDRESS = "macAddress";
public static final String CHANNEL_GUEST = "guest";
public static final String CHANNEL_BLOCKED = "blocked";
public static final String CHANNEL_RECONNECT = "reconnect";
public static final String CHANNEL_CMD = "cmd";
public static final String CHANNEL_CMD_RECONNECT = "reconnect";
public static final String CHANNEL_EXPERIENCE = "experience";
// List of additional wireless client channels
public static final String CHANNEL_AP = "ap";
@ -95,8 +97,9 @@ public final class UniFiBindingConstants {
public static final String CHANNEL_PORT_POE_VOLTAGE = "voltage";
public static final String CHANNEL_PORT_POE_CURRENT = "current";
// List of access point channels
// List of access point device channels
public static final String CHANNEL_AP_ENABLE = "enable";
public static final String CHANNEL_AP_STATE = "state";
// List of all Parameters
public static final String PARAMETER_HOST = "host";

View File

@ -12,8 +12,11 @@
*/
package org.openhab.binding.unifi.internal.api.dto;
import java.time.Instant;
import org.openhab.binding.unifi.internal.api.cache.UniFiControllerCache;
import org.openhab.binding.unifi.internal.api.util.UniFiTidyLowerCaseStringDeserializer;
import org.openhab.binding.unifi.internal.api.util.UniFiTimestampDeserializer;
import com.google.gson.JsonObject;
import com.google.gson.annotations.JsonAdapter;
@ -36,6 +39,8 @@ public class UniFiDevice implements HasId {
@JsonAdapter(UniFiTidyLowerCaseStringDeserializer.class)
private String mac;
private String ip;
private String model;
private String version;
@ -46,13 +51,23 @@ public class UniFiDevice implements HasId {
private String name;
private Integer state;
private Integer uptime;
@JsonAdapter(UniFiTimestampDeserializer.class)
private Instant lastSeen;
private String siteId;
@SerializedName("satisfaction")
private Integer experience;
private UniFiPortTable[] portTable;
private JsonObject[] portOverrides;
private boolean disabled;
private Boolean disabled;
public UniFiDevice(final UniFiControllerCache cache) {
this.cache = cache;
@ -79,14 +94,34 @@ public class UniFiDevice implements HasId {
return serial;
}
public Integer getExperience() {
return experience;
}
public String getName() {
return name == null || name.isBlank() ? mac : name;
}
public Integer getState() {
return state;
}
public Integer getUptime() {
return uptime;
}
public Instant getLastSeen() {
return lastSeen;
}
public String getMac() {
return mac;
}
public String getIp() {
return ip;
}
public UniFiSite getSite() {
return cache.getSite(siteId);
}
@ -99,13 +134,14 @@ public class UniFiDevice implements HasId {
return portOverrides;
}
public boolean isDisabled() {
public Boolean isDisabled() {
return disabled;
}
@Override
public String toString() {
return String.format("UniFiDevice{mac: '%s', name: '%s', type: %s, model: '%s', disabled: %b, site: %s}", mac,
name, type, model, disabled, getSite());
return String.format(
"UniFiDevice{mac: '%s', name: '%s', type: '%s', model: '%s', version: '%s', experience: %d, disabled: %b, uptime: %d, site: %s}",
mac, name, type, model, version, experience, disabled, uptime, getSite());
}
}

View File

@ -12,9 +12,10 @@
*/
package org.openhab.binding.unifi.internal.handler;
import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_AP_ENABLE;
import static org.openhab.binding.unifi.internal.UniFiBindingConstants.DEVICE_TYPE_UAP;
import static org.openhab.binding.unifi.internal.UniFiBindingConstants.*;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
@ -25,13 +26,18 @@ import org.openhab.binding.unifi.internal.api.UniFiException;
import org.openhab.binding.unifi.internal.api.cache.UniFiControllerCache;
import org.openhab.binding.unifi.internal.api.dto.UniFiDevice;
import org.openhab.binding.unifi.internal.api.dto.UniFiSite;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.unit.Units;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;
/**
* An access point managed by the UniFi controller software.
@ -47,10 +53,10 @@ public class UniFiAccessPointThingHandler extends UniFiBaseThingHandler<UniFiDev
super(thing);
}
private static boolean belongsToSite(final UniFiDevice client, final String siteName) {
private static boolean belongsToSite(final UniFiDevice device, final String siteName) {
boolean result = true;
if (!siteName.isEmpty()) {
final UniFiSite site = client.getSite();
final UniFiSite site = device.getSite();
if (site == null || !site.matchesName(siteName)) {
result = false;
}
@ -78,13 +84,73 @@ public class UniFiAccessPointThingHandler extends UniFiBaseThingHandler<UniFiDev
return device;
}
@Override
protected State getDefaultState(final String channelID) {
final State state;
switch (channelID) {
case CHANNEL_UPTIME:
state = new QuantityType<>(0, Units.SECOND);
break;
case CHANNEL_EXPERIENCE:
state = new QuantityType<>(0, Units.PERCENT);
break;
default:
state = UnDefType.NULL;
break;
}
return state;
}
@Override
protected State getChannelState(final UniFiDevice device, final String channelId) {
final UniFiSite site = device.getSite();
State state = getDefaultState(channelId);
switch (channelId) {
case CHANNEL_AP_ENABLE:
state = OnOffType.from(!device.isDisabled());
if (device.isDisabled() != null) {
state = OnOffType.from(!device.isDisabled());
}
break;
case CHANNEL_ONLINE:
if (device.getState() != null) {
state = OnOffType.from(device.getState() == 1);
}
break;
case CHANNEL_AP_STATE:
if (device.getState() != null) {
state = StringType.valueOf(device.getState().toString());
}
break;
case CHANNEL_NAME:
if (device.getName() != null) {
state = StringType.valueOf(device.getName());
}
break;
case CHANNEL_SITE:
if (site != null && site.getDescription() != null && !site.getDescription().isBlank()) {
state = StringType.valueOf(site.getDescription());
}
break;
case CHANNEL_IP_ADDRESS:
if (device.getIp() != null && !device.getIp().isBlank()) {
state = StringType.valueOf(device.getIp());
}
break;
case CHANNEL_UPTIME:
if (device.getUptime() != null) {
state = new QuantityType<>(device.getUptime(), Units.SECOND);
}
break;
case CHANNEL_LAST_SEEN:
if (device.getLastSeen() != null) {
state = new DateTimeType(ZonedDateTime.ofInstant(device.getLastSeen(), ZoneId.systemDefault()));
}
break;
case CHANNEL_EXPERIENCE:
if (device.getExperience() != null) {
state = new QuantityType<>(device.getExperience(), Units.PERCENT);
}
break;
}
return state;

View File

@ -7,6 +7,12 @@ addon.unifi.description = The UniFi binding integrates the UniFi controller from
thing-type.unifi.accessPoint.label = UniFi Access Point
thing-type.unifi.accessPoint.description = An access point managed by a UniFi controller
thing-type.unifi.accessPoint.channel.experience.description = The average experience of the connected clients
thing-type.unifi.accessPoint.channel.ipAddress.description = IP address of the device
thing-type.unifi.accessPoint.channel.lastSeen.description = Timestamp of when the device was last seen
thing-type.unifi.accessPoint.channel.name.description = Name of the device
thing-type.unifi.accessPoint.channel.online.description = Online status of the device
thing-type.unifi.accessPoint.channel.uptime.description = Uptime of the device (in seconds)
thing-type.unifi.controller.label = UniFi Controller
thing-type.unifi.controller.description = A UniFi controller
thing-type.unifi.poePort.label = UniFi PoE Port
@ -61,6 +67,18 @@ channel-type.unifi.apEnable.label = Enabled
channel-type.unifi.apEnable.description = If the access point is enabled
channel-type.unifi.blocked.label = Blocked
channel-type.unifi.blocked.description = Is device blocked
channel-type.unifi.devState.label = Device State
channel-type.unifi.devState.description = The state of the device
channel-type.unifi.devState.state.option.0 = Offline
channel-type.unifi.devState.state.option.1 = Connected
channel-type.unifi.devState.state.option.2 = Pending Adoption
channel-type.unifi.devState.state.option.4 = Updating
channel-type.unifi.devState.state.option.5 = Provisioning
channel-type.unifi.devState.state.option.6 = Unreachable
channel-type.unifi.devState.state.option.7 = Adopting
channel-type.unifi.devState.state.option.9 = Adoption Error
channel-type.unifi.devState.state.option.10 = Adoption Failed
channel-type.unifi.devState.state.option.11 = Isolated
channel-type.unifi.essid.label = Wireless Network
channel-type.unifi.essid.description = Wireless Network (ESSID) the wireless client is connected to
channel-type.unifi.experience.label = Experience

View File

@ -150,9 +150,34 @@
<description>An access point managed by a UniFi controller</description>
<channels>
<channel id="online" typeId="online">
<description>Online status of the device</description>
</channel>
<channel id="enable" typeId="apEnable"/>
<channel id="state" typeId="devState"/>
<channel id="name" typeId="name">
<description>Name of the device</description>
</channel>
<channel id="site" typeId="site"/>
<channel id="ipAddress" typeId="ipAddress">
<description>IP address of the device</description>
</channel>
<channel id="uptime" typeId="uptime">
<description>Uptime of the device (in seconds)</description>
</channel>
<channel id="lastSeen" typeId="lastSeen">
<description>Timestamp of when the device was last seen</description>
</channel>
<channel id="experience" typeId="experience">
<description>The average experience of the connected clients</description>
</channel>
</channels>
<properties>
<property name="vendor">Ubiquiti Networks</property>
<property name="thingTypeVersion">1</property>
</properties>
<config-description-ref uri="thing-type:unifi:accessPoint"/>
</thing-type>
@ -442,4 +467,24 @@
<description>If the access point is enabled</description>
</channel-type>
<channel-type id="devState">
<item-type>String</item-type>
<label>Device State</label>
<description>The state of the device</description>
<state readOnly="true">
<options>
<option value="0">Offline</option>
<option value="1">Connected</option>
<option value="2">Pending Adoption</option>
<option value="4">Updating</option>
<option value="5">Provisioning</option>
<option value="6">Unreachable</option>
<option value="7">Adopting</option>
<option value="9">Adoption Error</option>
<option value="10">Adoption Failed</option>
<option value="11">Isolated</option>
</options>
</state>
</channel-type>
</thing:thing-descriptions>

View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<update:update-descriptions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:update="https://openhab.org/schemas/update-description/v1.0.0"
xsi:schemaLocation="https://openhab.org/schemas/update-description/v1.0.0 https://openhab.org/schemas/update-description-1.0.0.xsd">
<thing-type uid="unifi:accessPoint">
<instruction-set targetVersion="1">
<add-channel id="online">
<type>unifi:online</type>
<description>Online status of the device</description>
</add-channel>
<add-channel id="state">
<type>unifi:devState</type>
</add-channel>
<add-channel id="name">
<type>unifi:name</type>
<description>Name of the device</description>
</add-channel>
<add-channel id="site">
<type>unifi:site</type>
</add-channel>
<add-channel id="ipAddress">
<type>unifi:ipAddress</type>
<description>IP address of the device</description>
</add-channel>
<add-channel id="uptime">
<type>unifi:uptime</type>
<description>Uptime of the device (in seconds)</description>
</add-channel>
<add-channel id="lastSeen">
<type>unifi:lastSeen</type>
<description>Timestamp of when the device was last seen</description>
</add-channel>
<add-channel id="experience">
<type>unifi:experience</type>
<description>The average experience of the connected clients</description>
</add-channel>
</instruction-set>
</thing-type>
</update:update-descriptions>