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.digitalstrom.internal.discovery;
15 import static org.openhab.binding.digitalstrom.internal.DigitalSTROMBindingConstants.BINDING_ID;
17 import java.util.Arrays;
18 import java.util.Date;
19 import java.util.HashMap;
20 import java.util.HashSet;
23 import org.openhab.binding.digitalstrom.internal.DigitalSTROMBindingConstants;
24 import org.openhab.binding.digitalstrom.internal.handler.BridgeHandler;
25 import org.openhab.binding.digitalstrom.internal.lib.climate.jsonresponsecontainer.impl.TemperatureControlStatus;
26 import org.openhab.core.config.discovery.AbstractDiscoveryService;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * The {@link ZoneTemperatureControlDiscoveryService} discovers all digitalSTROM zones which have temperature control
36 * configured. The thing-type has to be given to the
37 * {@link #ZoneTemperatureControlDiscoveryService(BridgeHandler, ThingTypeUID)} as {@link ThingTypeUID}. The supported
38 * {@link ThingTypeUID} can be found at {@link ZoneTemperatureControlHandler#SUPPORTED_THING_TYPES}
40 * @author Michael Ochel - Initial contribution
41 * @author Matthias Siegele - Initial contribution
43 public class ZoneTemperatureControlDiscoveryService extends AbstractDiscoveryService {
45 private final Logger logger = LoggerFactory.getLogger(ZoneTemperatureControlDiscoveryService.class);
46 BridgeHandler bridgeHandler;
47 private final ThingUID bridgeUID;
48 private final String thingTypeID;
50 public static final int TIMEOUT = 10;
53 * Creates a new {@link ZoneTemperatureControlDiscoveryService}.
55 * @param bridgeHandler must not be null
56 * @param supportedThingType must not be null
57 * @throws IllegalArgumentException see {@link AbstractDiscoveryService#AbstractDiscoveryService(int)}
59 public ZoneTemperatureControlDiscoveryService(BridgeHandler bridgeHandler, ThingTypeUID supportedThingType)
60 throws IllegalArgumentException {
61 super(new HashSet<>(Arrays.asList(supportedThingType)), TIMEOUT, true);
62 bridgeUID = bridgeHandler.getThing().getUID();
63 this.bridgeHandler = bridgeHandler;
64 thingTypeID = supportedThingType.getId();
68 protected void startScan() {
69 for (TemperatureControlStatus tempConStat : bridgeHandler.getTemperatureControlStatusFromAllZones()) {
70 internalConfigChanged(tempConStat);
75 public void deactivate() {
76 logger.debug("Deactivate discovery service for zone teperature control type remove thing types {}",
77 super.getSupportedThingTypes());
78 removeOlderResults(new Date().getTime());
82 * Method for the background discovery
84 * @see org.openhab.binding.digitalstrom.internal.lib.listener.TemperatureControlStatusListener#configChanged(TemperatureControlStatus)
85 * @param tempControlStatus can be null
87 public void configChanged(TemperatureControlStatus tempControlStatus) {
88 if (isBackgroundDiscoveryEnabled()) {
89 internalConfigChanged(tempControlStatus);
93 private void internalConfigChanged(TemperatureControlStatus tempControlStatus) {
94 if (tempControlStatus == null) {
97 if (tempControlStatus.isNotSetOff()) {
98 logger.debug("found configured zone TemperatureControlStatus = {}", tempControlStatus);
100 ThingUID thingUID = getThingUID(tempControlStatus);
101 if (thingUID != null) {
102 Map<String, Object> properties = new HashMap<>();
103 properties.put(DigitalSTROMBindingConstants.ZONE_ID, tempControlStatus.getZoneID());
104 String zoneName = tempControlStatus.getZoneName();
105 if (zoneName == null || zoneName.isBlank()) {
106 zoneName = tempControlStatus.getZoneID().toString();
108 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
109 .withBridge(bridgeUID).withLabel(zoneName).build();
110 thingDiscovered(discoveryResult);
116 private ThingUID getThingUID(TemperatureControlStatus tempControlStatus) {
117 ThingTypeUID thingTypeUID = new ThingTypeUID(BINDING_ID, thingTypeID);
118 if (getSupportedThingTypes().contains(thingTypeUID)) {
119 String thingID = tempControlStatus.getZoneID().toString();
120 return new ThingUID(thingTypeUID, bridgeUID, thingID);
127 * Returns the ID of this {@link ZoneTemperatureControlDiscoveryService}.
129 * @return id of the service
131 public String getID() {