]> git.basschouten.com Git - openhab-addons.git/blob
44edd256cea907a0ab265442c6dc71c5c656a6e0
[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.yamahareceiver.internal.discovery;
14
15 import static org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants.*;
16 import static org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants.Configs.CONFIG_ZONE;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants.Zone;
26 import org.openhab.binding.yamahareceiver.internal.handler.YamahaBridgeHandler;
27 import org.openhab.binding.yamahareceiver.internal.state.DeviceInformationState;
28 import org.openhab.core.config.discovery.AbstractDiscoveryService;
29 import org.openhab.core.config.discovery.DiscoveryResult;
30 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
31 import org.openhab.core.config.discovery.DiscoveryService;
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  * After the AVR bridge thing has been added and a connection could be established,
38  * the user is presented with the available zones.
39  *
40  * @author David Gräff - Initial contribution
41  * @author Tomasz Maruszak - Introduced config object
42  */
43 @NonNullByDefault
44 public class ZoneDiscoveryService extends AbstractDiscoveryService implements DiscoveryService, ThingHandlerService {
45
46     private @Nullable YamahaBridgeHandler handler;
47
48     /**
49      * Constructs a zone discovery service.
50      * Registers this zone discovery service programmatically.
51      * Call {@link ZoneDiscoveryService#destroy()} to unregister the service after use.
52      */
53     public ZoneDiscoveryService() {
54         super(ZONE_THING_TYPES_UIDS, 0, false);
55     }
56
57     @Override
58     public void activate() {
59         super.activate(null);
60     }
61
62     @Override
63     public void deactivate() {
64         super.deactivate();
65     }
66
67     @Override
68     protected void startScan() {
69     }
70
71     public static ThingUID zoneThing(ThingUID bridgeUid, String zoneName) {
72         return new ThingUID(ZONE_THING_TYPE, bridgeUid, zoneName);
73     }
74
75     /**
76      * The available zones are within the {@link DeviceInformationState}. Will will publish those
77      * as things via this discovery service instance.
78      *
79      * @param state The device information state
80      * @param bridgeUid The bridge UID
81      */
82     public void publishZones(DeviceInformationState state, ThingUID bridgeUid) {
83         // Create a copy of the list to avoid concurrent modification exceptions, because
84         // the state update takes place in another thread
85         List<Zone> zoneCopy = new ArrayList<>(state.zones);
86
87         for (Zone zone : zoneCopy) {
88             String zoneName = zone.name();
89             ThingUID uid = zoneThing(bridgeUid, zoneName);
90
91             Map<String, Object> properties = new HashMap<>();
92             properties.put(CONFIG_ZONE, zoneName);
93
94             DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(uid).withProperties(properties)
95                     .withLabel(state.name + " " + zoneName).withBridge(bridgeUid)
96                     .withRepresentationProperty(CONFIG_ZONE).build();
97
98             thingDiscovered(discoveryResult);
99         }
100     }
101
102     @Override
103     public void setThingHandler(@Nullable ThingHandler handler) {
104         if (handler instanceof YamahaBridgeHandler bridgeHandler) {
105             bridgeHandler.setZoneDiscoveryService(this);
106             this.handler = bridgeHandler;
107         }
108     }
109
110     @Override
111     public @Nullable ThingHandler getThingHandler() {
112         return handler;
113     }
114 }