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.ThermostatDTO;
24 import org.openhab.binding.ecobee.internal.handler.EcobeeAccountBridgeHandler;
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 ThermostatDiscoveryService} is responsible for discovering the Ecobee
38 * thermostats that are associated with the Ecobee Account.
40 * @author Mark Hilbush - Initial contribution
43 public class ThermostatDiscoveryService extends AbstractDiscoveryService
44 implements DiscoveryService, ThingHandlerService {
46 private final Logger logger = LoggerFactory.getLogger(ThermostatDiscoveryService.class);
48 private @NonNullByDefault({}) EcobeeAccountBridgeHandler bridgeHandler;
50 public ThermostatDiscoveryService() {
55 public void setThingHandler(@Nullable ThingHandler handler) {
56 if (handler instanceof EcobeeAccountBridgeHandler) {
57 this.bridgeHandler = (EcobeeAccountBridgeHandler) handler;
58 this.bridgeHandler.setDiscoveryService(this);
63 public @Nullable ThingHandler getThingHandler() {
68 public void activate() {
69 logger.debug("ThermostatDiscovery: Activating Ecobee thermostat discovery service for {}",
70 bridgeHandler.getThing().getUID());
74 public void deactivate() {
75 logger.debug("ThermostatDiscovery: Deactivating Ecobee thermostat discovery service for {}",
76 bridgeHandler.getThing().getUID());
80 public Set<ThingTypeUID> getSupportedThingTypes() {
81 return SUPPORTED_THERMOSTAT_BRIDGE_THING_TYPES_UIDS;
85 public void startBackgroundDiscovery() {
86 logger.trace("ThermostatDiscovery: Performing background discovery scan for {}",
87 bridgeHandler.getThing().getUID());
88 discoverThermostats();
92 public void startScan() {
93 logger.debug("ThermostatDiscovery: Starting discovery scan for {}", bridgeHandler.getThing().getUID());
94 discoverThermostats();
98 public synchronized void abortScan() {
103 protected synchronized void stopScan() {
107 private String buildLabel(String name) {
108 return String.format("Ecobee Thermostat %s", name);
111 private synchronized void discoverThermostats() {
112 for (ThermostatDTO thermostat : bridgeHandler.getRegisteredThermostats()) {
113 String name = thermostat.name;
114 String identifier = thermostat.identifier;
115 if (identifier != null && name != null) {
116 ThingUID thingUID = new ThingUID(UID_THERMOSTAT_BRIDGE, bridgeHandler.getThing().getUID(),
117 thermostat.identifier);
118 thingDiscovered(createDiscoveryResult(thingUID, identifier, name));
119 logger.trace("ThermostatDiscovery: Thermostat with id '{}' and name '{}' added to Inbox with UID '{}'",
120 thermostat.identifier, thermostat.name, thingUID);
125 private DiscoveryResult createDiscoveryResult(ThingUID thermostatUID, String identifier, String name) {
126 Map<String, Object> properties = new HashMap<>(2);
127 properties.put(CONFIG_THERMOSTAT_ID, identifier);
128 return DiscoveryResultBuilder.create(thermostatUID).withProperties(properties)
129 .withRepresentationProperty(CONFIG_THERMOSTAT_ID).withBridge(bridgeHandler.getThing().getUID())
130 .withLabel(buildLabel(name)).build();