2 * Copyright (c) 2010-2023 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_GUEST_CLIENTS;
16 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_TOTAL_CLIENTS;
17 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_WIRED_CLIENTS;
18 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.CHANNEL_WIRELESS_CLIENTS;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.unifi.internal.UniFiSiteThingConfig;
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.UniFiSite;
27 import org.openhab.core.library.types.DecimalType;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.State;
34 import org.openhab.core.types.UnDefType;
37 * The {@link UniFiSiteThingHandler} is responsible for handling commands and status
38 * updates for {@link UniFiSite} instances.
40 * @author Matthew Bowman - Initial contribution
41 * @author Hilbrand Bouwkamp - Initial contribution
44 public class UniFiSiteThingHandler extends UniFiBaseThingHandler<UniFiSite, UniFiSiteThingConfig> {
46 private UniFiSiteThingConfig config = new UniFiSiteThingConfig();
48 public UniFiSiteThingHandler(final Thing thing) {
53 protected boolean initialize(final UniFiSiteThingConfig config) {
55 if (!config.isValid()) {
56 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
57 "@text/error.thing.site.offline.configuration_error");
64 protected @Nullable UniFiSite getEntity(final UniFiControllerCache cache) {
65 return cache.getSite(config.getSiteID());
69 protected State getChannelState(final UniFiSite site, final String channelId) {
70 final UniFiControllerCache cache = site.getCache();
74 case CHANNEL_TOTAL_CLIENTS:
75 count = cache.countClients(site, c -> true);
77 case CHANNEL_WIRELESS_CLIENTS:
78 count = cache.countClients(site, c -> c.isWireless());
80 case CHANNEL_WIRED_CLIENTS:
81 count = cache.countClients(site, c -> c.isWired());
83 case CHANNEL_GUEST_CLIENTS:
84 count = cache.countClients(site, c -> c.isGuest());
87 // Unsupported channel; nothing to update
88 return UnDefType.NULL;
90 return new DecimalType(count);
94 protected boolean handleCommand(final UniFiController controller, final UniFiSite entity,
95 final ChannelUID channelUID, final Command command) throws UniFiException {