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.amplipi.internal.discovery;
15 import java.util.List;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.amplipi.internal.AmpliPiBindingConstants;
21 import org.openhab.binding.amplipi.internal.AmpliPiHandler;
22 import org.openhab.binding.amplipi.internal.AmpliPiStatusChangeListener;
23 import org.openhab.binding.amplipi.internal.model.Group;
24 import org.openhab.binding.amplipi.internal.model.Status;
25 import org.openhab.binding.amplipi.internal.model.Zone;
26 import org.openhab.core.config.discovery.AbstractDiscoveryService;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.thing.ThingUID;
30 import org.openhab.core.thing.binding.ThingHandler;
31 import org.openhab.core.thing.binding.ThingHandlerService;
34 * This class discoveres the available zones and groups of the AmpliPi system.
36 * @author Kai Kreuzer - Initial contribution
40 public class AmpliPiZoneAndGroupDiscoveryService extends AbstractDiscoveryService
41 implements ThingHandlerService, AmpliPiStatusChangeListener {
43 private static final int TIMEOUT = 10;
45 private @Nullable AmpliPiHandler handler;
46 private List<Zone> zones = List.of();
47 private List<Group> groups = List.of();
49 public AmpliPiZoneAndGroupDiscoveryService() throws IllegalArgumentException {
50 super(Set.of(AmpliPiBindingConstants.THING_TYPE_GROUP, AmpliPiBindingConstants.THING_TYPE_ZONE), TIMEOUT, true);
54 public void setThingHandler(ThingHandler handler) {
55 AmpliPiHandler ampliPiHander = (AmpliPiHandler) handler;
56 ampliPiHander.addStatusChangeListener(this);
57 this.handler = ampliPiHander;
61 public @Nullable ThingHandler getThingHandler() {
66 protected void startScan() {
67 for (Zone z : zones) {
68 if (!z.getDisabled()) {
72 for (Group g : groups) {
77 private void createZone(Zone z) {
78 if (handler != null) {
79 ThingUID bridgeUID = handler.getThing().getUID();
80 ThingUID uid = new ThingUID(AmpliPiBindingConstants.THING_TYPE_ZONE, bridgeUID, z.getId().toString());
81 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withLabel("AmpliPi Zone '" + z.getName() + "'")
82 .withProperty(AmpliPiBindingConstants.CFG_PARAM_ID, z.getId()).withBridge(bridgeUID)
83 .withRepresentationProperty(AmpliPiBindingConstants.CFG_PARAM_ID).build();
84 thingDiscovered(result);
88 private void createGroup(Group g) {
89 if (handler != null) {
90 ThingUID bridgeUID = handler.getThing().getUID();
91 ThingUID uid = new ThingUID(AmpliPiBindingConstants.THING_TYPE_GROUP, bridgeUID, g.getId().toString());
92 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withLabel("AmpliPi Group '" + g.getName() + "'")
93 .withProperty(AmpliPiBindingConstants.CFG_PARAM_ID, g.getId()).withBridge(bridgeUID)
94 .withRepresentationProperty(AmpliPiBindingConstants.CFG_PARAM_ID).build();
95 thingDiscovered(result);
100 public void deactivate() {
101 if (handler != null) {
102 handler.removeStatusChangeListener(this);
108 public void receive(Status status) {
109 zones = status.getZones();
110 groups = status.getGroups();
111 if (isBackgroundDiscoveryEnabled()) {