]> git.basschouten.com Git - openhab-addons.git/blob
5b8f7b74cdbc85d0bf4240aa5dc8a0df40acaf46
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.ecobee.internal.discovery;
14
15 import static org.openhab.binding.ecobee.internal.EcobeeBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20
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;
35
36 /**
37  * The {@link ThermostatDiscoveryService} is responsible for discovering the Ecobee
38  * thermostats that are associated with the Ecobee Account.
39  *
40  * @author Mark Hilbush - Initial contribution
41  */
42 @NonNullByDefault
43 public class ThermostatDiscoveryService extends AbstractDiscoveryService
44         implements DiscoveryService, ThingHandlerService {
45
46     private final Logger logger = LoggerFactory.getLogger(ThermostatDiscoveryService.class);
47
48     private @NonNullByDefault({}) EcobeeAccountBridgeHandler bridgeHandler;
49
50     public ThermostatDiscoveryService() {
51         super(30);
52     }
53
54     @Override
55     public void setThingHandler(@Nullable ThingHandler handler) {
56         if (handler instanceof EcobeeAccountBridgeHandler) {
57             this.bridgeHandler = (EcobeeAccountBridgeHandler) handler;
58             this.bridgeHandler.setDiscoveryService(this);
59         }
60     }
61
62     @Override
63     public @Nullable ThingHandler getThingHandler() {
64         return bridgeHandler;
65     }
66
67     @Override
68     public void activate() {
69         logger.debug("ThermostatDiscovery: Activating Ecobee thermostat discovery service for {}",
70                 bridgeHandler.getThing().getUID());
71     }
72
73     @Override
74     public void deactivate() {
75         logger.debug("ThermostatDiscovery: Deactivating Ecobee thermostat discovery service for {}",
76                 bridgeHandler.getThing().getUID());
77     }
78
79     @Override
80     public Set<ThingTypeUID> getSupportedThingTypes() {
81         return SUPPORTED_THERMOSTAT_BRIDGE_THING_TYPES_UIDS;
82     }
83
84     @Override
85     public void startBackgroundDiscovery() {
86         logger.trace("ThermostatDiscovery: Performing background discovery scan for {}",
87                 bridgeHandler.getThing().getUID());
88         discoverThermostats();
89     }
90
91     @Override
92     public void startScan() {
93         logger.debug("ThermostatDiscovery: Starting discovery scan for {}", bridgeHandler.getThing().getUID());
94         discoverThermostats();
95     }
96
97     @Override
98     public synchronized void abortScan() {
99         super.abortScan();
100     }
101
102     @Override
103     protected synchronized void stopScan() {
104         super.stopScan();
105     }
106
107     private String buildLabel(String name) {
108         return String.format("Ecobee Thermostat %s", name);
109     }
110
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);
121             }
122         }
123     }
124
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();
131     }
132 }