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.pilight.internal.handler;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.pilight.internal.PilightDeviceConfiguration;
18 import org.openhab.binding.pilight.internal.dto.Action;
19 import org.openhab.binding.pilight.internal.dto.Config;
20 import org.openhab.binding.pilight.internal.dto.Device;
21 import org.openhab.binding.pilight.internal.dto.Status;
22 import org.openhab.core.thing.*;
23 import org.openhab.core.thing.binding.BaseThingHandler;
24 import org.openhab.core.thing.binding.BridgeHandler;
25 import org.openhab.core.types.Command;
26 import org.openhab.core.types.RefreshType;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * The {@link PilightBaseHandler} is responsible for handling commands, which are
32 * sent to one of the channels.
34 * @author Stefan Röllin - Initial contribution
35 * @author Niklas Dörfler - Port pilight binding to openHAB 3 + add device discovery
38 public abstract class PilightBaseHandler extends BaseThingHandler {
40 private final Logger logger = LoggerFactory.getLogger(PilightBaseHandler.class);
42 private String name = "";
44 public PilightBaseHandler(Thing thing) {
49 public void handleCommand(ChannelUID channelUID, Command command) {
50 if (command instanceof RefreshType) {
51 refreshConfigAndStatus();
56 Action action = createUpdateCommand(channelUID, command);
63 public void initialize() {
64 PilightDeviceConfiguration config = getConfigAs(PilightDeviceConfiguration.class);
65 name = config.getName();
67 refreshConfigAndStatus();
70 public void updateFromStatusIfMatches(Status status) {
71 if (status.getDevices() != null && !status.getDevices().isEmpty()) {
72 if (name.equals(status.getDevices().get(0))) {
73 if (!ThingStatus.ONLINE.equals(getThing().getStatus())) {
74 updateStatus(ThingStatus.ONLINE);
76 updateFromStatus(status);
81 public void updateFromConfigIfMatches(Config config) {
82 Device device = config.getDevices().get(getName());
84 updateFromConfigDevice(device);
88 abstract void updateFromStatus(Status status);
90 abstract void updateFromConfigDevice(Device device);
92 abstract @Nullable Action createUpdateCommand(ChannelUID channelUID, Command command);
94 protected String getName() {
98 private void sendAction(Action action) {
99 final @Nullable PilightBridgeHandler handler = getPilightBridgeHandler();
100 if (handler != null) {
101 handler.sendAction(action);
103 logger.warn("No pilight bridge handler found to send action.");
107 private void refreshConfigAndStatus() {
108 final @Nullable PilightBridgeHandler handler = getPilightBridgeHandler();
109 if (handler != null) {
110 handler.refreshConfigAndStatus();
112 logger.warn("No pilight bridge handler found to refresh config and status.");
116 private @Nullable PilightBridgeHandler getPilightBridgeHandler() {
117 final @Nullable Bridge bridge = getBridge();
118 if (bridge != null) {
120 BridgeHandler handler = bridge.getHandler();
121 if (handler instanceof PilightBridgeHandler) {
122 return (PilightBridgeHandler) handler;