]> git.basschouten.com Git - openhab-addons.git/blob
eacf3fae80be1d1e704dbe35c5ae2ef457b53a33
[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 java.util.Map;
19
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;
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 UniFiAccessPointThingConfig config = new UniFiAccessPointThingConfig();
45
46     public UniFiAccessPointThingHandler(final Thing thing) {
47         super(thing);
48     }
49
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)) {
55                 result = false;
56             }
57         }
58         return result;
59     }
60
61     @Override
62     protected boolean initialize(final UniFiAccessPointThingConfig config) {
63         this.config = config;
64         if (!config.isValid()) {
65             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
66                     "@text/error.thing.ap.offline.configuration_error");
67             return false;
68         }
69         return true;
70     }
71
72     @Override
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())) {
76             return null;
77         }
78         return device;
79     }
80
81     @Override
82     protected State getChannelState(final UniFiDevice device, final String channelId) {
83         State state = getDefaultState(channelId);
84
85         switch (channelId) {
86             case CHANNEL_AP_ENABLE:
87                 state = OnOffType.from(!device.isDisabled());
88                 break;
89         }
90         return state;
91     }
92
93     @Override
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()));
99     }
100
101     @Override
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();
105
106         if (CHANNEL_AP_ENABLE.equals(channelID) && command instanceof OnOffType onOffCommand) {
107             return handleEnableCommand(controller, device, channelUID, onOffCommand);
108         }
109         return false;
110     }
111
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);
115         refresh();
116         return true;
117     }
118 }