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.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.Bridge;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingStatus;
26 import org.openhab.core.thing.binding.BaseThingHandler;
27 import org.openhab.core.thing.binding.BridgeHandler;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.RefreshType;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * The {@link PilightBaseHandler} is responsible for handling commands, which are
35 * sent to one of the channels.
37 * @author Stefan Röllin - Initial contribution
38 * @author Niklas Dörfler - Port pilight binding to openHAB 3 + add device discovery
41 public abstract class PilightBaseHandler extends BaseThingHandler {
43 private final Logger logger = LoggerFactory.getLogger(PilightBaseHandler.class);
45 private String name = "";
47 public PilightBaseHandler(Thing thing) {
52 public void handleCommand(ChannelUID channelUID, Command command) {
53 if (command instanceof RefreshType) {
54 refreshConfigAndStatus();
59 Action action = createUpdateCommand(channelUID, command);
66 public void initialize() {
67 PilightDeviceConfiguration config = getConfigAs(PilightDeviceConfiguration.class);
68 name = config.getName();
70 refreshConfigAndStatus();
73 public void updateFromStatusIfMatches(Status status) {
74 if (status.getDevices() != null && !status.getDevices().isEmpty()) {
75 if (name.equals(status.getDevices().get(0))) {
76 if (!ThingStatus.ONLINE.equals(getThing().getStatus())) {
77 updateStatus(ThingStatus.ONLINE);
79 updateFromStatus(status);
84 public void updateFromConfigIfMatches(Config config) {
85 Device device = config.getDevices().get(getName());
87 updateFromConfigDevice(device);
91 abstract void updateFromStatus(Status status);
93 abstract void updateFromConfigDevice(Device device);
95 abstract @Nullable Action createUpdateCommand(ChannelUID channelUID, Command command);
97 protected String getName() {
101 private void sendAction(Action action) {
102 final @Nullable PilightBridgeHandler handler = getPilightBridgeHandler();
103 if (handler != null) {
104 handler.sendAction(action);
106 logger.warn("No pilight bridge handler found to send action.");
110 private void refreshConfigAndStatus() {
111 final @Nullable PilightBridgeHandler handler = getPilightBridgeHandler();
112 if (handler != null) {
113 handler.refreshConfigAndStatus();
115 logger.warn("No pilight bridge handler found to refresh config and status.");
119 private @Nullable PilightBridgeHandler getPilightBridgeHandler() {
120 final @Nullable Bridge bridge = getBridge();
121 if (bridge != null) {
123 BridgeHandler handler = bridge.getHandler();
124 if (handler instanceof PilightBridgeHandler bridgeHandler) {
125 return bridgeHandler;