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.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;
36 * The {@link EvohomeDiscoveryService} class is capable of discovering the available data from Evohome
38 * @author Neil Renaud - Initial contribution
39 * @author Jasper van Zuijlen - Background discovery
43 public class EvohomeDiscoveryService extends AbstractDiscoveryService implements AccountStatusListener {
44 private final Logger logger = LoggerFactory.getLogger(EvohomeDiscoveryService.class);
45 private static final int TIMEOUT = 5;
47 private EvohomeAccountBridgeHandler bridge;
48 private ThingUID bridgeUID;
50 public EvohomeDiscoveryService(EvohomeAccountBridgeHandler bridge) {
51 super(EvohomeBindingConstants.SUPPORTED_THING_TYPES_UIDS, TIMEOUT);
54 this.bridgeUID = this.bridge.getThing().getUID();
55 this.bridge.addAccountStatusListener(this);
59 protected void startScan() {
64 protected void startBackgroundDiscovery() {
69 protected synchronized void stopScan() {
71 removeOlderResults(getTimestampOfLastScan());
75 public void accountStatusChanged(ThingStatus status) {
76 if (status == ThingStatus.ONLINE) {
82 public void deactivate() {
84 bridge.removeAccountStatusListener(this);
87 private void discoverDevices() {
88 if (bridge.getThing().getStatus() != ThingStatus.ONLINE) {
89 logger.debug("Evohome Gateway not online, scanning postponed");
92 Locations localEvohomeConfig = bridge.getEvohomeConfig();
94 if (localEvohomeConfig == null) {
97 for (Location location : localEvohomeConfig) {
98 if (location == null) {
101 for (Gateway gateway : location.getGateways()) {
102 for (TemperatureControlSystem tcs : gateway.getTemperatureControlSystems()) {
106 addDisplayDiscoveryResult(location, tcs);
107 for (Zone zone : tcs.getZones()) {
109 addZoneDiscoveryResult(location, zone);
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);
123 Map<String, Object> properties = new HashMap<>(2);
124 properties.put(EvohomeBindingConstants.PROPERTY_ID, id);
125 properties.put(EvohomeBindingConstants.PROPERTY_NAME, name);
127 addDiscoveredThing(thingUID, properties, name);
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);
135 Map<String, Object> properties = new HashMap<>(2);
136 properties.put(EvohomeBindingConstants.PROPERTY_ID, id);
137 properties.put(EvohomeBindingConstants.PROPERTY_NAME, name);
139 addDiscoveredThing(thingUID, properties, name);
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);