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