2 * Copyright (c) 2010-2023 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.evohome.internal.discovery;
15 import java.util.HashMap;
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;
34 * The {@link EvohomeDiscoveryService} class is capable of discovering the available data from Evohome
36 * @author Neil Renaud - Initial contribution
37 * @author Jasper van Zuijlen - Background discovery
40 public class EvohomeDiscoveryService extends AbstractDiscoveryService implements AccountStatusListener {
41 private final Logger logger = LoggerFactory.getLogger(EvohomeDiscoveryService.class);
42 private static final int TIMEOUT = 5;
44 private EvohomeAccountBridgeHandler bridge;
45 private ThingUID bridgeUID;
47 public EvohomeDiscoveryService(EvohomeAccountBridgeHandler bridge) {
48 super(EvohomeBindingConstants.SUPPORTED_THING_TYPES_UIDS, TIMEOUT);
51 this.bridgeUID = this.bridge.getThing().getUID();
52 this.bridge.addAccountStatusListener(this);
56 protected void startScan() {
61 protected void startBackgroundDiscovery() {
66 protected synchronized void stopScan() {
68 removeOlderResults(getTimestampOfLastScan());
72 public void accountStatusChanged(ThingStatus status) {
73 if (status == ThingStatus.ONLINE) {
79 public void deactivate() {
81 bridge.removeAccountStatusListener(this);
84 private void discoverDevices() {
85 if (bridge.getThing().getStatus() != ThingStatus.ONLINE) {
86 logger.debug("Evohome Gateway not online, scanning postponed");
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);
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);
109 Map<String, Object> properties = new HashMap<>(2);
110 properties.put(EvohomeBindingConstants.PROPERTY_ID, id);
111 properties.put(EvohomeBindingConstants.PROPERTY_NAME, name);
113 addDiscoveredThing(thingUID, properties, name);
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);
121 Map<String, Object> properties = new HashMap<>(2);
122 properties.put(EvohomeBindingConstants.PROPERTY_ID, id);
123 properties.put(EvohomeBindingConstants.PROPERTY_NAME, name);
125 addDiscoveredThing(thingUID, properties, name);
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);