]> git.basschouten.com Git - openhab-addons.git/blob
646e12e0b49000fb0d9cdb87e92974ae35cee832
[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.io.imperihome.internal.model.device;
14
15 import org.openhab.core.items.Item;
16 import org.openhab.core.library.types.DecimalType;
17 import org.openhab.core.library.types.OnOffType;
18 import org.openhab.core.library.types.OpenClosedType;
19 import org.openhab.core.library.types.StringType;
20 import org.openhab.core.types.State;
21 import org.openhab.io.imperihome.internal.model.param.DeviceParam;
22 import org.openhab.io.imperihome.internal.model.param.ParamType;
23
24 /**
25  * Abstraction of devices that are trippable, i.e. DevDoor, DevFlood, DevMotion, DevSmoke, DevCO2Alert.
26  *
27  * @author Pepijn de Geus - Initial contribution
28  */
29 public class TrippableDevice extends AbstractDevice {
30
31     public TrippableDevice(DeviceType type, Item item) {
32         super(type, item);
33
34         addParam(new DeviceParam(ParamType.ARMABLE, "0"));
35         addParam(new DeviceParam(ParamType.ARMED, "1"));
36         addParam(new DeviceParam(ParamType.ACKABLE, "0"));
37     }
38
39     @Override
40     public void stateUpdated(Item item, State newState) {
41         super.stateUpdated(item, newState);
42
43         boolean tripped = false;
44
45         if (item.getStateAs(OpenClosedType.class) != null) {
46             OpenClosedType state = item.getStateAs(OpenClosedType.class);
47             tripped = state == OpenClosedType.CLOSED;
48         } else if (item.getStateAs(OnOffType.class) != null) {
49             OnOffType state = item.getStateAs(OnOffType.class);
50             tripped = state == OnOffType.ON;
51         } else if (item.getStateAs(DecimalType.class) != null) {
52             DecimalType state = item.getStateAs(DecimalType.class);
53             tripped = state != null && state.intValue() != 0;
54         } else if (item.getStateAs(StringType.class) != null) {
55             StringType state = item.getStateAs(StringType.class);
56             tripped = state != null && !state.toString().isBlank() && !state.toString().trim().equals("ok");
57         } else {
58             logger.debug("Can't interpret state {} as tripped status", item.getState());
59         }
60
61         addParam(new DeviceParam(ParamType.TRIPPED, tripped ^ isInverted() ? "1" : "0"));
62
63         if (tripped) {
64             addParam(new DeviceParam(ParamType.LAST_TRIP, String.valueOf(System.currentTimeMillis())));
65         }
66     }
67 }