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("Niko Home Control: 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("Niko Home Control: not connected.");
76 logger.debug("Niko Home Control: 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("Niko Home Control: unrecognized action type {} for {} {}", nhcAction.getType(),
103 actionId, thingName);
107 Map<String, NhcThermostat> thermostats = nhcComm.getThermostats();
109 thermostats.forEach((thermostatId, nhcThermostat) -> {
110 String thingName = nhcThermostat.getName();
111 String thingLocation = nhcThermostat.getLocation();
112 addThermostatDevice(new ThingUID(THING_TYPE_THERMOSTAT, handler.getThing().getUID(), thermostatId),
113 thermostatId, thingName, thingLocation);
116 Map<String, NhcEnergyMeter> energyMeters = nhcComm.getEnergyMeters();
118 energyMeters.forEach((energyMeterId, nhcEnergyMeter) -> {
119 String thingName = nhcEnergyMeter.getName();
120 addEnergyMeterDevice(new ThingUID(THING_TYPE_ENERGYMETER, handler.getThing().getUID(), energyMeterId),
121 energyMeterId, thingName);
125 private void addActionDevice(ThingUID uid, String actionId, String thingName, @Nullable String thingLocation) {
126 DiscoveryResultBuilder discoveryResultBuilder = DiscoveryResultBuilder.create(uid).withBridge(bridgeUID)
127 .withLabel(thingName).withProperty(CONFIG_ACTION_ID, actionId);
128 if (thingLocation != null) {
129 discoveryResultBuilder.withProperty("Location", thingLocation);
131 thingDiscovered(discoveryResultBuilder.build());
134 private void addThermostatDevice(ThingUID uid, String thermostatId, String thingName,
135 @Nullable String thingLocation) {
136 DiscoveryResultBuilder discoveryResultBuilder = DiscoveryResultBuilder.create(uid).withBridge(bridgeUID)
137 .withLabel(thingName).withProperty(CONFIG_THERMOSTAT_ID, thermostatId);
138 if (thingLocation != null) {
139 discoveryResultBuilder.withProperty("Location", thingLocation);
141 thingDiscovered(discoveryResultBuilder.build());
144 private void addEnergyMeterDevice(ThingUID uid, String energyMeterId, String thingName) {
145 DiscoveryResultBuilder discoveryResultBuilder = DiscoveryResultBuilder.create(uid).withBridge(bridgeUID)
146 .withLabel(thingName).withProperty(CONFIG_ENERGYMETER_ID, energyMeterId);
147 thingDiscovered(discoveryResultBuilder.build());
151 protected void startScan() {
156 protected synchronized void stopScan() {
158 removeOlderResults(getTimestampOfLastScan());