2 * Copyright (c) 2010-2024 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 java.util.HashMap;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.pilight.internal.PilightChannelConfiguration;
21 import org.openhab.binding.pilight.internal.dto.Action;
22 import org.openhab.binding.pilight.internal.dto.Device;
23 import org.openhab.binding.pilight.internal.dto.Status;
24 import org.openhab.core.library.CoreItemFactory;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.StringType;
28 import org.openhab.core.thing.Channel;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.type.ChannelType;
32 import org.openhab.core.thing.type.ChannelTypeRegistry;
33 import org.openhab.core.thing.type.ChannelTypeUID;
34 import org.openhab.core.types.Command;
35 import org.openhab.core.types.State;
36 import org.openhab.core.types.StateDescription;
37 import org.openhab.core.types.UnDefType;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * The {@link PilightGenericHandler} is responsible for handling commands, which are
43 * sent to one of the channels.
45 * @author Stefan Röllin - Initial contribution
46 * @author Niklas Dörfler - Port pilight binding to openHAB 3 + add device discovery
49 public class PilightGenericHandler extends PilightBaseHandler {
51 private final Logger logger = LoggerFactory.getLogger(PilightGenericHandler.class);
53 private final ChannelTypeRegistry channelTypeRegistry;
55 private final Map<ChannelUID, Boolean> channelReadOnlyMap = new HashMap<>();
57 public PilightGenericHandler(Thing thing, ChannelTypeRegistry channelTypeRegistry) {
59 this.channelTypeRegistry = channelTypeRegistry;
63 public void initialize() {
65 initializeReadOnlyChannels();
69 protected void updateFromStatus(Status status) {
70 for (Channel channel : thing.getChannels()) {
71 PilightChannelConfiguration config = channel.getConfiguration().as(PilightChannelConfiguration.class);
72 updateState(channel.getUID(),
73 getDynamicChannelState(channel, status.getValues().get(config.getProperty())));
78 void updateFromConfigDevice(Device device) {
82 protected @Nullable Action createUpdateCommand(ChannelUID channelUID, Command command) {
83 if (isChannelReadOnly(channelUID)) {
84 logger.debug("Can't apply command '{}' to '{}' because channel is readonly.", command, channelUID.getId());
88 logger.debug("Create update command for '{}' not implemented.", channelUID.getId());
93 private State getDynamicChannelState(final Channel channel, final @Nullable String value) {
94 final @Nullable String acceptedItemType = channel.getAcceptedItemType();
96 if (value == null || acceptedItemType == null) {
97 return UnDefType.UNDEF;
100 switch (acceptedItemType) {
101 case CoreItemFactory.NUMBER:
102 return new DecimalType(value);
103 case CoreItemFactory.STRING:
104 return StringType.valueOf(value);
105 case CoreItemFactory.SWITCH:
106 return OnOffType.from(value);
108 logger.trace("Type '{}' for channel '{}' not implemented", channel.getAcceptedItemType(), channel);
109 return UnDefType.UNDEF;
113 private void initializeReadOnlyChannels() {
114 channelReadOnlyMap.clear();
115 for (Channel channel : thing.getChannels()) {
116 final @Nullable ChannelTypeUID channelTypeUID = channel.getChannelTypeUID();
117 if (channelTypeUID != null) {
118 final @Nullable ChannelType channelType = channelTypeRegistry.getChannelType(channelTypeUID, null);
120 if (channelType != null) {
121 logger.debug("initializeReadOnly {} {}", channelType, channelType.getState());
124 if (channelType != null) {
125 final @Nullable StateDescription state = channelType.getState();
127 channelReadOnlyMap.putIfAbsent(channel.getUID(), state.isReadOnly());
134 private boolean isChannelReadOnly(ChannelUID channelUID) {
135 Boolean isReadOnly = channelReadOnlyMap.get(channelUID);
136 return isReadOnly != null ? isReadOnly : true;