]> git.basschouten.com Git - openhab-addons.git/blob
99855a25fbb4fbc8e306d3ffd92b901e246a4c8f
[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.paradoxalarm.internal.handlers;
14
15 import static org.openhab.binding.paradoxalarm.internal.handlers.ParadoxAlarmBindingConstants.*;
16
17 import java.util.List;
18
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.openhab.binding.paradoxalarm.internal.communication.messages.partition.PartitionCommand;
21 import org.openhab.binding.paradoxalarm.internal.model.ParadoxPanel;
22 import org.openhab.binding.paradoxalarm.internal.model.Partition;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.OpenClosedType;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.types.Command;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * The {@link ParadoxPartitionHandler} Handler that updates states of paradox partitions from the cache.
34  *
35  * @author Konstantin Polihronov - Initial contribution
36  */
37 public class ParadoxPartitionHandler extends EntityBaseHandler {
38
39     private final Logger logger = LoggerFactory.getLogger(ParadoxPartitionHandler.class);
40
41     public ParadoxPartitionHandler(@NonNull Thing thing) {
42         super(thing);
43     }
44
45     @Override
46     protected void updateEntity() {
47         Partition partition = getPartition();
48         if (partition != null) {
49             updateState(PARTITION_LABEL_CHANNEL_UID, new StringType(partition.getLabel()));
50             updateState(PARTITION_STATE_CHANNEL_UID, new StringType(partition.getState().getMainState()));
51             updateState(PARTITION_ADDITIONAL_STATES_CHANNEL_UID,
52                     new StringType("Deprecated field. Use direct channels instead"));
53             updateState(PARTITION_READY_TO_ARM_CHANNEL_UID, booleanToSwitchState(partition.getState().isReadyToArm()));
54             updateState(PARTITION_IN_EXIT_DELAY_CHANNEL_UID,
55                     booleanToSwitchState(partition.getState().isInExitDelay()));
56             updateState(PARTITION_IN_ENTRY_DELAY_CHANNEL_UID,
57                     booleanToSwitchState(partition.getState().isInEntryDelay()));
58             updateState(PARTITION_IN_TROUBLE_CHANNEL_UID, booleanToSwitchState(partition.getState().isInTrouble()));
59             updateState(PARTITION_ALARM_IN_MEMORY_CHANNEL_UID,
60                     booleanToSwitchState(partition.getState().isHasAlarmInMemory()));
61             updateState(PARTITION_ZONE_BYPASS_CHANNEL_UID, booleanToSwitchState(partition.getState().isInZoneBypass()));
62             updateState(PARTITION_ZONE_IN_TAMPER_CHANNEL_UID,
63                     booleanToSwitchState(partition.getState().isHasZoneInTamperTrouble()));
64             updateState(PARTITION_ZONE_IN_LOW_BATTERY_CHANNEL_UID,
65                     booleanToSwitchState(partition.getState().isHasZoneInLowBatteryTrouble()));
66             updateState(PARTITION_ZONE_IN_FIRE_LOOP_CHANNEL_UID,
67                     booleanToSwitchState(partition.getState().isHasZoneInFireLoopTrouble()));
68             updateState(PARTITION_ZONE_IN_SUPERVISION_TROUBLE_CHANNEL_UID,
69                     booleanToSwitchState(partition.getState().isHasZoneInSupervisionTrouble()));
70             updateState(PARTITION_STAY_INSTANT_READY_CHANNEL_UID,
71                     booleanToSwitchState(partition.getState().isStayInstantReady()));
72             updateState(PARTITION_FORCE_READY_CHANNEL_UID, booleanToSwitchState(partition.getState().isForceReady()));
73             updateState(PARTITION_BYPASS_READY_CHANNEL_UID, booleanToSwitchState(partition.getState().isBypassReady()));
74             updateState(PARTITION_INHIBIT_READY_CHANNEL_UID,
75                     booleanToSwitchState(partition.getState().isInhibitReady()));
76             updateState(PARTITION_ALL_ZONES_CLOSED_CHANNEL_UID,
77                     booleanToContactState(partition.getState().isAreAllZoneclosed()));
78         }
79     }
80
81     private OpenClosedType booleanToContactState(boolean value) {
82         return value ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
83     }
84
85     private OnOffType booleanToSwitchState(boolean value) {
86         return value ? OnOffType.ON : OnOffType.OFF;
87     }
88
89     @Override
90     public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
91         if (command instanceof StringType) {
92             boolean isDisarmCommand = PartitionCommand.DISARM.name().equals(command.toString());
93             if (isDisarmCommand && !config.isDisarmEnabled()) {
94                 logger.warn(
95                         "Received DISARM command but Disarm is not enabled. This is security relevant feature. Check documentation!");
96                 return;
97             }
98
99             Partition partition = getPartition();
100             if (partition != null) {
101                 partition.handleCommand(command.toString());
102             }
103         } else {
104             super.handleCommand(channelUID, command);
105         }
106     }
107
108     protected Partition getPartition() {
109         int index = calculateEntityIndex();
110         ParadoxIP150BridgeHandler bridge = (ParadoxIP150BridgeHandler) getBridge().getHandler();
111         ParadoxPanel panel = bridge.getPanel();
112         List<Partition> partitions = panel.getPartitions();
113         if (partitions == null) {
114             logger.debug(
115                     "Partitions collection of Paradox Panel object is null. Probably not yet initialized. Skipping update.");
116             return null;
117         }
118         if (partitions.size() <= index) {
119             logger.debug("Attempted to access partition out of bounds of current partitions list. Index: {}, List: {}",
120                     index, partitions);
121             return null;
122         }
123
124         Partition partition = partitions.get(index);
125         return partition;
126     }
127 }