2 * Copyright (c) 2010-2024 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_AP_ENABLE;
16 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.DEVICE_TYPE_UAP;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.unifi.internal.UniFiAccessPointThingConfig;
21 import org.openhab.binding.unifi.internal.api.UniFiController;
22 import org.openhab.binding.unifi.internal.api.UniFiException;
23 import org.openhab.binding.unifi.internal.api.cache.UniFiControllerCache;
24 import org.openhab.binding.unifi.internal.api.dto.UniFiDevice;
25 import org.openhab.binding.unifi.internal.api.dto.UniFiSite;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingStatus;
30 import org.openhab.core.thing.ThingStatusDetail;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.State;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * An access point managed by the UniFi controller software.
39 * @author Thomas Lauterbach - Initial contribution
42 public class UniFiAccessPointThingHandler extends UniFiBaseThingHandler<UniFiDevice, UniFiAccessPointThingConfig> {
44 private final Logger logger = LoggerFactory.getLogger(UniFiAccessPointThingHandler.class);
46 private UniFiAccessPointThingConfig config = new UniFiAccessPointThingConfig();
48 public UniFiAccessPointThingHandler(final Thing thing) {
52 private static boolean belongsToSite(final UniFiDevice client, final String siteName) {
53 boolean result = true;
54 if (!siteName.isEmpty()) {
55 final UniFiSite site = client.getSite();
56 if (site == null || !site.matchesName(siteName)) {
64 protected boolean initialize(final UniFiAccessPointThingConfig config) {
66 if (!config.isValid()) {
67 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
68 "@text/error.thing.ap.offline.configuration_error");
75 protected @Nullable UniFiDevice getEntity(final UniFiControllerCache cache) {
76 final UniFiDevice device = cache.getDevice(config.getMacAddress());
77 if (device == null || !belongsToSite(device, config.getSite()) || !DEVICE_TYPE_UAP.equals(device.getType())) {
84 protected State getChannelState(final UniFiDevice device, final String channelId) {
85 State state = getDefaultState(channelId);
88 case CHANNEL_AP_ENABLE:
89 state = OnOffType.from(!device.isDisabled());
96 protected boolean handleCommand(final UniFiController controller, final UniFiDevice device,
97 final ChannelUID channelUID, final Command command) throws UniFiException {
98 final String channelID = channelUID.getIdWithoutGroup();
100 if (CHANNEL_AP_ENABLE.equals(channelID) && command instanceof OnOffType onOffCommand) {
101 return handleEnableCommand(controller, device, channelUID, onOffCommand);
106 private boolean handleEnableCommand(final UniFiController controller, final UniFiDevice device,
107 final ChannelUID channelUID, final OnOffType command) throws UniFiException {
108 controller.disableAccessPoint(device, command == OnOffType.OFF);