]> git.basschouten.com Git - openhab-addons.git/blob
cf9cd92216d06c5c4d56807e377fc53d691e4e75
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.evohome.internal.discovery;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.openhab.binding.evohome.internal.EvohomeBindingConstants;
19 import org.openhab.binding.evohome.internal.api.models.v2.response.Gateway;
20 import org.openhab.binding.evohome.internal.api.models.v2.response.Location;
21 import org.openhab.binding.evohome.internal.api.models.v2.response.TemperatureControlSystem;
22 import org.openhab.binding.evohome.internal.api.models.v2.response.Zone;
23 import org.openhab.binding.evohome.internal.handler.AccountStatusListener;
24 import org.openhab.binding.evohome.internal.handler.EvohomeAccountBridgeHandler;
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.thing.ThingStatus;
29 import org.openhab.core.thing.ThingUID;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * The {@link EvohomeDiscoveryService} class is capable of discovering the available data from Evohome
35  *
36  * @author Neil Renaud - Initial contribution
37  * @author Jasper van Zuijlen - Background discovery
38  *
39  */
40 public class EvohomeDiscoveryService extends AbstractDiscoveryService implements AccountStatusListener {
41     private final Logger logger = LoggerFactory.getLogger(EvohomeDiscoveryService.class);
42     private static final int TIMEOUT = 5;
43
44     private EvohomeAccountBridgeHandler bridge;
45     private ThingUID bridgeUID;
46
47     public EvohomeDiscoveryService(EvohomeAccountBridgeHandler bridge) {
48         super(EvohomeBindingConstants.SUPPORTED_THING_TYPES_UIDS, TIMEOUT);
49
50         this.bridge = bridge;
51         this.bridgeUID = this.bridge.getThing().getUID();
52         this.bridge.addAccountStatusListener(this);
53     }
54
55     @Override
56     protected void startScan() {
57         discoverDevices();
58     }
59
60     @Override
61     protected void startBackgroundDiscovery() {
62         discoverDevices();
63     }
64
65     @Override
66     protected synchronized void stopScan() {
67         super.stopScan();
68         removeOlderResults(getTimestampOfLastScan());
69     }
70
71     @Override
72     public void accountStatusChanged(ThingStatus status) {
73         if (status == ThingStatus.ONLINE) {
74             discoverDevices();
75         }
76     }
77
78     @Override
79     public void deactivate() {
80         super.deactivate();
81         bridge.removeAccountStatusListener(this);
82     }
83
84     private void discoverDevices() {
85         if (bridge.getThing().getStatus() != ThingStatus.ONLINE) {
86             logger.debug("Evohome Gateway not online, scanning postponed");
87             return;
88         }
89
90         for (Location location : bridge.getEvohomeConfig()) {
91             for (Gateway gateway : location.getGateways()) {
92                 for (TemperatureControlSystem tcs : gateway.getTemperatureControlSystems()) {
93                     addDisplayDiscoveryResult(location, tcs);
94                     for (Zone zone : tcs.getZones()) {
95                         addZoneDiscoveryResult(location, zone);
96                     }
97                 }
98             }
99         }
100
101         stopScan();
102     }
103
104     private void addDisplayDiscoveryResult(Location location, TemperatureControlSystem tcs) {
105         String id = tcs.getSystemId();
106         String name = location.getLocationInfo().getName();
107         ThingUID thingUID = new ThingUID(EvohomeBindingConstants.THING_TYPE_EVOHOME_DISPLAY, bridgeUID, id);
108
109         Map<String, Object> properties = new HashMap<>(2);
110         properties.put(EvohomeBindingConstants.PROPERTY_ID, id);
111         properties.put(EvohomeBindingConstants.PROPERTY_NAME, name);
112
113         addDiscoveredThing(thingUID, properties, name);
114     }
115
116     private void addZoneDiscoveryResult(Location location, Zone zone) {
117         String id = zone.getZoneId();
118         String name = zone.getName() + " (" + location.getLocationInfo().getName() + ")";
119         ThingUID thingUID = new ThingUID(EvohomeBindingConstants.THING_TYPE_EVOHOME_HEATING_ZONE, bridgeUID, id);
120
121         Map<String, Object> properties = new HashMap<>(2);
122         properties.put(EvohomeBindingConstants.PROPERTY_ID, id);
123         properties.put(EvohomeBindingConstants.PROPERTY_NAME, name);
124
125         addDiscoveredThing(thingUID, properties, name);
126     }
127
128     private void addDiscoveredThing(ThingUID thingUID, Map<String, Object> properties, String displayLabel) {
129         DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
130                 .withBridge(bridgeUID).withLabel(displayLabel).build();
131         thingDiscovered(discoveryResult);
132     }
133 }