]> git.basschouten.com Git - openhab-addons.git/blob
5d758b7699e2de07cecdc894c09315390cd65b95
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.pilight.internal.handler;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
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;
40
41 /**
42  * The {@link PilightGenericHandler} is responsible for handling commands, which are
43  * sent to one of the channels.
44  *
45  * @author Stefan Röllin - Initial contribution
46  * @author Niklas Dörfler - Port pilight binding to openHAB 3 + add device discovery
47  */
48 @NonNullByDefault
49 public class PilightGenericHandler extends PilightBaseHandler {
50
51     private final Logger logger = LoggerFactory.getLogger(PilightGenericHandler.class);
52
53     private final ChannelTypeRegistry channelTypeRegistry;
54
55     private final Map<ChannelUID, Boolean> channelReadOnlyMap = new HashMap<>();
56
57     public PilightGenericHandler(Thing thing, ChannelTypeRegistry channelTypeRegistry) {
58         super(thing);
59         this.channelTypeRegistry = channelTypeRegistry;
60     }
61
62     @Override
63     public void initialize() {
64         super.initialize();
65         initializeReadOnlyChannels();
66     }
67
68     @Override
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())));
74         }
75     }
76
77     @Override
78     void updateFromConfigDevice(Device device) {
79     }
80
81     @Override
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());
85             return null;
86         }
87
88         logger.debug("Create update command for '{}' not implemented.", channelUID.getId());
89
90         return null;
91     }
92
93     private State getDynamicChannelState(final Channel channel, final @Nullable String value) {
94         final @Nullable String acceptedItemType = channel.getAcceptedItemType();
95
96         if (value == null || acceptedItemType == null) {
97             return UnDefType.UNDEF;
98         }
99
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);
107             default:
108                 logger.trace("Type '{}' for channel '{}' not implemented", channel.getAcceptedItemType(), channel);
109                 return UnDefType.UNDEF;
110         }
111     }
112
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);
119
120                 if (channelType != null) {
121                     logger.debug("initializeReadOnly {} {}", channelType, channelType.getState());
122                 }
123
124                 if (channelType != null) {
125                     final @Nullable StateDescription state = channelType.getState();
126                     if (state != null) {
127                         channelReadOnlyMap.putIfAbsent(channel.getUID(), state.isReadOnly());
128                     }
129                 }
130             }
131         }
132     }
133
134     private boolean isChannelReadOnly(ChannelUID channelUID) {
135         Boolean isReadOnly = channelReadOnlyMap.get(channelUID);
136         return isReadOnly != null ? isReadOnly : true;
137     }
138 }