]> git.basschouten.com Git - openhab-addons.git/blob
ef725c8ed5be38340a174f0f32dac5642fb55618
[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.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;
34
35 /**
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}
40  *
41  * @author Michael Ochel
42  * @author Matthias Siegele
43  */
44 public class ZoneTemperatureControlDiscoveryService extends AbstractDiscoveryService {
45
46     private final Logger logger = LoggerFactory.getLogger(ZoneTemperatureControlDiscoveryService.class);
47     BridgeHandler bridgeHandler;
48     private final ThingUID bridgeUID;
49     private final String thingTypeID;
50
51     public static final int TIMEOUT = 10;
52
53     /**
54      * Creates a new {@link ZoneTemperatureControlDiscoveryService}.
55      *
56      * @param bridgeHandler must not be null
57      * @param supportedThingType must not be null
58      * @throws IllegalArgumentException see {@link AbstractDiscoveryService#AbstractDiscoveryService(int)}
59      */
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();
66     }
67
68     @Override
69     protected void startScan() {
70         for (TemperatureControlStatus tempConStat : bridgeHandler.getTemperatureControlStatusFromAllZones()) {
71             internalConfigChanged(tempConStat);
72         }
73     }
74
75     @Override
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());
80     }
81
82     /**
83      * Method for the background discovery
84      *
85      * @see org.openhab.binding.digitalstrom.internal.lib.listener.TemperatureControlStatusListener#configChanged(TemperatureControlStatus)
86      * @param tempControlStatus can be null
87      */
88     public void configChanged(TemperatureControlStatus tempControlStatus) {
89         if (isBackgroundDiscoveryEnabled()) {
90             internalConfigChanged(tempControlStatus);
91         }
92     }
93
94     private void internalConfigChanged(TemperatureControlStatus tempControlStatus) {
95         if (tempControlStatus == null) {
96             return;
97         }
98         if (tempControlStatus.isNotSetOff()) {
99             logger.debug("found configured zone TemperatureControlStatus = {}", tempControlStatus);
100
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();
108                 }
109                 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
110                         .withBridge(bridgeUID).withLabel(zoneName).build();
111                 thingDiscovered(discoveryResult);
112
113             }
114         }
115     }
116
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);
122             return thingUID;
123         } else {
124             return null;
125         }
126     }
127
128     /**
129      * Returns the ID of this {@link ZoneTemperatureControlDiscoveryService}.
130      *
131      * @return id of the service
132      */
133     public String getID() {
134         return thingTypeID;
135     }
136 }