]> git.basschouten.com Git - openhab-addons.git/blob
419a488aad47d117ab6da57437a7bb50d887005c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.unifi.internal.handler;
14
15 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_AP_ENABLE;
16 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.DEVICE_TYPE_UAP;
17
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;
35
36 /**
37  * An access point managed by the UniFi controller software.
38  *
39  * @author Thomas Lauterbach - Initial contribution
40  */
41 @NonNullByDefault
42 public class UniFiAccessPointThingHandler extends UniFiBaseThingHandler<UniFiDevice, UniFiAccessPointThingConfig> {
43
44     private final Logger logger = LoggerFactory.getLogger(UniFiAccessPointThingHandler.class);
45
46     private UniFiAccessPointThingConfig config = new UniFiAccessPointThingConfig();
47
48     public UniFiAccessPointThingHandler(final Thing thing) {
49         super(thing);
50     }
51
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)) {
57                 result = false;
58             }
59         }
60         return result;
61     }
62
63     @Override
64     protected boolean initialize(final UniFiAccessPointThingConfig config) {
65         this.config = config;
66         if (!config.isValid()) {
67             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
68                     "@text/error.thing.ap.offline.configuration_error");
69             return false;
70         }
71         return true;
72     }
73
74     @Override
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())) {
78             return null;
79         }
80         return device;
81     }
82
83     @Override
84     protected State getChannelState(final UniFiDevice device, final String channelId) {
85         State state = getDefaultState(channelId);
86
87         switch (channelId) {
88             case CHANNEL_AP_ENABLE:
89                 state = OnOffType.from(!device.isDisabled());
90                 break;
91         }
92         return state;
93     }
94
95     @Override
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();
99
100         if (CHANNEL_AP_ENABLE.equals(channelID) && command instanceof OnOffType onOffCommand) {
101             return handleEnableCommand(controller, device, channelUID, onOffCommand);
102         }
103         return false;
104     }
105
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);
109         refresh();
110         return true;
111     }
112 }