2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.warmup.internal.discovery;
15 import static org.openhab.binding.warmup.internal.WarmupBindingConstants.*;
17 import java.util.HashMap;
18 import java.util.HashSet;
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;
36 * The {@link WarmupDiscoveryService} is used to discover devices that are connected to a My Warmup account.
38 * @author James Melville - Initial contribution
40 @Component(scope = ServiceScope.PROTOTYPE, service = WarmupDiscoveryService.class)
42 public class WarmupDiscoveryService extends AbstractThingHandlerDiscoveryService<MyWarmupAccountHandler>
43 implements WarmupRefreshListener {
45 private @Nullable ThingUID bridgeUID;
47 public WarmupDiscoveryService() {
48 super(MyWarmupAccountHandler.class, DISCOVERABLE_THING_TYPES_UIDS, 5, false);
52 public void deactivate() {
56 public void startScan() {
57 removeOlderResults(getTimestampOfLastScan());
58 thingHandler.setDiscoveryService(this);
62 * Process device list and populate discovery list with things
64 * @param domain Data model representing all devices
67 public void refresh(@Nullable QueryResponseDTO domain) {
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);
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());
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());
95 discoveredThings.add(roomThingUID);