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.onewire.internal.handler;
15 import static org.openhab.binding.onewire.internal.OwBindingConstants.*;
17 import java.util.HashSet;
20 import java.util.stream.Collectors;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.onewire.internal.OwDynamicStateDescriptionProvider;
24 import org.openhab.binding.onewire.internal.OwException;
25 import org.openhab.binding.onewire.internal.config.BAE091xHandlerConfiguration;
26 import org.openhab.binding.onewire.internal.device.BAE0910;
27 import org.openhab.binding.onewire.internal.device.OwChannelConfig;
28 import org.openhab.binding.onewire.internal.device.OwSensorType;
29 import org.openhab.core.library.types.OnOffType;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.thing.ThingStatusDetail;
35 import org.openhab.core.thing.ThingTypeUID;
36 import org.openhab.core.thing.ThingUID;
37 import org.openhab.core.thing.binding.builder.ThingBuilder;
38 import org.openhab.core.types.Command;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * The {@link BAE091xSensorThingHandler} is responsible for handling BAE0910 based multisensors
45 * @author Jan N. Klug - Initial contribution
48 public class BAE091xSensorThingHandler extends OwBaseThingHandler {
49 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_BAE091X);
51 private final Logger logger = LoggerFactory.getLogger(BAE091xSensorThingHandler.class);
53 public static final Set<OwSensorType> SUPPORTED_SENSOR_TYPES = Set.of(OwSensorType.BAE0910);
55 public BAE091xSensorThingHandler(Thing thing, OwDynamicStateDescriptionProvider dynamicStateDescriptionProvider) {
56 super(thing, dynamicStateDescriptionProvider, SUPPORTED_SENSOR_TYPES);
60 public void handleCommand(ChannelUID channelUID, Command command) {
61 if (command instanceof OnOffType) {
62 if (channelUID.getId().startsWith(CHANNEL_DIGITAL)) {
63 Bridge bridge = getBridge();
65 OwserverBridgeHandler bridgeHandler = (OwserverBridgeHandler) bridge.getHandler();
66 if (bridgeHandler != null) {
67 if (!((BAE0910) sensors.get(0)).writeChannel(bridgeHandler, channelUID.getId(), command)) {
68 logger.debug("writing to channel {} in thing {} not permitted (input channel)", channelUID,
72 logger.warn("bridge handler not found");
75 logger.warn("bridge not found");
80 super.handleCommand(channelUID, command);
84 public void initialize() {
85 if (!super.configureThingHandler()) {
89 sensors.add(new BAE0910(sensorId, this));
91 scheduler.execute(this::configureThingChannels);
95 protected void configureThingChannels() {
96 ThingUID thingUID = getThing().getUID();
97 logger.debug("configuring sensors for {}", thingUID);
99 BAE091xHandlerConfiguration configuration = getConfig().as(BAE091xHandlerConfiguration.class);
101 Set<OwChannelConfig> wantedChannel = new HashSet<>(SENSOR_TYPE_CHANNEL_MAP.getOrDefault(sensorType, Set.of()));
104 switch (configuration.pin1) {
105 case CONFIG_BAE_PIN_DISABLED:
107 case CONFIG_BAE_PIN_COUNTER:
108 wantedChannel.add(new OwChannelConfig(CHANNEL_COUNTER, CHANNEL_TYPE_UID_BAE_COUNTER));
111 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
112 "unknown configuration option for pin 1");
117 switch (configuration.pin2) {
118 case CONFIG_BAE_PIN_DISABLED:
120 case CONFIG_BAE_PIN_OUT:
122 new OwChannelConfig(CHANNEL_DIGITAL2, CHANNEL_TYPE_UID_BAE_DIGITAL_OUT, "Digital Out Pin 2"));
124 case CONFIG_BAE_PIN_PWM:
126 .add(new OwChannelConfig(CHANNEL_PWM_DUTY3, CHANNEL_TYPE_UID_BAE_PWM_DUTY, "Duty Cycle PWM 3"));
127 wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_FREQ1, CHANNEL_TYPE_UID_BAE_PWM_FREQUENCY,
128 "Frequency PWM 1/3"));
131 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
132 "unknown configuration option for pin 2");
137 switch (configuration.pin6) {
138 case CONFIG_BAE_PIN_DISABLED:
140 case CONFIG_BAE_PIN_PIO:
141 wantedChannel.add(new OwChannelConfig(CHANNEL_DIGITAL6, CHANNEL_TYPE_UID_BAE_PIO, "PIO Pin 6"));
143 case CONFIG_BAE_PIN_PWM:
145 .add(new OwChannelConfig(CHANNEL_PWM_DUTY4, CHANNEL_TYPE_UID_BAE_PWM_DUTY, "Duty Cycle PWM 4"));
146 wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_FREQ2, CHANNEL_TYPE_UID_BAE_PWM_FREQUENCY,
147 "Frequency PWM 2/4"));
150 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
151 "unknown configuration option for pin 6");
156 switch (configuration.pin7) {
157 case CONFIG_BAE_PIN_DISABLED:
159 case CONFIG_BAE_PIN_ANALOG:
160 wantedChannel.add(new OwChannelConfig(CHANNEL_VOLTAGE, CHANNEL_TYPE_UID_BAE_ANALOG, "Analog Input"));
162 case CONFIG_BAE_PIN_OUT:
164 new OwChannelConfig(CHANNEL_DIGITAL7, CHANNEL_TYPE_UID_BAE_DIGITAL_OUT, "Digital Out Pin 7"));
166 case CONFIG_BAE_PIN_PWM:
168 .add(new OwChannelConfig(CHANNEL_PWM_DUTY2, CHANNEL_TYPE_UID_BAE_PWM_DUTY, "Duty Cycle PWM 2"));
169 wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_FREQ2, CHANNEL_TYPE_UID_BAE_PWM_FREQUENCY,
170 "Frequency PWM 2/4"));
173 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
174 "unknown configuration option for pin 7");
179 switch (configuration.pin8) {
180 case CONFIG_BAE_PIN_DISABLED:
182 case CONFIG_BAE_PIN_IN:
183 wantedChannel.add(new OwChannelConfig(CHANNEL_DIGITAL8, CHANNEL_TYPE_UID_BAE_DIN, "Digital In Pin 8"));
185 case CONFIG_BAE_PIN_OUT:
187 .add(new OwChannelConfig(CHANNEL_DIGITAL8, CHANNEL_TYPE_UID_BAE_DOUT, "Digital Out Pin 8"));
189 case CONFIG_BAE_PIN_PWM:
191 .add(new OwChannelConfig(CHANNEL_PWM_DUTY1, CHANNEL_TYPE_UID_BAE_PWM_DUTY, "Duty Cycle PWM 1"));
192 wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_FREQ1, CHANNEL_TYPE_UID_BAE_PWM_FREQUENCY,
193 "Frequency PWM 1/3"));
197 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
198 "unknown configuration option for pin 8");
202 ThingBuilder thingBuilder = editThing();
204 // remove unwanted channels
205 Set<String> existingChannelIds = thing.getChannels().stream().map(channel -> channel.getUID().getId())
206 .collect(Collectors.toSet());
207 Set<String> wantedChannelIds = wantedChannel.stream().map(channelConfig -> channelConfig.channelId)
208 .collect(Collectors.toSet());
209 existingChannelIds.stream().filter(channelId -> !wantedChannelIds.contains(channelId))
210 .forEach(channelId -> removeChannelIfExisting(thingBuilder, channelId));
212 // add or update wanted channels
213 wantedChannel.forEach(channelConfig -> {
214 addChannelIfMissingAndEnable(thingBuilder, channelConfig);
217 updateThing(thingBuilder.build());
220 sensors.get(0).configureChannels();
221 } catch (OwException e) {
222 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
227 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE);
231 public void updateSensorProperties(OwserverBridgeHandler bridgeHandler) throws OwException {
232 Map<String, String> properties = editProperties();
234 sensorType = BAE0910.getDeviceSubType(bridgeHandler, sensorId);
236 properties.put(PROPERTY_MODELID, sensorType.toString());
237 properties.put(PROPERTY_VENDOR, "Brain4home");
239 updateProperties(properties);
241 logger.trace("updated modelid/vendor to {} / {}", sensorType.name(), "Brain4home");