]> git.basschouten.com Git - openhab-addons.git/blob
d46f40c0f4dde73beab0106a10274b69288eb21f
[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.amplipi.internal.discovery;
14
15 import java.util.List;
16 import java.util.Set;
17
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;
32
33 /**
34  * This class discoveres the available zones and groups of the AmpliPi system.
35  *
36  * @author Kai Kreuzer - Initial contribution
37  *
38  */
39 @NonNullByDefault
40 public class AmpliPiZoneAndGroupDiscoveryService extends AbstractDiscoveryService
41         implements ThingHandlerService, AmpliPiStatusChangeListener {
42
43     private static final int TIMEOUT = 10;
44
45     private @Nullable AmpliPiHandler handler;
46     private List<Zone> zones = List.of();
47     private List<Group> groups = List.of();
48
49     public AmpliPiZoneAndGroupDiscoveryService() throws IllegalArgumentException {
50         super(Set.of(AmpliPiBindingConstants.THING_TYPE_GROUP, AmpliPiBindingConstants.THING_TYPE_ZONE), TIMEOUT, true);
51     }
52
53     @Override
54     public void setThingHandler(ThingHandler handler) {
55         AmpliPiHandler ampliPiHander = (AmpliPiHandler) handler;
56         ampliPiHander.addStatusChangeListener(this);
57         this.handler = ampliPiHander;
58     }
59
60     @Override
61     public @Nullable ThingHandler getThingHandler() {
62         return handler;
63     }
64
65     @Override
66     protected void startScan() {
67         for (Zone z : zones) {
68             if (!z.getDisabled()) {
69                 createZone(z);
70             }
71         }
72         for (Group g : groups) {
73             createGroup(g);
74         }
75     }
76
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);
85         }
86     }
87
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);
96         }
97     }
98
99     @Override
100     public void deactivate() {
101         if (handler != null) {
102             handler.removeStatusChangeListener(this);
103         }
104         super.deactivate();
105     }
106
107     @Override
108     public void receive(Status status) {
109         zones = status.getZones();
110         groups = status.getGroups();
111         if (isBackgroundDiscoveryEnabled()) {
112             startScan();
113         }
114     }
115 }