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.neato.internal;
15 import static org.openhab.binding.neato.internal.NeatoBindingConstants.*;
17 import java.util.Collections;
18 import java.util.HashMap;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.openhab.binding.neato.internal.discovery.NeatoAccountDiscoveryService;
26 import org.openhab.binding.neato.internal.handler.NeatoAccountHandler;
27 import org.openhab.binding.neato.internal.handler.NeatoHandler;
28 import org.openhab.core.config.discovery.DiscoveryService;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.ThingHandlerFactory;
36 import org.osgi.framework.ServiceRegistration;
37 import org.osgi.service.component.annotations.Component;
40 * The {@link NeatoHandlerFactory} is responsible for creating things and thing
43 * @author Patrik Wimnell - Initial contribution
44 * @author Jeff Lauterbach - Adding Bridge thing type
46 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.neato")
47 public class NeatoHandlerFactory extends BaseThingHandlerFactory {
49 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPE_UIDS = Collections
50 .unmodifiableSet(Stream.of(BRIDGE_TYPE_NEATOACCOUNT, THING_TYPE_VACUUMCLEANER).collect(Collectors.toSet()));
52 public static final Set<ThingTypeUID> DISCOVERABLE_THING_TYPE_UIDS = Collections
53 .singleton(THING_TYPE_VACUUMCLEANER);
55 private Map<ThingUID, ServiceRegistration<DiscoveryService>> discoveryServiceRegistrations = new HashMap<>();
58 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
59 return SUPPORTED_THING_TYPE_UIDS.contains(thingTypeUID);
63 protected ThingHandler createHandler(Thing thing) {
64 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
66 if (thingTypeUID.equals(THING_TYPE_VACUUMCLEANER)) {
67 return new NeatoHandler(thing);
68 } else if (thingTypeUID.equals(BRIDGE_TYPE_NEATOACCOUNT)) {
69 NeatoAccountHandler handler = new NeatoAccountHandler((Bridge) thing);
70 registerAccountDiscoveryService(handler);
78 protected void removeHandler(@NonNull ThingHandler thingHandler) {
79 ServiceRegistration<DiscoveryService> serviceRegistration = discoveryServiceRegistrations
80 .get(thingHandler.getThing().getUID());
82 if (serviceRegistration != null) {
83 serviceRegistration.unregister();
87 private void registerAccountDiscoveryService(NeatoAccountHandler handler) {
88 NeatoAccountDiscoveryService discoveryService = new NeatoAccountDiscoveryService(handler);
90 ServiceRegistration<DiscoveryService> serviceRegistration = this.bundleContext
91 .registerService(DiscoveryService.class, discoveryService, null);
93 discoveryServiceRegistrations.put(handler.getThing().getUID(), serviceRegistration);