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.pulseaudio.internal;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Hashtable;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.pulseaudio.internal.discovery.PulseaudioDeviceDiscoveryService;
26 import org.openhab.binding.pulseaudio.internal.handler.PulseaudioBridgeHandler;
27 import org.openhab.binding.pulseaudio.internal.handler.PulseaudioHandler;
28 import org.openhab.core.config.core.Configuration;
29 import org.openhab.core.config.discovery.DiscoveryService;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.thing.binding.ThingHandlerFactory;
37 import org.osgi.framework.ServiceRegistration;
38 import org.osgi.service.component.ComponentContext;
39 import org.osgi.service.component.annotations.Activate;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Modified;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * The {@link PulseaudioHandlerFactory} is responsible for creating things and thing
49 * @author Tobias Bräutigam - Initial contribution
51 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.pulseaudio")
53 public class PulseaudioHandlerFactory extends BaseThingHandlerFactory {
54 private final Logger logger = LoggerFactory.getLogger(PulseaudioHandlerFactory.class);
56 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
57 .unmodifiableSet(Stream.concat(PulseaudioBridgeHandler.SUPPORTED_THING_TYPES_UIDS.stream(),
58 PulseaudioHandler.SUPPORTED_THING_TYPES_UIDS.stream()).collect(Collectors.toSet()));
60 private final Map<ThingHandler, ServiceRegistration<?>> discoveryServiceReg = new HashMap<>();
62 private PulseAudioBindingConfiguration configuration = new PulseAudioBindingConfiguration();
65 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
66 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
70 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
71 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
72 if (PulseaudioBridgeHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
73 return super.createThing(thingTypeUID, configuration, thingUID, null);
75 if (PulseaudioHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
76 ThingUID deviceUID = getPulseaudioDeviceUID(thingTypeUID, thingUID, configuration, bridgeUID);
77 return super.createThing(thingTypeUID, configuration, deviceUID, bridgeUID);
79 throw new IllegalArgumentException("The thing type " + thingTypeUID + " is not supported by the binding.");
82 private void registerDeviceDiscoveryService(PulseaudioBridgeHandler paBridgeHandler) {
83 PulseaudioDeviceDiscoveryService discoveryService = new PulseaudioDeviceDiscoveryService(paBridgeHandler);
84 discoveryService.activate();
85 this.discoveryServiceReg.put(paBridgeHandler,
86 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
89 private ThingUID getPulseaudioDeviceUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
90 Configuration configuration, @Nullable ThingUID bridgeUID) {
91 if (thingUID == null) {
92 String name = (String) configuration.get(PulseaudioBindingConstants.DEVICE_PARAMETER_NAME_OR_DESCRIPTION);
93 return new ThingUID(thingTypeUID, name, bridgeUID == null ? null : bridgeUID.getId());
99 protected void removeHandler(ThingHandler thingHandler) {
100 ServiceRegistration<?> serviceRegistration = this.discoveryServiceReg.get(thingHandler);
101 if (serviceRegistration != null) {
102 PulseaudioDeviceDiscoveryService service = (PulseaudioDeviceDiscoveryService) bundleContext
103 .getService(serviceRegistration.getReference());
104 if (service != null) {
105 service.deactivate();
107 serviceRegistration.unregister();
109 discoveryServiceReg.remove(thingHandler);
110 super.removeHandler(thingHandler);
114 protected @Nullable ThingHandler createHandler(Thing thing) {
115 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
117 if (PulseaudioBridgeHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
118 PulseaudioBridgeHandler handler = new PulseaudioBridgeHandler((Bridge) thing, configuration);
119 registerDeviceDiscoveryService(handler);
121 } else if (PulseaudioHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
122 return new PulseaudioHandler(thing, bundleContext);
128 // The activate component call is used to access the bindings configuration
130 protected synchronized void activate(ComponentContext componentContext, Map<String, Object> config) {
131 super.activate(componentContext);
136 protected void modified(Map<String, Object> config) {
137 configuration.update(new Configuration(config).as(PulseAudioBindingConfiguration.class));
138 logger.debug("pulseaudio configuration update received ({})", config);