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 = Set.of(THING_TYPE_VACUUMCLEANER);
54 private Map<ThingUID, ServiceRegistration<DiscoveryService>> discoveryServiceRegistrations = new HashMap<>();
57 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
58 return SUPPORTED_THING_TYPE_UIDS.contains(thingTypeUID);
62 protected ThingHandler createHandler(Thing thing) {
63 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
65 if (thingTypeUID.equals(THING_TYPE_VACUUMCLEANER)) {
66 return new NeatoHandler(thing);
67 } else if (thingTypeUID.equals(BRIDGE_TYPE_NEATOACCOUNT)) {
68 NeatoAccountHandler handler = new NeatoAccountHandler((Bridge) thing);
69 registerAccountDiscoveryService(handler);
77 protected void removeHandler(@NonNull ThingHandler thingHandler) {
78 ServiceRegistration<DiscoveryService> serviceRegistration = discoveryServiceRegistrations
79 .get(thingHandler.getThing().getUID());
81 if (serviceRegistration != null) {
82 serviceRegistration.unregister();
86 private void registerAccountDiscoveryService(NeatoAccountHandler handler) {
87 NeatoAccountDiscoveryService discoveryService = new NeatoAccountDiscoveryService(handler);
89 ServiceRegistration<DiscoveryService> serviceRegistration = this.bundleContext
90 .registerService(DiscoveryService.class, discoveryService, null);
92 discoveryServiceRegistrations.put(handler.getThing().getUID(), serviceRegistration);