]> git.basschouten.com Git - openhab-addons.git/blob
29da0abb17183ecdee8693ee0cf4365f419a146e
[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.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;
23
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;
38
39 /**
40  * The {@link ZWayDeviceStateConverter} is responsible for converting Z-Way device level to openHAB states
41  *
42  * @author Patrick Hecker - Initial contribution
43  */
44 public class ZWayDeviceStateConverter {
45     public static State toState(Device device, Channel channel) {
46         // Store level locally
47         String level = device.getMetrics().getLevel();
48
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());
57             } else {
58                 return getBinaryState(level.toLowerCase());
59             }
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);
68             } else {
69                 return getMultilevelState(level);
70             }
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);
81         }
82
83         return UnDefType.UNDEF;
84     }
85
86     /**
87      * Transforms a value in an openHAB type.
88      *
89      * @param multilevel sensor value
90      * @return transformed openHAB state
91      */
92     private static State getMultilevelState(String multilevelValue) {
93         if (multilevelValue != null) {
94             return new DecimalType(multilevelValue);
95         }
96         return UnDefType.UNDEF;
97     }
98
99     private static State getPercentState(String multilevelValue) {
100         if (multilevelValue != null) {
101             return new PercentType(multilevelValue);
102         }
103         return UnDefType.UNDEF;
104     }
105
106     /**
107      * Transforms a value in an openHAB type.
108      *
109      * @param binary switch value
110      * @return transformed openHAB state
111      */
112     private static State getBinaryState(String binarySwitchState) {
113         if (binarySwitchState != null) {
114             if (binarySwitchState.equals("on")) {
115                 return OnOffType.ON;
116             } else if (binarySwitchState.equals("off")) {
117                 return OnOffType.OFF;
118             }
119         }
120         return UnDefType.UNDEF;
121     }
122
123     /**
124      * Transforms a value in an openHAB type.
125      * - ON to OPEN
126      * - OFF to CLOSED
127      *
128      * @param binary sensor state
129      * @return
130      */
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;
137             }
138         }
139         return UnDefType.UNDEF;
140     }
141
142     /**
143      * Transforms a value in an openHAB type.
144      *
145      * @param Z-Way color value
146      * @return transformed openHAB state
147      */
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());
153
154             return hsbType;
155         }
156
157         return UnDefType.UNDEF;
158     }
159 }