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.pioneeravr.internal.discovery;
15 import java.util.HashMap;
16 import java.util.HashSet;
20 import org.jupnp.model.meta.RemoteDevice;
21 import org.openhab.binding.pioneeravr.internal.PioneerAvrBindingConstants;
22 import org.openhab.core.config.discovery.DiscoveryResult;
23 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
24 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
25 import org.openhab.core.thing.ThingTypeUID;
26 import org.openhab.core.thing.ThingUID;
27 import org.osgi.service.component.ComponentContext;
28 import org.osgi.service.component.annotations.Activate;
29 import org.osgi.service.component.annotations.Component;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * An UpnpDiscoveryParticipant which allows to discover Pioneer AVRs.
36 * @author Antoine Besnard - Initial contribution
39 public class PioneerAvrDiscoveryParticipant implements UpnpDiscoveryParticipant {
41 private final Logger logger = LoggerFactory.getLogger(PioneerAvrDiscoveryParticipant.class);
43 private boolean isAutoDiscoveryEnabled;
44 private Set<ThingTypeUID> supportedThingTypes;
46 public PioneerAvrDiscoveryParticipant() {
47 this.isAutoDiscoveryEnabled = true;
48 this.supportedThingTypes = PioneerAvrBindingConstants.SUPPORTED_THING_TYPES_UIDS;
52 * Called at the service activation.
54 * @param componentContext
57 protected void activate(ComponentContext componentContext) {
58 if (componentContext.getProperties() != null) {
59 String autoDiscoveryPropertyValue = (String) componentContext.getProperties().get("enableAutoDiscovery");
60 if (autoDiscoveryPropertyValue != null && !autoDiscoveryPropertyValue.isEmpty()) {
61 isAutoDiscoveryEnabled = Boolean.valueOf(autoDiscoveryPropertyValue);
64 supportedThingTypes = isAutoDiscoveryEnabled ? PioneerAvrBindingConstants.SUPPORTED_THING_TYPES_UIDS
69 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
70 return supportedThingTypes;
74 public DiscoveryResult createResult(RemoteDevice device) {
75 DiscoveryResult result = null;
76 ThingUID thingUid = getThingUID(device);
77 if (thingUid != null) {
78 String friendlyName = device.getDetails().getFriendlyName();
79 String label = friendlyName == null || friendlyName.isEmpty() ? device.getDisplayString() : friendlyName;
80 Map<String, Object> properties = new HashMap<>(2, 1);
81 properties.put(PioneerAvrBindingConstants.HOST_PARAMETER,
82 device.getIdentity().getDescriptorURL().getHost());
83 properties.put(PioneerAvrBindingConstants.PROTOCOL_PARAMETER, PioneerAvrBindingConstants.IP_PROTOCOL_NAME);
85 result = DiscoveryResultBuilder.create(thingUid).withLabel(label).withProperties(properties).build();
92 public ThingUID getThingUID(RemoteDevice device) {
93 ThingUID result = null;
94 if (isAutoDiscoveryEnabled) {
95 String manufacturer = device.getDetails().getManufacturerDetails().getManufacturer();
96 if (manufacturer != null
97 && manufacturer.toLowerCase().contains(PioneerAvrBindingConstants.MANUFACTURER.toLowerCase())) {
98 logger.debug("Manufacturer matched: search: {}, device value: {}.",
99 PioneerAvrBindingConstants.MANUFACTURER, manufacturer);
100 String type = device.getType().getType();
102 && type.toLowerCase().contains(PioneerAvrBindingConstants.UPNP_DEVICE_TYPE.toLowerCase())) {
103 logger.debug("Device type matched: search: {}, device value: {}.",
104 PioneerAvrBindingConstants.UPNP_DEVICE_TYPE, type);
106 String deviceModel = device.getDetails().getModelDetails() != null
107 ? device.getDetails().getModelDetails().getModelName()
110 ThingTypeUID thingTypeUID = PioneerAvrBindingConstants.IP_AVR_THING_TYPE;
112 if (isSupportedDeviceModel(deviceModel, PioneerAvrBindingConstants.SUPPORTED_DEVICE_MODELS2020)) {
113 thingTypeUID = PioneerAvrBindingConstants.IP_AVR_THING_TYPE2020;
114 } else if (isSupportedDeviceModel(deviceModel,
115 PioneerAvrBindingConstants.SUPPORTED_DEVICE_MODELS2019)) {
116 thingTypeUID = PioneerAvrBindingConstants.IP_AVR_THING_TYPE2019;
117 } else if (isSupportedDeviceModel(deviceModel,
118 PioneerAvrBindingConstants.SUPPORTED_DEVICE_MODELS2018)) {
119 thingTypeUID = PioneerAvrBindingConstants.IP_AVR_THING_TYPE2018;
120 } else if (isSupportedDeviceModel(deviceModel,
121 PioneerAvrBindingConstants.SUPPORTED_DEVICE_MODELS2017)) {
122 thingTypeUID = PioneerAvrBindingConstants.IP_AVR_THING_TYPE2017;
123 } else if (isSupportedDeviceModel(deviceModel,
124 PioneerAvrBindingConstants.SUPPORTED_DEVICE_MODELS2016)) {
125 thingTypeUID = PioneerAvrBindingConstants.IP_AVR_THING_TYPE2016;
126 } else if (isSupportedDeviceModel(deviceModel,
127 PioneerAvrBindingConstants.SUPPORTED_DEVICE_MODELS2015)) {
128 thingTypeUID = PioneerAvrBindingConstants.IP_AVR_THING_TYPE2015;
129 } else if (isSupportedDeviceModel(deviceModel,
130 PioneerAvrBindingConstants.SUPPORTED_DEVICE_MODELS2014)) {
131 thingTypeUID = PioneerAvrBindingConstants.IP_AVR_THING_TYPE2014;
132 } else if (!isSupportedDeviceModel(deviceModel,
133 PioneerAvrBindingConstants.SUPPORTED_DEVICE_MODELS)) {
134 logger.debug("Device model {} not supported. Odd behaviors may happen.", deviceModel);
135 thingTypeUID = PioneerAvrBindingConstants.IP_AVR_UNSUPPORTED_THING_TYPE;
138 result = new ThingUID(thingTypeUID, device.getIdentity().getUdn().getIdentifierString());
147 * Return true only if the given device model is supported.
152 private boolean isSupportedDeviceModel(String deviceModel, Set<String> supportedDeviceModels) {
153 return deviceModel != null && !deviceModel.isBlank() && supportedDeviceModels.stream()
154 .anyMatch(input -> deviceModel.toLowerCase().startsWith(input.toLowerCase()));