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.i18n.LocaleProvider;
39 import org.openhab.core.i18n.TranslationProvider;
40 import org.openhab.core.thing.Bridge;
41 import org.openhab.core.thing.Thing;
42 import org.openhab.core.thing.ThingTypeUID;
43 import org.openhab.core.thing.ThingUID;
44 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
45 import org.openhab.core.thing.binding.ThingHandler;
46 import org.openhab.core.thing.binding.ThingHandlerFactory;
47 import org.osgi.framework.ServiceRegistration;
48 import org.osgi.service.component.ComponentContext;
49 import org.osgi.service.component.annotations.Activate;
50 import org.osgi.service.component.annotations.Component;
51 import org.osgi.service.component.annotations.Reference;
54 * The {@link MieleHandlerFactory} is responsible for creating things and thing
57 * @author Karel Goderis - Initial contribution
59 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.miele")
60 public class MieleHandlerFactory extends BaseThingHandlerFactory {
62 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
63 .concat(MieleBridgeHandler.SUPPORTED_THING_TYPES.stream(),
64 MieleApplianceHandler.SUPPORTED_THING_TYPES.stream())
65 .collect(Collectors.toSet());
67 private final TranslationProvider i18nProvider;
68 private final LocaleProvider localeProvider;
70 private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
73 public MieleHandlerFactory(final @Reference TranslationProvider i18nProvider,
74 final @Reference LocaleProvider localeProvider, ComponentContext componentContext) {
75 this.i18nProvider = i18nProvider;
76 this.localeProvider = localeProvider;
80 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
81 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
85 public Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID,
87 if (MieleBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
88 ThingUID mieleBridgeUID = getBridgeThingUID(thingTypeUID, thingUID, configuration);
89 return super.createThing(thingTypeUID, configuration, mieleBridgeUID, null);
91 if (MieleApplianceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
92 ThingUID mieleApplianceUID = getApplianceUID(thingTypeUID, thingUID, configuration, bridgeUID);
93 return super.createThing(thingTypeUID, configuration, mieleApplianceUID, bridgeUID);
95 throw new IllegalArgumentException(
96 "The thing type " + thingTypeUID + " is not supported by the miele binding.");
100 protected ThingHandler createHandler(Thing thing) {
101 if (MieleBridgeHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
102 MieleBridgeHandler handler = new MieleBridgeHandler((Bridge) thing);
103 registerApplianceDiscoveryService(handler);
105 } else if (MieleApplianceHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
106 if (thing.getThingTypeUID().equals(THING_TYPE_HOOD)) {
107 return new HoodHandler(thing, i18nProvider, localeProvider);
109 if (thing.getThingTypeUID().equals(THING_TYPE_FRIDGEFREEZER)) {
110 return new FridgeFreezerHandler(thing, i18nProvider, localeProvider);
112 if (thing.getThingTypeUID().equals(THING_TYPE_FRIDGE)) {
113 return new FridgeHandler(thing, i18nProvider, localeProvider);
115 if (thing.getThingTypeUID().equals(THING_TYPE_OVEN)) {
116 return new OvenHandler(thing, i18nProvider, localeProvider);
118 if (thing.getThingTypeUID().equals(THING_TYPE_HOB)) {
119 return new HobHandler(thing, i18nProvider, localeProvider);
121 if (thing.getThingTypeUID().equals(THING_TYPE_WASHINGMACHINE)) {
122 return new WashingMachineHandler(thing, i18nProvider, localeProvider);
124 if (thing.getThingTypeUID().equals(THING_TYPE_DRYER)) {
125 return new TumbleDryerHandler(thing, i18nProvider, localeProvider);
127 if (thing.getThingTypeUID().equals(THING_TYPE_DISHWASHER)) {
128 return new DishWasherHandler(thing, i18nProvider, localeProvider);
130 if (thing.getThingTypeUID().equals(THING_TYPE_COFFEEMACHINE)) {
131 return new CoffeeMachineHandler(thing, i18nProvider, localeProvider);
138 private ThingUID getBridgeThingUID(ThingTypeUID thingTypeUID, ThingUID thingUID, Configuration configuration) {
139 if (thingUID == null) {
140 String hostID = (String) configuration.get(HOST);
141 thingUID = new ThingUID(thingTypeUID, hostID);
146 private ThingUID getApplianceUID(ThingTypeUID thingTypeUID, ThingUID thingUID, Configuration configuration,
147 ThingUID bridgeUID) {
148 String applianceId = (String) configuration.get(APPLIANCE_ID);
150 if (thingUID == null) {
151 thingUID = new ThingUID(thingTypeUID, applianceId, bridgeUID.getId());
156 private synchronized void registerApplianceDiscoveryService(MieleBridgeHandler bridgeHandler) {
157 MieleApplianceDiscoveryService discoveryService = new MieleApplianceDiscoveryService(bridgeHandler);
158 discoveryService.activate();
159 this.discoveryServiceRegs.put(bridgeHandler.getThing().getUID(),
160 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
164 protected synchronized void removeHandler(ThingHandler thingHandler) {
165 if (thingHandler instanceof MieleBridgeHandler) {
166 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
167 if (serviceReg != null) {
168 // remove discovery service, if bridge handler is removed
169 MieleApplianceDiscoveryService service = (MieleApplianceDiscoveryService) bundleContext
170 .getService(serviceReg.getReference());
171 serviceReg.unregister();
172 if (service != null) {
173 service.deactivate();