2 * Copyright (c) 2010-2022 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.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.miele.internal.discovery.MieleApplianceDiscoveryService;
27 import org.openhab.binding.miele.internal.handler.CoffeeMachineHandler;
28 import org.openhab.binding.miele.internal.handler.DishWasherHandler;
29 import org.openhab.binding.miele.internal.handler.FridgeFreezerHandler;
30 import org.openhab.binding.miele.internal.handler.FridgeHandler;
31 import org.openhab.binding.miele.internal.handler.HobHandler;
32 import org.openhab.binding.miele.internal.handler.HoodHandler;
33 import org.openhab.binding.miele.internal.handler.MieleApplianceHandler;
34 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler;
35 import org.openhab.binding.miele.internal.handler.OvenHandler;
36 import org.openhab.binding.miele.internal.handler.TumbleDryerHandler;
37 import org.openhab.binding.miele.internal.handler.WashingMachineHandler;
38 import org.openhab.core.config.core.Configuration;
39 import org.openhab.core.config.discovery.DiscoveryService;
40 import org.openhab.core.i18n.LocaleProvider;
41 import org.openhab.core.i18n.TranslationProvider;
42 import org.openhab.core.thing.Bridge;
43 import org.openhab.core.thing.Thing;
44 import org.openhab.core.thing.ThingTypeUID;
45 import org.openhab.core.thing.ThingUID;
46 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
47 import org.openhab.core.thing.binding.ThingHandler;
48 import org.openhab.core.thing.binding.ThingHandlerFactory;
49 import org.osgi.framework.ServiceRegistration;
50 import org.osgi.service.component.ComponentContext;
51 import org.osgi.service.component.annotations.Activate;
52 import org.osgi.service.component.annotations.Component;
53 import org.osgi.service.component.annotations.Reference;
56 * The {@link MieleHandlerFactory} is responsible for creating things and thing
59 * @author Karel Goderis - Initial contribution
62 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.miele")
63 public class MieleHandlerFactory extends BaseThingHandlerFactory {
65 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
66 .concat(MieleBridgeHandler.SUPPORTED_THING_TYPES.stream(),
67 MieleApplianceHandler.SUPPORTED_THING_TYPES.stream())
68 .collect(Collectors.toSet());
70 private final TranslationProvider i18nProvider;
71 private final LocaleProvider localeProvider;
73 private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
76 public MieleHandlerFactory(final @Reference TranslationProvider i18nProvider,
77 final @Reference LocaleProvider localeProvider, ComponentContext componentContext) {
78 this.i18nProvider = i18nProvider;
79 this.localeProvider = localeProvider;
83 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
84 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
88 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
89 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
90 if (MieleBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
91 ThingUID mieleBridgeUID = getBridgeThingUID(thingTypeUID, thingUID, configuration);
92 return super.createThing(thingTypeUID, configuration, mieleBridgeUID, null);
94 if (MieleApplianceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
95 ThingUID mieleApplianceUID = getApplianceUID(thingTypeUID, thingUID, configuration, bridgeUID);
96 return super.createThing(thingTypeUID, configuration, mieleApplianceUID, bridgeUID);
98 throw new IllegalArgumentException(
99 "The thing type " + thingTypeUID + " is not supported by the miele binding.");
103 protected @Nullable ThingHandler createHandler(Thing thing) {
104 if (MieleBridgeHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
105 MieleBridgeHandler handler = new MieleBridgeHandler((Bridge) thing);
106 registerApplianceDiscoveryService(handler);
108 } else if (MieleApplianceHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
109 if (thing.getThingTypeUID().equals(THING_TYPE_HOOD)) {
110 return new HoodHandler(thing, i18nProvider, localeProvider);
112 if (thing.getThingTypeUID().equals(THING_TYPE_FRIDGEFREEZER)) {
113 return new FridgeFreezerHandler(thing, i18nProvider, localeProvider);
115 if (thing.getThingTypeUID().equals(THING_TYPE_FRIDGE)) {
116 return new FridgeHandler(thing, i18nProvider, localeProvider);
118 if (thing.getThingTypeUID().equals(THING_TYPE_OVEN)) {
119 return new OvenHandler(thing, i18nProvider, localeProvider);
121 if (thing.getThingTypeUID().equals(THING_TYPE_HOB)) {
122 return new HobHandler(thing, i18nProvider, localeProvider);
124 if (thing.getThingTypeUID().equals(THING_TYPE_WASHINGMACHINE)) {
125 return new WashingMachineHandler(thing, i18nProvider, localeProvider);
127 if (thing.getThingTypeUID().equals(THING_TYPE_DRYER)) {
128 return new TumbleDryerHandler(thing, i18nProvider, localeProvider);
130 if (thing.getThingTypeUID().equals(THING_TYPE_DISHWASHER)) {
131 return new DishWasherHandler(thing, i18nProvider, localeProvider);
133 if (thing.getThingTypeUID().equals(THING_TYPE_COFFEEMACHINE)) {
134 return new CoffeeMachineHandler(thing, i18nProvider, localeProvider);
141 private ThingUID getBridgeThingUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
142 Configuration configuration) {
143 if (thingUID == null) {
144 String hostID = (String) configuration.get(HOST);
145 thingUID = new ThingUID(thingTypeUID, hostID);
150 private ThingUID getApplianceUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
151 Configuration configuration, @Nullable ThingUID bridgeUID) {
152 String applianceId = (String) configuration.get(APPLIANCE_ID);
154 if (thingUID == null) {
155 if (bridgeUID == null) {
156 thingUID = new ThingUID(thingTypeUID, applianceId);
158 thingUID = new ThingUID(thingTypeUID, bridgeUID, applianceId);
164 private synchronized void registerApplianceDiscoveryService(MieleBridgeHandler bridgeHandler) {
165 MieleApplianceDiscoveryService discoveryService = new MieleApplianceDiscoveryService(bridgeHandler);
166 discoveryService.activate();
167 this.discoveryServiceRegs.put(bridgeHandler.getThing().getUID(),
168 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
172 protected synchronized void removeHandler(ThingHandler thingHandler) {
173 if (thingHandler instanceof MieleBridgeHandler) {
174 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
175 if (serviceReg != null) {
176 // remove discovery service, if bridge handler is removed
177 MieleApplianceDiscoveryService service = (MieleApplianceDiscoveryService) bundleContext
178 .getService(serviceReg.getReference());
179 serviceReg.unregister();
180 if (service != null) {
181 service.deactivate();