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
38 * {@link org.openhab.core.thing.ThingTypeUID}. The supported {@link org.openhab.core.thing.ThingTypeUID}
40 * {@link org.openhab.binding.digitalstrom.internal.handler.ZoneTemperatureControlHandler#SUPPORTED_THING_TYPES}
42 * @author Michael Ochel - Initial contribution
43 * @author Matthias Siegele - Initial contribution
45 public class ZoneTemperatureControlDiscoveryService extends AbstractDiscoveryService {
47 private final Logger logger = LoggerFactory.getLogger(ZoneTemperatureControlDiscoveryService.class);
48 BridgeHandler bridgeHandler;
49 private final ThingUID bridgeUID;
50 private final String thingTypeID;
52 public static final int TIMEOUT = 10;
55 * Creates a new {@link ZoneTemperatureControlDiscoveryService}.
57 * @param bridgeHandler must not be null
58 * @param supportedThingType must not be null
59 * @throws IllegalArgumentException see {@link AbstractDiscoveryService#AbstractDiscoveryService(int)}
61 public ZoneTemperatureControlDiscoveryService(BridgeHandler bridgeHandler, ThingTypeUID supportedThingType)
62 throws IllegalArgumentException {
63 super(new HashSet<>(Arrays.asList(supportedThingType)), TIMEOUT, true);
64 bridgeUID = bridgeHandler.getThing().getUID();
65 this.bridgeHandler = bridgeHandler;
66 thingTypeID = supportedThingType.getId();
70 protected void startScan() {
71 for (TemperatureControlStatus tempConStat : bridgeHandler.getTemperatureControlStatusFromAllZones()) {
72 internalConfigChanged(tempConStat);
77 public void deactivate() {
78 logger.debug("Deactivate discovery service for zone teperature control type remove thing types {}",
79 super.getSupportedThingTypes());
80 removeOlderResults(new Date().getTime());
84 * Method for the background discovery
86 * @see org.openhab.binding.digitalstrom.internal.lib.listener.TemperatureControlStatusListener#configChanged(TemperatureControlStatus)
87 * @param tempControlStatus can be null
89 public void configChanged(TemperatureControlStatus tempControlStatus) {
90 if (isBackgroundDiscoveryEnabled()) {
91 internalConfigChanged(tempControlStatus);
95 private void internalConfigChanged(TemperatureControlStatus tempControlStatus) {
96 if (tempControlStatus == null) {
99 if (tempControlStatus.isNotSetOff()) {
100 logger.debug("found configured zone TemperatureControlStatus = {}", tempControlStatus);
102 ThingUID thingUID = getThingUID(tempControlStatus);
103 if (thingUID != null) {
104 Map<String, Object> properties = new HashMap<>();
105 properties.put(DigitalSTROMBindingConstants.ZONE_ID, tempControlStatus.getZoneID());
106 String zoneName = tempControlStatus.getZoneName();
107 if (zoneName == null || zoneName.isBlank()) {
108 zoneName = tempControlStatus.getZoneID().toString();
110 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
111 .withBridge(bridgeUID).withLabel(zoneName).build();
112 thingDiscovered(discoveryResult);
118 private ThingUID getThingUID(TemperatureControlStatus tempControlStatus) {
119 ThingTypeUID thingTypeUID = new ThingTypeUID(BINDING_ID, thingTypeID);
120 if (getSupportedThingTypes().contains(thingTypeUID)) {
121 String thingID = tempControlStatus.getZoneID().toString();
122 return new ThingUID(thingTypeUID, bridgeUID, thingID);
129 * Returns the ID of this {@link ZoneTemperatureControlDiscoveryService}.
131 * @return id of the service
133 public String getID() {