2 * Copyright (c) 2010-2022 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.unifi.internal.handler;
15 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_ENABLE_PARAMETER_MODE;
16 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_ENABLE_PARAMETER_MODE_AUTO;
17 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_ENABLE_PARAMETER_MODE_OFF;
18 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_ONLINE;
19 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_PORT_POE_CMD;
20 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_PORT_POE_CMD_POWER_CYCLE;
21 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_PORT_POE_CURRENT;
22 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_PORT_POE_ENABLE;
23 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_PORT_POE_MODE;
24 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_PORT_POE_POWER;
25 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_PORT_POE_VOLTAGE;
26 import static org.openhab.core.library.unit.MetricPrefix.MILLI;
28 import java.util.List;
30 import java.util.Objects;
31 import java.util.stream.Collectors;
33 import javax.measure.quantity.ElectricCurrent;
34 import javax.measure.quantity.ElectricPotential;
35 import javax.measure.quantity.Power;
37 import org.eclipse.jdt.annotation.NonNullByDefault;
38 import org.eclipse.jdt.annotation.Nullable;
39 import org.openhab.binding.unifi.internal.UniFiPoePortThingConfig;
40 import org.openhab.binding.unifi.internal.api.UniFiController;
41 import org.openhab.binding.unifi.internal.api.UniFiException;
42 import org.openhab.binding.unifi.internal.api.cache.UniFiControllerCache;
43 import org.openhab.binding.unifi.internal.api.dto.UnfiPortOverrideJsonElement;
44 import org.openhab.binding.unifi.internal.api.dto.UniFiDevice;
45 import org.openhab.binding.unifi.internal.api.dto.UniFiPortTable;
46 import org.openhab.binding.unifi.internal.api.dto.UniFiPortTuple;
47 import org.openhab.core.library.types.OnOffType;
48 import org.openhab.core.library.types.QuantityType;
49 import org.openhab.core.library.types.StringType;
50 import org.openhab.core.library.unit.Units;
51 import org.openhab.core.thing.ChannelUID;
52 import org.openhab.core.thing.Thing;
53 import org.openhab.core.thing.ThingStatus;
54 import org.openhab.core.thing.ThingStatusDetail;
55 import org.openhab.core.types.Command;
56 import org.openhab.core.types.State;
57 import org.openhab.core.types.UnDefType;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
62 * A Power Over Ethernet (PoE) port on a UniFi switch.
64 * @author Hilbrand Bouwkamp - Initial contribution
67 public class UniFiPoePortThingHandler
68 extends UniFiBaseThingHandler<Map<Integer, UniFiPortTuple>, UniFiPoePortThingConfig> {
70 private final Logger logger = LoggerFactory.getLogger(UniFiPoePortThingHandler.class);
72 private UniFiPoePortThingConfig config = new UniFiPoePortThingConfig();
73 private String poeEnableMode = "";
75 public UniFiPoePortThingHandler(final Thing thing) {
80 protected boolean initialize(final UniFiPoePortThingConfig config) {
82 if (!config.isValid()) {
83 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
84 "@text/error.thing.poe.offline.configuration_error");
87 final String channelConfigPoeEnableMode = (String) getThing().getChannel(CHANNEL_PORT_POE_ENABLE)
88 .getConfiguration().get(CHANNEL_ENABLE_PARAMETER_MODE);
89 poeEnableMode = channelConfigPoeEnableMode.isBlank() ? CHANNEL_ENABLE_PARAMETER_MODE_AUTO
90 : channelConfigPoeEnableMode;
95 protected @Nullable Map<Integer, UniFiPortTuple> getEntity(final UniFiControllerCache cache) {
96 return cache.getSwitchPorts(config.getMacAddress());
100 protected State getChannelState(final Map<Integer, UniFiPortTuple> ports, final String channelId) {
101 final UniFiPortTuple portTuple = getPort(ports);
103 if (portTuple == null) {
104 return setOfflineOnNoPoEPortData();
106 final UniFiPortTable port = portTuple.getTable();
109 return setOfflineOnNoPoEPortData();
115 state = OnOffType.from(port.isUp());
117 case CHANNEL_PORT_POE_ENABLE:
118 state = OnOffType.from(port.isPoeEnabled());
120 case CHANNEL_PORT_POE_MODE:
121 state = StringType.valueOf(port.getPoeMode());
123 case CHANNEL_PORT_POE_POWER:
124 state = new QuantityType<Power>(Double.valueOf(port.getPoePower()), Units.WATT);
126 case CHANNEL_PORT_POE_VOLTAGE:
127 state = new QuantityType<ElectricPotential>(Double.valueOf(port.getPoeVoltage()), Units.VOLT);
129 case CHANNEL_PORT_POE_CURRENT:
130 state = new QuantityType<ElectricCurrent>(Double.valueOf(port.getPoeCurrent()), MILLI(Units.AMPERE));
133 state = UnDefType.UNDEF;
138 private State setOfflineOnNoPoEPortData() {
139 if (getThing().getStatus() != ThingStatus.OFFLINE) {
140 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
141 "@text/error.thing.poe.offline.nodata_error");
143 return UnDefType.NULL;
146 private @Nullable UniFiPortTuple getPort(final Map<Integer, UniFiPortTuple> ports) {
147 return ports.get(config.getPortNumber());
151 protected boolean handleCommand(final UniFiController controller, final Map<Integer, UniFiPortTuple> ports,
152 final ChannelUID channelUID, final Command command) throws UniFiException {
153 final String channelID = channelUID.getIdWithoutGroup();
156 case CHANNEL_PORT_POE_ENABLE:
157 if (command instanceof OnOffType) {
158 return handleModeCommand(controller, ports, getPort(ports),
159 OnOffType.ON == command ? poeEnableMode : CHANNEL_ENABLE_PARAMETER_MODE_OFF);
162 case CHANNEL_PORT_POE_MODE:
163 if (command instanceof StringType) {
164 return handleModeCommand(controller, ports, getPort(ports), command.toFullString());
167 case CHANNEL_PORT_POE_CMD:
168 if (command instanceof StringType) {
169 return handleCmd(controller, getPort(ports), command.toFullString());
177 private boolean handleModeCommand(final UniFiController controller, final Map<Integer, UniFiPortTuple> ports,
178 final @Nullable UniFiPortTuple uniFiPortTuple, final String poeMode) throws UniFiException {
179 final UniFiDevice device = controller.getCache().getDevice(config.getMacAddress());
181 if (device == null || uniFiPortTuple == null) {
182 logger.info("Could not change the PoE port state for thing '{}': device {} or portToUpdate {} null",
183 getThing().getUID(), device, uniFiPortTuple);
185 final List<UnfiPortOverrideJsonElement> updatedList = ports.entrySet().stream()
186 .map(e -> e.getValue().getJsonElement()).filter(Objects::nonNull).collect(Collectors.toList());
188 updatedList.stream().filter(p -> p.getPortIdx() == uniFiPortTuple.getPortIdx()).findAny()
189 .ifPresent(p -> p.setPoeMode(poeMode));
190 controller.poeMode(device, updatedList);
191 // No refresh because UniFi device takes some time to update. Therefore a refresh would only show the
197 private boolean handleCmd(final UniFiController controller, @Nullable final UniFiPortTuple portToUpdate,
198 final String command) throws UniFiException {
199 final UniFiDevice device = controller.getCache().getDevice(config.getMacAddress());
200 if (device == null || portToUpdate == null) {
201 logger.info("Could not change the PoE port state for thing '{}': device {} or portToUpdate {} null",
202 getThing().getUID(), device, portToUpdate);
204 if (CHANNEL_PORT_POE_CMD_POWER_CYCLE.equalsIgnoreCase(command.replaceAll("[- ]", ""))) {
205 controller.poePowerCycle(device, portToUpdate.getPortIdx());
207 logger.info("Unknown command '{}' given to PoE port for thing '{}': device {} or portToUpdate {} null",
208 command, getThing().getUID(), device, portToUpdate);