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 UniFiPortTable port = getPort(ports).getTable();
104 logger.debug("No PoE port for thing '{}' could be found in the data. Refresh ignored.",
105 getThing().getUID());
106 return UnDefType.NULL;
112 state = OnOffType.from(port.isUp());
114 case CHANNEL_PORT_POE_ENABLE:
115 state = OnOffType.from(port.isPoeEnabled());
117 case CHANNEL_PORT_POE_MODE:
118 state = StringType.valueOf(port.getPoeMode());
120 case CHANNEL_PORT_POE_POWER:
121 state = new QuantityType<Power>(Double.valueOf(port.getPoePower()), Units.WATT);
123 case CHANNEL_PORT_POE_VOLTAGE:
124 state = new QuantityType<ElectricPotential>(Double.valueOf(port.getPoeVoltage()), Units.VOLT);
126 case CHANNEL_PORT_POE_CURRENT:
127 state = new QuantityType<ElectricCurrent>(Double.valueOf(port.getPoeCurrent()), MILLI(Units.AMPERE));
130 state = UnDefType.UNDEF;
135 private @Nullable UniFiPortTuple getPort(final Map<Integer, UniFiPortTuple> ports) {
136 return ports.get(config.getPortNumber());
140 protected boolean handleCommand(final UniFiController controller, final Map<Integer, UniFiPortTuple> ports,
141 final ChannelUID channelUID, final Command command) throws UniFiException {
142 final String channelID = channelUID.getIdWithoutGroup();
145 case CHANNEL_PORT_POE_ENABLE:
146 if (command instanceof OnOffType) {
147 return handleModeCommand(controller, ports, getPort(ports),
148 OnOffType.ON == command ? poeEnableMode : CHANNEL_ENABLE_PARAMETER_MODE_OFF);
151 case CHANNEL_PORT_POE_MODE:
152 if (command instanceof StringType) {
153 return handleModeCommand(controller, ports, getPort(ports), command.toFullString());
156 case CHANNEL_PORT_POE_CMD:
157 if (command instanceof StringType) {
158 return handleCmd(controller, getPort(ports), command.toFullString());
166 private boolean handleModeCommand(final UniFiController controller, final Map<Integer, UniFiPortTuple> ports,
167 final @Nullable UniFiPortTuple uniFiPortTuple, final String poeMode) throws UniFiException {
168 final UniFiDevice device = controller.getCache().getDevice(config.getMacAddress());
170 if (device == null || uniFiPortTuple == null) {
171 logger.info("Could not change the PoE port state for thing '{}': device {} or portToUpdate {} null",
172 getThing().getUID(), device, uniFiPortTuple);
174 final List<UnfiPortOverrideJsonElement> updatedList = ports.entrySet().stream()
175 .map(e -> e.getValue().getJsonElement()).filter(Objects::nonNull).collect(Collectors.toList());
177 updatedList.stream().filter(p -> p.getPortIdx() == uniFiPortTuple.getPortIdx()).findAny()
178 .ifPresent(p -> p.setPoeMode(poeMode));
179 controller.poeMode(device, updatedList);
180 // No refresh because UniFi device takes some time to update. Therefore a refresh would only show the
186 private boolean handleCmd(final UniFiController controller, @Nullable final UniFiPortTuple portToUpdate,
187 final String command) throws UniFiException {
188 final UniFiDevice device = controller.getCache().getDevice(config.getMacAddress());
189 if (device == null || portToUpdate == null) {
190 logger.info("Could not change the PoE port state for thing '{}': device {} or portToUpdate {} null",
191 getThing().getUID(), device, portToUpdate);
193 if (CHANNEL_PORT_POE_CMD_POWER_CYCLE.equalsIgnoreCase(command.replaceAll("[- ]", ""))) {
194 controller.poePowerCycle(device, portToUpdate.getPortIdx());
196 logger.info("Unknown command '{}' given to PoE port for thing '{}': device {} or portToUpdate {} null",
197 command, getThing().getUID(), device, portToUpdate);