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.silvercrestwifisocket.internal;
17 import org.openhab.binding.silvercrestwifisocket.internal.exceptions.MacAddressNotValidException;
18 import org.openhab.binding.silvercrestwifisocket.internal.handler.SilvercrestWifiSocketHandler;
19 import org.openhab.binding.silvercrestwifisocket.internal.handler.SilvercrestWifiSocketMediator;
20 import org.openhab.core.thing.Thing;
21 import org.openhab.core.thing.ThingTypeUID;
22 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
23 import org.openhab.core.thing.binding.ThingHandler;
24 import org.openhab.core.thing.binding.ThingHandlerFactory;
25 import org.osgi.service.component.annotations.Component;
26 import org.osgi.service.component.annotations.Reference;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * The {@link SilvercrestWifiSocketHandlerFactory} is responsible for creating things and thing
34 * @author Jaime Vaz - Initial contribution
36 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.silvercrestwifisocket")
37 public class SilvercrestWifiSocketHandlerFactory extends BaseThingHandlerFactory {
39 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set
40 .of(SilvercrestWifiSocketBindingConstants.THING_TYPE_WIFI_SOCKET);
42 private final Logger logger = LoggerFactory.getLogger(SilvercrestWifiSocketHandlerFactory.class);
44 private SilvercrestWifiSocketMediator mediator;
47 * Used by OSGI to inject the mediator in the handler factory.
49 * @param mediator the mediator
52 public void setMediator(final SilvercrestWifiSocketMediator mediator) {
53 logger.debug("Mediator has been injected on handler factory service.");
54 this.mediator = mediator;
58 * Used by OSGI to unsets the mediator from the handler factory.
60 * @param mediator the mediator
62 public void unsetMediator(final SilvercrestWifiSocketMediator mitsubishiMediator) {
63 logger.debug("Mediator has been unsetted from discovery service.");
68 public boolean supportsThingType(final ThingTypeUID thingTypeUID) {
69 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
73 protected ThingHandler createHandler(final Thing thing) {
74 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
76 if (thingTypeUID.equals(SilvercrestWifiSocketBindingConstants.THING_TYPE_WIFI_SOCKET)) {
77 SilvercrestWifiSocketHandler handler;
78 logger.debug("Creating a new SilvercrestWifiSocketHandler...");
80 handler = new SilvercrestWifiSocketHandler(thing);
81 logger.debug("SilvercrestWifiSocketMediator will register the handler.");
82 if (this.mediator != null) {
83 this.mediator.registerThingAndWifiSocketHandler(thing, handler);
86 "The mediator is missing on Handler factory. Without one mediator the handler cannot work!");
90 } catch (MacAddressNotValidException e) {
91 logger.debug("The mac address passed to WifiSocketHandler by configurations is not valid.");
98 public void unregisterHandler(final Thing thing) {
99 if (this.mediator != null) {
100 this.mediator.unregisterWifiSocketHandlerByThing(thing);
102 super.unregisterHandler(thing);