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.openhab.core.library.types.DecimalType;
16 import org.openhab.core.library.types.HSBType;
17 import org.openhab.core.library.types.OnOffType;
18 import org.openhab.core.library.types.OpenClosedType;
19 import org.openhab.core.library.types.PercentType;
20 import org.openhab.core.thing.Channel;
21 import org.openhab.core.types.State;
22 import org.openhab.core.types.UnDefType;
24 import de.fh_zwickau.informatik.sensor.model.devices.Color;
25 import de.fh_zwickau.informatik.sensor.model.devices.Device;
26 import de.fh_zwickau.informatik.sensor.model.devices.types.Battery;
27 import de.fh_zwickau.informatik.sensor.model.devices.types.Doorlock;
28 import de.fh_zwickau.informatik.sensor.model.devices.types.SensorBinary;
29 import de.fh_zwickau.informatik.sensor.model.devices.types.SensorDiscrete;
30 import de.fh_zwickau.informatik.sensor.model.devices.types.SensorMultilevel;
31 import de.fh_zwickau.informatik.sensor.model.devices.types.SwitchBinary;
32 import de.fh_zwickau.informatik.sensor.model.devices.types.SwitchControl;
33 import de.fh_zwickau.informatik.sensor.model.devices.types.SwitchMultilevel;
34 import de.fh_zwickau.informatik.sensor.model.devices.types.SwitchRGBW;
35 import de.fh_zwickau.informatik.sensor.model.devices.types.SwitchToggle;
36 import de.fh_zwickau.informatik.sensor.model.devices.types.Thermostat;
37 import de.fh_zwickau.informatik.sensor.model.devices.types.ToggleButton;
40 * The {@link ZWayDeviceStateConverter} is responsible for converting Z-Way device level to openHAB states
42 * @author Patrick Hecker - Initial contribution
44 public class ZWayDeviceStateConverter {
45 public static State toState(Device device, Channel channel) {
46 // Store level locally
47 String level = device.getMetrics().getLevel();
49 // Set item state to level depending on device type
50 if (device instanceof Battery) {
51 return getMultilevelState(level);
52 } else if (device instanceof Doorlock) {
53 return getBinaryState(level.toLowerCase());
54 } else if (device instanceof SensorBinary) {
55 if (channel.getAcceptedItemType().equals("Contact")) {
56 return getDoorlockState(level.toLowerCase());
58 return getBinaryState(level.toLowerCase());
60 } else if (device instanceof SensorMultilevel) {
61 return getMultilevelState(level);
62 } else if (device instanceof SwitchBinary) {
63 return getBinaryState(level.toLowerCase());
64 } else if (device instanceof SwitchMultilevel) {
65 if (channel.getAcceptedItemType().equals("Rollershutter")
66 || channel.getAcceptedItemType().equals("Dimmer")) {
67 return getPercentState(level);
69 return getMultilevelState(level);
71 } else if (device instanceof SwitchRGBW) {
72 return getColorState(device.getMetrics().getColor());
73 } else if (device instanceof Thermostat) {
74 return getMultilevelState(level);
75 } else if (device instanceof SwitchControl) {
76 return getBinaryState(level.toLowerCase());
77 } else if (device instanceof ToggleButton || device instanceof SwitchToggle) {
78 return getBinaryState(level.toLowerCase());
79 } else if (device instanceof SensorDiscrete) {
80 return getMultilevelState(level);
83 return UnDefType.UNDEF;
87 * Transforms a value in an openHAB type.
89 * @param multilevel sensor value
90 * @return transformed openHAB state
92 private static State getMultilevelState(String multilevelValue) {
93 if (multilevelValue != null) {
94 return new DecimalType(multilevelValue);
96 return UnDefType.UNDEF;
99 private static State getPercentState(String multilevelValue) {
100 if (multilevelValue != null) {
101 return new PercentType(multilevelValue);
103 return UnDefType.UNDEF;
107 * Transforms a value in an openHAB type.
109 * @param binary switch value
110 * @return transformed openHAB state
112 private static State getBinaryState(String binarySwitchState) {
113 if (binarySwitchState != null) {
114 if (binarySwitchState.equals("on")) {
116 } else if (binarySwitchState.equals("off")) {
117 return OnOffType.OFF;
120 return UnDefType.UNDEF;
124 * Transforms a value in an openHAB type.
128 * @param binary sensor state
131 private static State getDoorlockState(String binarySensorState) {
132 if (binarySensorState != null) {
133 if (binarySensorState.equals("on")) {
134 return OpenClosedType.OPEN;
135 } else if (binarySensorState.equals("off")) {
136 return OpenClosedType.CLOSED;
139 return UnDefType.UNDEF;
143 * Transforms a value in an openHAB type.
145 * @param Z-Way color value
146 * @return transformed openHAB state
148 private static State getColorState(Color colorSwitchState) {
149 if (colorSwitchState != null && colorSwitchState.getRed() != null && colorSwitchState.getGreen() != null
150 && colorSwitchState.getBlue() != null) {
151 HSBType hsbType = HSBType.fromRGB(colorSwitchState.getRed(), colorSwitchState.getGreen(),
152 colorSwitchState.getBlue());
157 return UnDefType.UNDEF;