]> git.basschouten.com Git - openhab-addons.git/blob
7b57652b7cefca23d3920a593d8f3e5e6dd4dcab
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.digitalstrom.internal.discovery;
14
15 import static org.openhab.binding.digitalstrom.internal.DigitalSTROMBindingConstants.BINDING_ID;
16
17 import java.util.Arrays;
18 import java.util.Date;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Map;
22
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;
33
34 /**
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}
39  *
40  * @author Michael Ochel - Initial contribution
41  * @author Matthias Siegele - Initial contribution
42  */
43 public class ZoneTemperatureControlDiscoveryService extends AbstractDiscoveryService {
44
45     private final Logger logger = LoggerFactory.getLogger(ZoneTemperatureControlDiscoveryService.class);
46     BridgeHandler bridgeHandler;
47     private final ThingUID bridgeUID;
48     private final String thingTypeID;
49
50     public static final int TIMEOUT = 10;
51
52     /**
53      * Creates a new {@link ZoneTemperatureControlDiscoveryService}.
54      *
55      * @param bridgeHandler must not be null
56      * @param supportedThingType must not be null
57      * @throws IllegalArgumentException see {@link AbstractDiscoveryService#AbstractDiscoveryService(int)}
58      */
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();
65     }
66
67     @Override
68     protected void startScan() {
69         for (TemperatureControlStatus tempConStat : bridgeHandler.getTemperatureControlStatusFromAllZones()) {
70             internalConfigChanged(tempConStat);
71         }
72     }
73
74     @Override
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());
79     }
80
81     /**
82      * Method for the background discovery
83      *
84      * @see org.openhab.binding.digitalstrom.internal.lib.listener.TemperatureControlStatusListener#configChanged(TemperatureControlStatus)
85      * @param tempControlStatus can be null
86      */
87     public void configChanged(TemperatureControlStatus tempControlStatus) {
88         if (isBackgroundDiscoveryEnabled()) {
89             internalConfigChanged(tempControlStatus);
90         }
91     }
92
93     private void internalConfigChanged(TemperatureControlStatus tempControlStatus) {
94         if (tempControlStatus == null) {
95             return;
96         }
97         if (tempControlStatus.isNotSetOff()) {
98             logger.debug("found configured zone TemperatureControlStatus = {}", tempControlStatus);
99
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();
107                 }
108                 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
109                         .withBridge(bridgeUID).withLabel(zoneName).build();
110                 thingDiscovered(discoveryResult);
111
112             }
113         }
114     }
115
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             ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, thingID);
121             return thingUID;
122         } else {
123             return null;
124         }
125     }
126
127     /**
128      * Returns the ID of this {@link ZoneTemperatureControlDiscoveryService}.
129      *
130      * @return id of the service
131      */
132     public String getID() {
133         return thingTypeID;
134     }
135 }