2 * Copyright (c) 2010-2021 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.miele.internal;
15 import static org.openhab.binding.miele.internal.MieleBindingConstants.*;
17 import java.util.HashMap;
18 import java.util.Hashtable;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
24 import org.openhab.binding.miele.internal.discovery.MieleApplianceDiscoveryService;
25 import org.openhab.binding.miele.internal.handler.CoffeeMachineHandler;
26 import org.openhab.binding.miele.internal.handler.DishWasherHandler;
27 import org.openhab.binding.miele.internal.handler.FridgeFreezerHandler;
28 import org.openhab.binding.miele.internal.handler.FridgeHandler;
29 import org.openhab.binding.miele.internal.handler.HobHandler;
30 import org.openhab.binding.miele.internal.handler.HoodHandler;
31 import org.openhab.binding.miele.internal.handler.MieleApplianceHandler;
32 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler;
33 import org.openhab.binding.miele.internal.handler.OvenHandler;
34 import org.openhab.binding.miele.internal.handler.TumbleDryerHandler;
35 import org.openhab.binding.miele.internal.handler.WashingMachineHandler;
36 import org.openhab.core.config.core.Configuration;
37 import org.openhab.core.config.discovery.DiscoveryService;
38 import org.openhab.core.thing.Bridge;
39 import org.openhab.core.thing.Thing;
40 import org.openhab.core.thing.ThingTypeUID;
41 import org.openhab.core.thing.ThingUID;
42 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
43 import org.openhab.core.thing.binding.ThingHandler;
44 import org.openhab.core.thing.binding.ThingHandlerFactory;
45 import org.osgi.framework.ServiceRegistration;
46 import org.osgi.service.component.annotations.Component;
49 * The {@link MieleHandlerFactory} is responsible for creating things and thing
52 * @author Karel Goderis - Initial contribution
54 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.miele")
55 public class MieleHandlerFactory extends BaseThingHandlerFactory {
57 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
58 .concat(MieleBridgeHandler.SUPPORTED_THING_TYPES.stream(),
59 MieleApplianceHandler.SUPPORTED_THING_TYPES.stream())
60 .collect(Collectors.toSet());
62 private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
65 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
66 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
70 public Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID,
72 if (MieleBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
73 ThingUID mieleBridgeUID = getBridgeThingUID(thingTypeUID, thingUID, configuration);
74 return super.createThing(thingTypeUID, configuration, mieleBridgeUID, null);
76 if (MieleApplianceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
77 ThingUID mieleApplianceUID = getApplianceUID(thingTypeUID, thingUID, configuration, bridgeUID);
78 return super.createThing(thingTypeUID, configuration, mieleApplianceUID, bridgeUID);
80 throw new IllegalArgumentException(
81 "The thing type " + thingTypeUID + " is not supported by the miele binding.");
85 protected ThingHandler createHandler(Thing thing) {
86 if (MieleBridgeHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
87 MieleBridgeHandler handler = new MieleBridgeHandler((Bridge) thing);
88 registerApplianceDiscoveryService(handler);
90 } else if (MieleApplianceHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
91 if (thing.getThingTypeUID().equals(THING_TYPE_HOOD)) {
92 return new HoodHandler(thing);
94 if (thing.getThingTypeUID().equals(THING_TYPE_FRIDGEFREEZER)) {
95 return new FridgeFreezerHandler(thing);
97 if (thing.getThingTypeUID().equals(THING_TYPE_FRIDGE)) {
98 return new FridgeHandler(thing);
100 if (thing.getThingTypeUID().equals(THING_TYPE_OVEN)) {
101 return new OvenHandler(thing);
103 if (thing.getThingTypeUID().equals(THING_TYPE_HOB)) {
104 return new HobHandler(thing);
106 if (thing.getThingTypeUID().equals(THING_TYPE_WASHINGMACHINE)) {
107 return new WashingMachineHandler(thing);
109 if (thing.getThingTypeUID().equals(THING_TYPE_DRYER)) {
110 return new TumbleDryerHandler(thing);
112 if (thing.getThingTypeUID().equals(THING_TYPE_DISHWASHER)) {
113 return new DishWasherHandler(thing);
115 if (thing.getThingTypeUID().equals(THING_TYPE_COFFEEMACHINE)) {
116 return new CoffeeMachineHandler(thing);
123 private ThingUID getBridgeThingUID(ThingTypeUID thingTypeUID, ThingUID thingUID, Configuration configuration) {
124 if (thingUID == null) {
125 String hostID = (String) configuration.get(HOST);
126 thingUID = new ThingUID(thingTypeUID, hostID);
131 private ThingUID getApplianceUID(ThingTypeUID thingTypeUID, ThingUID thingUID, Configuration configuration,
132 ThingUID bridgeUID) {
133 String applianceId = (String) configuration.get(APPLIANCE_ID);
135 if (thingUID == null) {
136 thingUID = new ThingUID(thingTypeUID, applianceId, bridgeUID.getId());
141 private synchronized void registerApplianceDiscoveryService(MieleBridgeHandler bridgeHandler) {
142 MieleApplianceDiscoveryService discoveryService = new MieleApplianceDiscoveryService(bridgeHandler);
143 discoveryService.activate();
144 this.discoveryServiceRegs.put(bridgeHandler.getThing().getUID(),
145 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
149 protected synchronized void removeHandler(ThingHandler thingHandler) {
150 if (thingHandler instanceof MieleBridgeHandler) {
151 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
152 if (serviceReg != null) {
153 // remove discovery service, if bridge handler is removed
154 MieleApplianceDiscoveryService service = (MieleApplianceDiscoveryService) bundleContext
155 .getService(serviceReg.getReference());
156 serviceReg.unregister();
157 if (service != null) {
158 service.deactivate();