]> git.basschouten.com Git - openhab-addons.git/blob
b43fa60b58e5e2c318d65ca1ab8c272018bd7ddf
[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.warmup.internal.discovery;
14
15 import static org.openhab.binding.warmup.internal.WarmupBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.Map;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.warmup.internal.handler.MyWarmupAccountHandler;
24 import org.openhab.binding.warmup.internal.handler.WarmupRefreshListener;
25 import org.openhab.binding.warmup.internal.model.query.LocationDTO;
26 import org.openhab.binding.warmup.internal.model.query.QueryResponseDTO;
27 import org.openhab.binding.warmup.internal.model.query.RoomDTO;
28 import org.openhab.core.config.discovery.AbstractDiscoveryService;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.config.discovery.DiscoveryService;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingUID;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerService;
35
36 /**
37  * The {@link WarmupDiscoveryService} is used to discover devices that are connected to a My Warmup account.
38  *
39  * @author James Melville - Initial contribution
40  */
41 @NonNullByDefault
42 public class WarmupDiscoveryService extends AbstractDiscoveryService
43         implements DiscoveryService, ThingHandlerService, WarmupRefreshListener {
44
45     private @Nullable MyWarmupAccountHandler bridgeHandler;
46     private @Nullable ThingUID bridgeUID;
47
48     public WarmupDiscoveryService() {
49         super(DISCOVERABLE_THING_TYPES_UIDS, 5, false);
50     }
51
52     @Override
53     public void deactivate() {
54     }
55
56     @Override
57     public void startScan() {
58         final MyWarmupAccountHandler handler = bridgeHandler;
59         if (handler != null) {
60             removeOlderResults(getTimestampOfLastScan());
61             handler.setDiscoveryService(this);
62         }
63     }
64
65     /**
66      * Process device list and populate discovery list with things
67      *
68      * @param domain Data model representing all devices
69      */
70     @Override
71     public void refresh(@Nullable QueryResponseDTO domain) {
72         if (domain != null) {
73             HashSet<ThingUID> discoveredThings = new HashSet<ThingUID>();
74             for (LocationDTO location : domain.getData().getUser().getLocations()) {
75                 for (RoomDTO room : location.getRooms()) {
76                     discoverRoom(location, room, discoveredThings);
77                 }
78             }
79         }
80     }
81
82     private void discoverRoom(LocationDTO location, RoomDTO room, HashSet<ThingUID> discoveredThings) {
83         if (room.getThermostat4ies() != null && !room.getThermostat4ies().isEmpty()) {
84             final String deviceSN = room.getThermostat4ies().get(0).getDeviceSN();
85             ThingUID localBridgeUID = this.bridgeUID;
86             if (localBridgeUID != null && deviceSN != null) {
87                 final Map<String, Object> roomProperties = new HashMap<>();
88                 roomProperties.put(Thing.PROPERTY_SERIAL_NUMBER, deviceSN);
89                 roomProperties.put(PROPERTY_ROOM_ID, room.getId());
90                 roomProperties.put(PROPERTY_ROOM_NAME, room.getName());
91                 roomProperties.put(PROPERTY_LOCATION_ID, location.getId());
92                 roomProperties.put(PROPERTY_LOCATION_NAME, location.getName());
93
94                 ThingUID roomThingUID = new ThingUID(THING_TYPE_ROOM, localBridgeUID, deviceSN);
95                 thingDiscovered(DiscoveryResultBuilder.create(roomThingUID).withBridge(localBridgeUID)
96                         .withProperties(roomProperties).withLabel(location.getName() + " - " + room.getName())
97                         .withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER).build());
98
99                 discoveredThings.add(roomThingUID);
100             }
101         }
102     }
103
104     @Override
105     public void setThingHandler(@Nullable final ThingHandler handler) {
106         if (handler instanceof MyWarmupAccountHandler accountHandler) {
107             bridgeHandler = accountHandler;
108             bridgeUID = handler.getThing().getUID();
109         } else {
110             bridgeHandler = null;
111             bridgeUID = null;
112         }
113     }
114
115     @Override
116     public @Nullable ThingHandler getThingHandler() {
117         return bridgeHandler;
118     }
119 }