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.yamahareceiver.internal.discovery;
15 import static org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants.*;
16 import static org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants.Configs.CONFIG_ZONE;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
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;
37 * After the AVR bridge thing has been added and a connection could be established,
38 * the user is presented with the available zones.
40 * @author David Gräff - Initial contribution
41 * @author Tomasz Maruszak - Introduced config object
44 public class ZoneDiscoveryService extends AbstractDiscoveryService implements DiscoveryService, ThingHandlerService {
46 private @Nullable YamahaBridgeHandler handler;
49 * Constructs a zone discovery service.
50 * Registers this zone discovery service programmatically.
51 * Call {@link ZoneDiscoveryService#destroy()} to unregister the service after use.
53 public ZoneDiscoveryService() {
54 super(ZONE_THING_TYPES_UIDS, 0, false);
58 public void activate() {
63 public void deactivate() {
68 protected void startScan() {
71 public static ThingUID zoneThing(ThingUID bridgeUid, String zoneName) {
72 return new ThingUID(ZONE_THING_TYPE, bridgeUid, zoneName);
76 * The available zones are within the {@link DeviceInformationState}. Will will publish those
77 * as things via this discovery service instance.
79 * @param state The device information state
80 * @param bridgeUid The bridge UID
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);
87 for (Zone zone : zoneCopy) {
88 String zoneName = zone.name();
89 ThingUID uid = zoneThing(bridgeUid, zoneName);
91 Map<String, Object> properties = new HashMap<>();
92 properties.put(CONFIG_ZONE, zoneName);
94 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(uid).withProperties(properties)
95 .withLabel(state.name + " " + zoneName).withBridge(bridgeUid)
96 .withRepresentationProperty(CONFIG_ZONE).build();
98 thingDiscovered(discoveryResult);
103 public void setThingHandler(@Nullable ThingHandler handler) {
104 if (handler instanceof YamahaBridgeHandler) {
105 this.handler = (YamahaBridgeHandler) handler;
106 this.handler.setZoneDiscoveryService(this);
111 public @Nullable ThingHandler getThingHandler() {