]> git.basschouten.com Git - openhab-addons.git/blob
4805c1d9f2dcaea17e4666c894aabd1c64df2b38
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.zway.internal.converter;
14
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;
25
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;
40
41 /**
42  * The {@link ZWayDeviceStateConverter} is responsible for converting Z-Way device level to openHAB states
43  *
44  * @author Patrick Hecker - Initial contribution
45  */
46 @NonNullByDefault
47 public class ZWayDeviceStateConverter {
48     public static State toState(Device device, Channel channel) {
49         // Store level locally
50         String level = device.getMetrics().getLevel();
51
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());
60             } else {
61                 return getBinaryState(level.toLowerCase());
62             }
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);
71             } else {
72                 return getMultilevelState(level);
73             }
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);
84         }
85
86         return UnDefType.UNDEF;
87     }
88
89     /**
90      * Transforms a value in an openHAB type.
91      *
92      * @param multilevel sensor value
93      * @return transformed openHAB state
94      */
95     private static State getMultilevelState(@Nullable String multilevelValue) {
96         if (multilevelValue != null) {
97             return new DecimalType(multilevelValue);
98         }
99         return UnDefType.UNDEF;
100     }
101
102     private static State getPercentState(@Nullable String multilevelValue) {
103         if (multilevelValue != null) {
104             return new PercentType(multilevelValue);
105         }
106         return UnDefType.UNDEF;
107     }
108
109     /**
110      * Transforms a value in an openHAB type.
111      *
112      * @param binary switch value
113      * @return transformed openHAB state
114      */
115     private static State getBinaryState(@Nullable String binarySwitchState) {
116         if (binarySwitchState != null) {
117             if ("on".equals(binarySwitchState)) {
118                 return OnOffType.ON;
119             } else if ("off".equals(binarySwitchState)) {
120                 return OnOffType.OFF;
121             }
122         }
123         return UnDefType.UNDEF;
124     }
125
126     /**
127      * Transforms a value in an openHAB type.
128      * - ON to OPEN
129      * - OFF to CLOSED
130      *
131      * @param binary sensor state
132      * @return
133      */
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;
140             }
141         }
142         return UnDefType.UNDEF;
143     }
144
145     /**
146      * Transforms a value in an openHAB type.
147      *
148      * @param Z-Way color value
149      * @return transformed openHAB state
150      */
151     private static State getColorState(@Nullable Color colorSwitchState) {
152         if (colorSwitchState != null && colorSwitchState.getRed() != null && colorSwitchState.getGreen() != null
153                 && colorSwitchState.getBlue() != null) {
154             return HSBType.fromRGB(colorSwitchState.getRed(), colorSwitchState.getGreen(), colorSwitchState.getBlue());
155         }
156
157         return UnDefType.UNDEF;
158     }
159 }