2 * Copyright (c) 2010-2020 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.ecobee.internal.discovery;
15 import static org.openhab.binding.ecobee.internal.EcobeeBindingConstants.*;
17 import java.util.HashMap;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.ecobee.internal.dto.thermostat.RemoteSensorDTO;
24 import org.openhab.binding.ecobee.internal.handler.EcobeeThermostatBridgeHandler;
25 import org.openhab.core.config.discovery.AbstractDiscoveryService;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.config.discovery.DiscoveryService;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerService;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * The {@link SensorDiscoveryService} is responsible for discovering the Ecobee
38 * sensors that are assigned to a thermostat.
40 * @author Mark Hilbush - Initial contribution
43 public class SensorDiscoveryService extends AbstractDiscoveryService implements DiscoveryService, ThingHandlerService {
45 private final Logger logger = LoggerFactory.getLogger(SensorDiscoveryService.class);
47 private @NonNullByDefault({}) EcobeeThermostatBridgeHandler bridgeHandler;
49 public SensorDiscoveryService() {
54 public void setThingHandler(@Nullable ThingHandler handler) {
55 if (handler instanceof EcobeeThermostatBridgeHandler) {
56 ((EcobeeThermostatBridgeHandler) handler).setDiscoveryService(this);
57 bridgeHandler = (EcobeeThermostatBridgeHandler) handler;
62 public @Nullable ThingHandler getThingHandler() {
67 public void activate() {
68 logger.debug("SensorDiscovery: Activating Ecobee sensor discovery service");
72 public void deactivate() {
73 logger.debug("SensorDiscovery: Deactivating Ecobee sensor discovery service");
77 public Set<ThingTypeUID> getSupportedThingTypes() {
78 return SUPPORTED_SENSOR_THING_TYPES_UIDS;
82 public void startBackgroundDiscovery() {
83 logger.debug("SensorDiscovery: Performing background discovery scan for {}", bridgeHandler.getThing().getUID());
88 public void startScan() {
89 logger.debug("SensorDiscovery: Starting discovery scan for {}", bridgeHandler.getThing().getUID());
94 public synchronized void abortScan() {
99 protected synchronized void stopScan() {
103 private String buildLabel(String name) {
104 return String.format("Ecobee Sensor %s", name);
107 private synchronized void discoverSensors() {
108 for (RemoteSensorDTO sensor : bridgeHandler.getSensors()) {
109 ThingUID bridgeUID = bridgeHandler.getThing().getUID();
110 ThingUID sensorUID = new ThingUID(UID_SENSOR_THING, bridgeUID, sensor.id.replace(":", "-"));
111 thingDiscovered(createDiscoveryResult(sensorUID, bridgeUID, sensor));
112 logger.trace("SensorDiscovery: Sensor with id '{}' and name '{}' added to Inbox with UID '{}'", sensor.id,
113 sensor.name, sensorUID);
117 private DiscoveryResult createDiscoveryResult(ThingUID sensorUID, ThingUID bridgeUID, RemoteSensorDTO sensor) {
118 Map<String, Object> properties = new HashMap<>(2);
119 properties.put(CONFIG_SENSOR_ID, sensor.id);
120 return DiscoveryResultBuilder.create(sensorUID).withProperties(properties)
121 .withRepresentationProperty(CONFIG_SENSOR_ID).withBridge(bridgeUID).withLabel(buildLabel(sensor.name))