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.handler.ZoneTemperatureControlHandler;
26 import org.openhab.binding.digitalstrom.internal.lib.climate.jsonresponsecontainer.impl.TemperatureControlStatus;
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.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The {@link ZoneTemperatureControlDiscoveryService} discovers all digitalSTROM zones which have temperature control
37 * configured. The thing-type has to be given to the
38 * {@link #ZoneTemperatureControlDiscoveryService(BridgeHandler, ThingTypeUID)} as {@link ThingTypeUID}. The supported
39 * {@link ThingTypeUID} can be found at {@link ZoneTemperatureControlHandler#SUPPORTED_THING_TYPES}
41 * @author Michael Ochel
42 * @author Matthias Siegele
44 public class ZoneTemperatureControlDiscoveryService extends AbstractDiscoveryService {
46 private final Logger logger = LoggerFactory.getLogger(ZoneTemperatureControlDiscoveryService.class);
47 BridgeHandler bridgeHandler;
48 private final ThingUID bridgeUID;
49 private final String thingTypeID;
51 public static final int TIMEOUT = 10;
54 * Creates a new {@link ZoneTemperatureControlDiscoveryService}.
56 * @param bridgeHandler must not be null
57 * @param supportedThingType must not be null
58 * @throws IllegalArgumentException see {@link AbstractDiscoveryService#AbstractDiscoveryService(int)}
60 public ZoneTemperatureControlDiscoveryService(BridgeHandler bridgeHandler, ThingTypeUID supportedThingType)
61 throws IllegalArgumentException {
62 super(new HashSet<>(Arrays.asList(supportedThingType)), TIMEOUT, true);
63 bridgeUID = bridgeHandler.getThing().getUID();
64 this.bridgeHandler = bridgeHandler;
65 thingTypeID = supportedThingType.getId();
69 protected void startScan() {
70 for (TemperatureControlStatus tempConStat : bridgeHandler.getTemperatureControlStatusFromAllZones()) {
71 internalConfigChanged(tempConStat);
76 public void deactivate() {
77 logger.debug("Deactivate discovery service for zone teperature control type remove thing types {}",
78 super.getSupportedThingTypes());
79 removeOlderResults(new Date().getTime());
83 * Method for the background discovery
85 * @see org.openhab.binding.digitalstrom.internal.lib.listener.TemperatureControlStatusListener#configChanged(TemperatureControlStatus)
86 * @param tempControlStatus can be null
88 public void configChanged(TemperatureControlStatus tempControlStatus) {
89 if (isBackgroundDiscoveryEnabled()) {
90 internalConfigChanged(tempControlStatus);
94 private void internalConfigChanged(TemperatureControlStatus tempControlStatus) {
95 if (tempControlStatus == null) {
98 if (tempControlStatus.isNotSetOff()) {
99 logger.debug("found configured zone TemperatureControlStatus = {}", tempControlStatus);
101 ThingUID thingUID = getThingUID(tempControlStatus);
102 if (thingUID != null) {
103 Map<String, Object> properties = new HashMap<>();
104 properties.put(DigitalSTROMBindingConstants.ZONE_ID, tempControlStatus.getZoneID());
105 String zoneName = tempControlStatus.getZoneName();
106 if (zoneName == null || zoneName.isBlank()) {
107 zoneName = tempControlStatus.getZoneID().toString();
109 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
110 .withBridge(bridgeUID).withLabel(zoneName).build();
111 thingDiscovered(discoveryResult);
117 private ThingUID getThingUID(TemperatureControlStatus tempControlStatus) {
118 ThingTypeUID thingTypeUID = new ThingTypeUID(BINDING_ID, thingTypeID);
119 if (getSupportedThingTypes().contains(thingTypeUID)) {
120 String thingID = tempControlStatus.getZoneID().toString();
121 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, thingID);
129 * Returns the ID of this {@link ZoneTemperatureControlDiscoveryService}.
131 * @return id of the service
133 public String getID() {