]> git.basschouten.com Git - openhab-addons.git/blob
2abe6f531012e29e3d9c5b2d1f0b6c0ab4fef713
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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
38  * {@link org.openhab.core.thing.ThingTypeUID}. The supported {@link org.openhab.core.thing.ThingTypeUID}
39  * can be found at
40  * {@link org.openhab.binding.digitalstrom.internal.handler.ZoneTemperatureControlHandler#SUPPORTED_THING_TYPES}
41  *
42  * @author Michael Ochel - Initial contribution
43  * @author Matthias Siegele - Initial contribution
44  */
45 public class ZoneTemperatureControlDiscoveryService extends AbstractDiscoveryService {
46
47     private final Logger logger = LoggerFactory.getLogger(ZoneTemperatureControlDiscoveryService.class);
48     BridgeHandler bridgeHandler;
49     private final ThingUID bridgeUID;
50     private final String thingTypeID;
51
52     public static final int TIMEOUT = 10;
53
54     /**
55      * Creates a new {@link ZoneTemperatureControlDiscoveryService}.
56      *
57      * @param bridgeHandler must not be null
58      * @param supportedThingType must not be null
59      * @throws IllegalArgumentException see {@link AbstractDiscoveryService#AbstractDiscoveryService(int)}
60      */
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();
67     }
68
69     @Override
70     protected void startScan() {
71         for (TemperatureControlStatus tempConStat : bridgeHandler.getTemperatureControlStatusFromAllZones()) {
72             internalConfigChanged(tempConStat);
73         }
74     }
75
76     @Override
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());
81     }
82
83     /**
84      * Method for the background discovery
85      *
86      * @see org.openhab.binding.digitalstrom.internal.lib.listener.TemperatureControlStatusListener#configChanged(TemperatureControlStatus)
87      * @param tempControlStatus can be null
88      */
89     public void configChanged(TemperatureControlStatus tempControlStatus) {
90         if (isBackgroundDiscoveryEnabled()) {
91             internalConfigChanged(tempControlStatus);
92         }
93     }
94
95     private void internalConfigChanged(TemperatureControlStatus tempControlStatus) {
96         if (tempControlStatus == null) {
97             return;
98         }
99         if (tempControlStatus.isNotSetOff()) {
100             logger.debug("found configured zone TemperatureControlStatus = {}", tempControlStatus);
101
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();
109                 }
110                 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
111                         .withBridge(bridgeUID).withLabel(zoneName).build();
112                 thingDiscovered(discoveryResult);
113
114             }
115         }
116     }
117
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);
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 }