2 * Copyright (c) 2010-2021 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.io.imperihome.internal.model.device;
15 import org.apache.commons.lang.StringUtils;
16 import org.openhab.core.items.Item;
17 import org.openhab.core.library.types.DecimalType;
18 import org.openhab.core.library.types.OnOffType;
19 import org.openhab.core.library.types.OpenClosedType;
20 import org.openhab.core.library.types.StringType;
21 import org.openhab.core.types.State;
22 import org.openhab.io.imperihome.internal.model.param.DeviceParam;
23 import org.openhab.io.imperihome.internal.model.param.ParamType;
26 * Abstraction of devices that are trippable, i.e. DevDoor, DevFlood, DevMotion, DevSmoke, DevCO2Alert.
28 * @author Pepijn de Geus - Initial contribution
30 public class TrippableDevice extends AbstractDevice {
32 public TrippableDevice(DeviceType type, Item item) {
35 addParam(new DeviceParam(ParamType.ARMABLE, "0"));
36 addParam(new DeviceParam(ParamType.ARMED, "1"));
37 addParam(new DeviceParam(ParamType.ACKABLE, "0"));
41 public void stateUpdated(Item item, State newState) {
42 super.stateUpdated(item, newState);
44 boolean tripped = false;
46 if (item.getStateAs(OpenClosedType.class) != null) {
47 OpenClosedType state = (OpenClosedType) item.getStateAs(OpenClosedType.class);
48 tripped = state == OpenClosedType.CLOSED;
49 } else if (item.getStateAs(OnOffType.class) != null) {
50 OnOffType state = (OnOffType) item.getStateAs(OnOffType.class);
51 tripped = state == OnOffType.ON;
52 } else if (item.getStateAs(DecimalType.class) != null) {
53 DecimalType state = (DecimalType) item.getStateAs(DecimalType.class);
54 tripped = state.intValue() != 0;
55 } else if (item.getStateAs(StringType.class) != null) {
56 StringType state = (StringType) item.getStateAs(StringType.class);
57 tripped = StringUtils.isNotBlank(state.toString()) && !state.toString().trim().equals("ok");
59 logger.debug("Can't interpret state {} as tripped status", item.getState());
62 addParam(new DeviceParam(ParamType.TRIPPED, tripped ^ isInverted() ? "1" : "0"));
65 addParam(new DeviceParam(ParamType.LAST_TRIP, String.valueOf(System.currentTimeMillis())));