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.zway.internal.converter;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.core.library.types.DecimalType;
18 import org.openhab.core.library.types.HSBType;
19 import org.openhab.core.library.types.OnOffType;
20 import org.openhab.core.library.types.OpenClosedType;
21 import org.openhab.core.library.types.PercentType;
22 import org.openhab.core.thing.Channel;
23 import org.openhab.core.types.State;
24 import org.openhab.core.types.UnDefType;
26 import de.fh_zwickau.informatik.sensor.model.devices.Color;
27 import de.fh_zwickau.informatik.sensor.model.devices.Device;
28 import de.fh_zwickau.informatik.sensor.model.devices.types.Battery;
29 import de.fh_zwickau.informatik.sensor.model.devices.types.Doorlock;
30 import de.fh_zwickau.informatik.sensor.model.devices.types.SensorBinary;
31 import de.fh_zwickau.informatik.sensor.model.devices.types.SensorDiscrete;
32 import de.fh_zwickau.informatik.sensor.model.devices.types.SensorMultilevel;
33 import de.fh_zwickau.informatik.sensor.model.devices.types.SwitchBinary;
34 import de.fh_zwickau.informatik.sensor.model.devices.types.SwitchControl;
35 import de.fh_zwickau.informatik.sensor.model.devices.types.SwitchMultilevel;
36 import de.fh_zwickau.informatik.sensor.model.devices.types.SwitchRGBW;
37 import de.fh_zwickau.informatik.sensor.model.devices.types.SwitchToggle;
38 import de.fh_zwickau.informatik.sensor.model.devices.types.Thermostat;
39 import de.fh_zwickau.informatik.sensor.model.devices.types.ToggleButton;
42 * The {@link ZWayDeviceStateConverter} is responsible for converting Z-Way device level to openHAB states
44 * @author Patrick Hecker - Initial contribution
47 public class ZWayDeviceStateConverter {
48 public static State toState(Device device, Channel channel) {
49 // Store level locally
50 String level = device.getMetrics().getLevel();
52 // Set item state to level depending on device type
53 if (device instanceof Battery) {
54 return getMultilevelState(level);
55 } else if (device instanceof Doorlock) {
56 return getBinaryState(level.toLowerCase());
57 } else if (device instanceof SensorBinary) {
58 if ("Contact".equals(channel.getAcceptedItemType())) {
59 return getDoorlockState(level.toLowerCase());
61 return getBinaryState(level.toLowerCase());
63 } else if (device instanceof SensorMultilevel) {
64 return getMultilevelState(level);
65 } else if (device instanceof SwitchBinary) {
66 return getBinaryState(level.toLowerCase());
67 } else if (device instanceof SwitchMultilevel) {
68 if ("Rollershutter".equals(channel.getAcceptedItemType())
69 || "Dimmer".equals(channel.getAcceptedItemType())) {
70 return getPercentState(level);
72 return getMultilevelState(level);
74 } else if (device instanceof SwitchRGBW) {
75 return getColorState(device.getMetrics().getColor());
76 } else if (device instanceof Thermostat) {
77 return getMultilevelState(level);
78 } else if (device instanceof SwitchControl) {
79 return getBinaryState(level.toLowerCase());
80 } else if (device instanceof ToggleButton || device instanceof SwitchToggle) {
81 return getBinaryState(level.toLowerCase());
82 } else if (device instanceof SensorDiscrete) {
83 return getMultilevelState(level);
86 return UnDefType.UNDEF;
90 * Transforms a value in an openHAB type.
92 * @param multilevel sensor value
93 * @return transformed openHAB state
95 private static State getMultilevelState(@Nullable String multilevelValue) {
96 if (multilevelValue != null) {
97 return new DecimalType(multilevelValue);
99 return UnDefType.UNDEF;
102 private static State getPercentState(@Nullable String multilevelValue) {
103 if (multilevelValue != null) {
104 return new PercentType(multilevelValue);
106 return UnDefType.UNDEF;
110 * Transforms a value in an openHAB type.
112 * @param binary switch value
113 * @return transformed openHAB state
115 private static State getBinaryState(@Nullable String binarySwitchState) {
116 if (binarySwitchState != null) {
117 if ("on".equals(binarySwitchState)) {
119 } else if ("off".equals(binarySwitchState)) {
120 return OnOffType.OFF;
123 return UnDefType.UNDEF;
127 * Transforms a value in an openHAB type.
131 * @param binary sensor state
134 private static State getDoorlockState(@Nullable String binarySensorState) {
135 if (binarySensorState != null) {
136 if ("on".equals(binarySensorState)) {
137 return OpenClosedType.OPEN;
138 } else if ("off".equals(binarySensorState)) {
139 return OpenClosedType.CLOSED;
142 return UnDefType.UNDEF;
146 * Transforms a value in an openHAB type.
148 * @param Z-Way color value
149 * @return transformed openHAB state
151 private static State getColorState(@Nullable Color colorSwitchState) {
152 if (colorSwitchState != null && colorSwitchState.getRed() != null && colorSwitchState.getGreen() != null
153 && colorSwitchState.getBlue() != null) {
154 HSBType hsbType = HSBType.fromRGB(colorSwitchState.getRed(), colorSwitchState.getGreen(),
155 colorSwitchState.getBlue());
160 return UnDefType.UNDEF;