2 * Copyright (c) 2010-2023 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.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;
37 * The {@link WarmupDiscoveryService} is used to discover devices that are connected to a My Warmup account.
39 * @author James Melville - Initial contribution
42 public class WarmupDiscoveryService extends AbstractDiscoveryService
43 implements DiscoveryService, ThingHandlerService, WarmupRefreshListener {
45 private @Nullable MyWarmupAccountHandler bridgeHandler;
46 private @Nullable ThingUID bridgeUID;
48 public WarmupDiscoveryService() {
49 super(DISCOVERABLE_THING_TYPES_UIDS, 5, false);
53 public void deactivate() {
57 public void startScan() {
58 final MyWarmupAccountHandler handler = bridgeHandler;
59 if (handler != null) {
60 removeOlderResults(getTimestampOfLastScan());
61 handler.setDiscoveryService(this);
66 * Process device list and populate discovery list with things
68 * @param domain Data model representing all devices
71 public void refresh(@Nullable QueryResponseDTO domain) {
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);
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());
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());
99 discoveredThings.add(roomThingUID);
105 public void setThingHandler(@Nullable final ThingHandler handler) {
106 if (handler instanceof MyWarmupAccountHandler) {
107 bridgeHandler = (MyWarmupAccountHandler) handler;
108 bridgeUID = handler.getThing().getUID();
110 bridgeHandler = null;
116 public @Nullable ThingHandler getThingHandler() {
117 return bridgeHandler;