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;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.unifi.internal.UniFiAccessPointThingConfig;
23 import org.openhab.binding.unifi.internal.api.UniFiController;
24 import org.openhab.binding.unifi.internal.api.UniFiException;
25 import org.openhab.binding.unifi.internal.api.cache.UniFiControllerCache;
26 import org.openhab.binding.unifi.internal.api.dto.UniFiDevice;
27 import org.openhab.binding.unifi.internal.api.dto.UniFiSite;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.ThingStatusDetail;
33 import org.openhab.core.types.Command;
34 import org.openhab.core.types.State;
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 UniFiAccessPointThingConfig config = new UniFiAccessPointThingConfig();
46 public UniFiAccessPointThingHandler(final Thing thing) {
50 private static boolean belongsToSite(final UniFiDevice client, final String siteName) {
51 boolean result = true;
52 if (!siteName.isEmpty()) {
53 final UniFiSite site = client.getSite();
54 if (site == null || !site.matchesName(siteName)) {
62 protected boolean initialize(final UniFiAccessPointThingConfig config) {
64 if (!config.isValid()) {
65 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
66 "@text/error.thing.ap.offline.configuration_error");
73 protected @Nullable UniFiDevice getEntity(final UniFiControllerCache cache) {
74 final UniFiDevice device = cache.getDevice(config.getMacAddress());
75 if (device == null || !belongsToSite(device, config.getSite()) || !DEVICE_TYPE_UAP.equals(device.getType())) {
82 protected State getChannelState(final UniFiDevice device, final String channelId) {
83 State state = getDefaultState(channelId);
86 case CHANNEL_AP_ENABLE:
87 state = OnOffType.from(!device.isDisabled());
94 protected void updateProperties(final UniFiDevice device) {
95 updateProperties(Map.of( //
96 Thing.PROPERTY_MODEL_ID, device.getModel(), //
97 Thing.PROPERTY_FIRMWARE_VERSION, device.getVersion(), //
98 Thing.PROPERTY_SERIAL_NUMBER, device.getSerial()));
102 protected boolean handleCommand(final UniFiController controller, final UniFiDevice device,
103 final ChannelUID channelUID, final Command command) throws UniFiException {
104 final String channelID = channelUID.getIdWithoutGroup();
106 if (CHANNEL_AP_ENABLE.equals(channelID) && command instanceof OnOffType onOffCommand) {
107 return handleEnableCommand(controller, device, channelUID, onOffCommand);
112 private boolean handleEnableCommand(final UniFiController controller, final UniFiDevice device,
113 final ChannelUID channelUID, final OnOffType command) throws UniFiException {
114 controller.disableAccessPoint(device, command == OnOffType.OFF);