]> git.basschouten.com Git - openhab-addons.git/blob
f41a61526090cca29c2fe4914dd6bac7464c5d32
[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.AbstractThingHandlerDiscoveryService;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingUID;
32 import org.osgi.service.component.annotations.Component;
33 import org.osgi.service.component.annotations.ServiceScope;
34
35 /**
36  * The {@link WarmupDiscoveryService} is used to discover devices that are connected to a My Warmup account.
37  *
38  * @author James Melville - Initial contribution
39  */
40 @Component(scope = ServiceScope.PROTOTYPE, service = WarmupDiscoveryService.class)
41 @NonNullByDefault
42 public class WarmupDiscoveryService extends AbstractThingHandlerDiscoveryService<MyWarmupAccountHandler>
43         implements WarmupRefreshListener {
44
45     private @Nullable ThingUID bridgeUID;
46
47     public WarmupDiscoveryService() {
48         super(MyWarmupAccountHandler.class, DISCOVERABLE_THING_TYPES_UIDS, 5, false);
49     }
50
51     @Override
52     public void deactivate() {
53     }
54
55     @Override
56     public void startScan() {
57         removeOlderResults(getTimestampOfLastScan());
58         thingHandler.setDiscoveryService(this);
59     }
60
61     /**
62      * Process device list and populate discovery list with things
63      *
64      * @param domain Data model representing all devices
65      */
66     @Override
67     public void refresh(@Nullable QueryResponseDTO domain) {
68         if (domain != null) {
69             HashSet<ThingUID> discoveredThings = new HashSet<>();
70             for (LocationDTO location : domain.getData().getUser().getLocations()) {
71                 for (RoomDTO room : location.getRooms()) {
72                     discoverRoom(location, room, discoveredThings);
73                 }
74             }
75         }
76     }
77
78     private void discoverRoom(LocationDTO location, RoomDTO room, HashSet<ThingUID> discoveredThings) {
79         if (room.getThermostat4ies() != null && !room.getThermostat4ies().isEmpty()) {
80             final String deviceSN = room.getThermostat4ies().get(0).getDeviceSN();
81             ThingUID localBridgeUID = this.bridgeUID;
82             if (localBridgeUID != null && deviceSN != null) {
83                 final Map<String, Object> roomProperties = new HashMap<>();
84                 roomProperties.put(Thing.PROPERTY_SERIAL_NUMBER, deviceSN);
85                 roomProperties.put(PROPERTY_ROOM_ID, room.getId());
86                 roomProperties.put(PROPERTY_ROOM_NAME, room.getName());
87                 roomProperties.put(PROPERTY_LOCATION_ID, location.getId());
88                 roomProperties.put(PROPERTY_LOCATION_NAME, location.getName());
89
90                 ThingUID roomThingUID = new ThingUID(THING_TYPE_ROOM, localBridgeUID, deviceSN);
91                 thingDiscovered(DiscoveryResultBuilder.create(roomThingUID).withBridge(localBridgeUID)
92                         .withProperties(roomProperties).withLabel(location.getName() + " - " + room.getName())
93                         .withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER).build());
94
95                 discoveredThings.add(roomThingUID);
96             }
97         }
98     }
99 }