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.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.eclipse.jetty.client.HttpClient;
27 import org.openhab.binding.miele.internal.discovery.MieleApplianceDiscoveryService;
28 import org.openhab.binding.miele.internal.handler.CoffeeMachineHandler;
29 import org.openhab.binding.miele.internal.handler.DishwasherHandler;
30 import org.openhab.binding.miele.internal.handler.FridgeFreezerHandler;
31 import org.openhab.binding.miele.internal.handler.FridgeHandler;
32 import org.openhab.binding.miele.internal.handler.HobHandler;
33 import org.openhab.binding.miele.internal.handler.HoodHandler;
34 import org.openhab.binding.miele.internal.handler.MieleApplianceHandler;
35 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler;
36 import org.openhab.binding.miele.internal.handler.OvenHandler;
37 import org.openhab.binding.miele.internal.handler.TumbleDryerHandler;
38 import org.openhab.binding.miele.internal.handler.WashingMachineHandler;
39 import org.openhab.core.config.core.Configuration;
40 import org.openhab.core.config.discovery.DiscoveryService;
41 import org.openhab.core.i18n.LocaleProvider;
42 import org.openhab.core.i18n.TimeZoneProvider;
43 import org.openhab.core.i18n.TranslationProvider;
44 import org.openhab.core.io.net.http.HttpClientFactory;
45 import org.openhab.core.thing.Bridge;
46 import org.openhab.core.thing.Thing;
47 import org.openhab.core.thing.ThingTypeUID;
48 import org.openhab.core.thing.ThingUID;
49 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
50 import org.openhab.core.thing.binding.ThingHandler;
51 import org.openhab.core.thing.binding.ThingHandlerFactory;
52 import org.osgi.framework.ServiceRegistration;
53 import org.osgi.service.component.ComponentContext;
54 import org.osgi.service.component.annotations.Activate;
55 import org.osgi.service.component.annotations.Component;
56 import org.osgi.service.component.annotations.Reference;
59 * The {@link MieleHandlerFactory} is responsible for creating things and thing
62 * @author Karel Goderis - Initial contribution
63 * @author Jacob Laursen - Refactored to use framework's HTTP client
66 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.miele")
67 public class MieleHandlerFactory extends BaseThingHandlerFactory {
69 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
70 .concat(MieleBridgeHandler.SUPPORTED_THING_TYPES.stream(),
71 MieleApplianceHandler.SUPPORTED_THING_TYPES.stream())
72 .collect(Collectors.toSet());
74 private final HttpClient httpClient;
75 private final TranslationProvider i18nProvider;
76 private final LocaleProvider localeProvider;
77 private final TimeZoneProvider timeZoneProvider;
79 private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
82 public MieleHandlerFactory(@Reference final HttpClientFactory httpClientFactory,
83 final @Reference TranslationProvider i18nProvider, final @Reference LocaleProvider localeProvider,
84 final @Reference TimeZoneProvider timeZoneProvider, ComponentContext componentContext) {
85 this.httpClient = httpClientFactory.getCommonHttpClient();
86 this.i18nProvider = i18nProvider;
87 this.localeProvider = localeProvider;
88 this.timeZoneProvider = timeZoneProvider;
92 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
93 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
97 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
98 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
99 if (MieleBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
100 ThingUID mieleBridgeUID = getBridgeThingUID(thingTypeUID, thingUID, configuration);
101 return super.createThing(thingTypeUID, configuration, mieleBridgeUID, null);
103 if (MieleApplianceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
104 ThingUID mieleApplianceUID = getApplianceUID(thingTypeUID, thingUID, configuration, bridgeUID);
105 return super.createThing(thingTypeUID, configuration, mieleApplianceUID, bridgeUID);
107 throw new IllegalArgumentException(
108 "The thing type " + thingTypeUID + " is not supported by the miele binding.");
112 protected @Nullable ThingHandler createHandler(Thing thing) {
113 if (MieleBridgeHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
114 MieleBridgeHandler handler = new MieleBridgeHandler((Bridge) thing, httpClient);
115 registerApplianceDiscoveryService(handler);
117 } else if (MieleApplianceHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
118 if (thing.getThingTypeUID().equals(THING_TYPE_HOOD)) {
119 return new HoodHandler(thing, i18nProvider, localeProvider, timeZoneProvider);
121 if (thing.getThingTypeUID().equals(THING_TYPE_FRIDGEFREEZER)) {
122 return new FridgeFreezerHandler(thing, i18nProvider, localeProvider, timeZoneProvider);
124 if (thing.getThingTypeUID().equals(THING_TYPE_FRIDGE)) {
125 return new FridgeHandler(thing, i18nProvider, localeProvider, timeZoneProvider);
127 if (thing.getThingTypeUID().equals(THING_TYPE_OVEN)) {
128 return new OvenHandler(thing, i18nProvider, localeProvider, timeZoneProvider);
130 if (thing.getThingTypeUID().equals(THING_TYPE_HOB)) {
131 return new HobHandler(thing, i18nProvider, localeProvider, timeZoneProvider);
133 if (thing.getThingTypeUID().equals(THING_TYPE_WASHINGMACHINE)) {
134 return new WashingMachineHandler(thing, i18nProvider, localeProvider, timeZoneProvider);
136 if (thing.getThingTypeUID().equals(THING_TYPE_DRYER)) {
137 return new TumbleDryerHandler(thing, i18nProvider, localeProvider, timeZoneProvider);
139 if (thing.getThingTypeUID().equals(THING_TYPE_DISHWASHER)) {
140 return new DishwasherHandler(thing, i18nProvider, localeProvider, timeZoneProvider);
142 if (thing.getThingTypeUID().equals(THING_TYPE_COFFEEMACHINE)) {
143 return new CoffeeMachineHandler(thing, i18nProvider, localeProvider, timeZoneProvider);
150 private ThingUID getBridgeThingUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
151 Configuration configuration) {
152 if (thingUID == null) {
153 String hostID = (String) configuration.get(HOST);
154 thingUID = new ThingUID(thingTypeUID, hostID);
159 private ThingUID getApplianceUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
160 Configuration configuration, @Nullable ThingUID bridgeUID) {
161 String applianceId = (String) configuration.get(APPLIANCE_ID);
163 if (thingUID == null) {
164 if (bridgeUID == null) {
165 thingUID = new ThingUID(thingTypeUID, applianceId);
167 thingUID = new ThingUID(thingTypeUID, bridgeUID, applianceId);
173 private synchronized void registerApplianceDiscoveryService(MieleBridgeHandler bridgeHandler) {
174 MieleApplianceDiscoveryService discoveryService = new MieleApplianceDiscoveryService(bridgeHandler);
175 discoveryService.activate();
176 this.discoveryServiceRegs.put(bridgeHandler.getThing().getUID(),
177 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
181 protected synchronized void removeHandler(ThingHandler thingHandler) {
182 if (thingHandler instanceof MieleBridgeHandler) {
183 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
184 if (serviceReg != null) {
185 // remove discovery service, if bridge handler is removed
186 MieleApplianceDiscoveryService service = (MieleApplianceDiscoveryService) bundleContext
187 .getService(serviceReg.getReference());
188 serviceReg.unregister();
189 if (service != null) {
190 service.deactivate();