2 * Copyright (c) 2010-2021 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.nikohomecontrol.internal.discovery;
15 import static org.openhab.binding.nikohomecontrol.internal.NikoHomeControlBindingConstants.*;
17 import java.util.Date;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.nikohomecontrol.internal.handler.NikoHomeControlBridgeHandler;
23 import org.openhab.binding.nikohomecontrol.internal.protocol.NhcAction;
24 import org.openhab.binding.nikohomecontrol.internal.protocol.NhcEnergyMeter;
25 import org.openhab.binding.nikohomecontrol.internal.protocol.NhcThermostat;
26 import org.openhab.binding.nikohomecontrol.internal.protocol.NikoHomeControlCommunication;
27 import org.openhab.core.config.discovery.AbstractDiscoveryService;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.thing.ThingUID;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * If a Niko Home Control bridge is added or if the user scans manually for things this
35 * {@link NikoHomeControlDiscoveryService} is used to return Niko Home Control Actions as things to the framework.
37 * @author Mark Herwege - Initial Contribution
40 public class NikoHomeControlDiscoveryService extends AbstractDiscoveryService {
42 private final Logger logger = LoggerFactory.getLogger(NikoHomeControlDiscoveryService.class);
44 private static final int TIMEOUT = 5;
46 private ThingUID bridgeUID;
47 private NikoHomeControlBridgeHandler handler;
49 public NikoHomeControlDiscoveryService(NikoHomeControlBridgeHandler handler) {
50 super(SUPPORTED_THING_TYPES_UIDS, TIMEOUT, false);
51 logger.debug("discovery service {}", handler);
52 bridgeUID = handler.getThing().getUID();
53 this.handler = handler;
56 public void activate() {
57 handler.setNhcDiscovery(this);
61 public void deactivate() {
62 removeOlderResults(new Date().getTime());
63 handler.setNhcDiscovery(null);
67 * Discovers devices connected to a Niko Home Control controller
69 public void discoverDevices() {
70 NikoHomeControlCommunication nhcComm = handler.getCommunication();
72 if ((nhcComm == null) || !nhcComm.communicationActive()) {
73 logger.warn("not connected");
76 logger.debug("getting devices on {}", handler.getThing().getUID().getId());
78 Map<String, NhcAction> actions = nhcComm.getActions();
80 actions.forEach((actionId, nhcAction) -> {
81 String thingName = nhcAction.getName();
82 String thingLocation = nhcAction.getLocation();
84 switch (nhcAction.getType()) {
86 addActionDevice(new ThingUID(THING_TYPE_PUSHBUTTON, handler.getThing().getUID(), actionId),
87 actionId, thingName, thingLocation);
90 addActionDevice(new ThingUID(THING_TYPE_ON_OFF_LIGHT, handler.getThing().getUID(), actionId),
91 actionId, thingName, thingLocation);
94 addActionDevice(new ThingUID(THING_TYPE_DIMMABLE_LIGHT, handler.getThing().getUID(), actionId),
95 actionId, thingName, thingLocation);
98 addActionDevice(new ThingUID(THING_TYPE_BLIND, handler.getThing().getUID(), actionId), actionId,
99 thingName, thingLocation);
102 logger.debug("unrecognized action type {} for {} {}", nhcAction.getType(), actionId, thingName);
106 Map<String, NhcThermostat> thermostats = nhcComm.getThermostats();
108 thermostats.forEach((thermostatId, nhcThermostat) -> {
109 String thingName = nhcThermostat.getName();
110 String thingLocation = nhcThermostat.getLocation();
111 addThermostatDevice(new ThingUID(THING_TYPE_THERMOSTAT, handler.getThing().getUID(), thermostatId),
112 thermostatId, thingName, thingLocation);
115 Map<String, NhcEnergyMeter> energyMeters = nhcComm.getEnergyMeters();
117 energyMeters.forEach((energyMeterId, nhcEnergyMeter) -> {
118 String thingName = nhcEnergyMeter.getName();
119 addEnergyMeterDevice(new ThingUID(THING_TYPE_ENERGYMETER, handler.getThing().getUID(), energyMeterId),
120 energyMeterId, thingName);
124 private void addActionDevice(ThingUID uid, String actionId, String thingName, @Nullable String thingLocation) {
125 DiscoveryResultBuilder discoveryResultBuilder = DiscoveryResultBuilder.create(uid).withBridge(bridgeUID)
126 .withLabel(thingName).withProperty(CONFIG_ACTION_ID, actionId);
127 if (thingLocation != null) {
128 discoveryResultBuilder.withProperty("Location", thingLocation);
130 thingDiscovered(discoveryResultBuilder.build());
133 private void addThermostatDevice(ThingUID uid, String thermostatId, String thingName,
134 @Nullable String thingLocation) {
135 DiscoveryResultBuilder discoveryResultBuilder = DiscoveryResultBuilder.create(uid).withBridge(bridgeUID)
136 .withLabel(thingName).withProperty(CONFIG_THERMOSTAT_ID, thermostatId);
137 if (thingLocation != null) {
138 discoveryResultBuilder.withProperty("Location", thingLocation);
140 thingDiscovered(discoveryResultBuilder.build());
143 private void addEnergyMeterDevice(ThingUID uid, String energyMeterId, String thingName) {
144 DiscoveryResultBuilder discoveryResultBuilder = DiscoveryResultBuilder.create(uid).withBridge(bridgeUID)
145 .withLabel(thingName).withProperty(CONFIG_ENERGYMETER_ID, energyMeterId);
146 thingDiscovered(discoveryResultBuilder.build());
150 protected void startScan() {
155 protected synchronized void stopScan() {
157 removeOlderResults(getTimestampOfLastScan());