]> git.basschouten.com Git - openhab-addons.git/blob
8ca6ed97a5f4b0e7a06428269d1fb17d35ae5560
[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_DETAILED_STATE_CHANNEL_UID, new StringType(partition.getState().getDetailedState()));
52             updateState(PARTITION_ADDITIONAL_STATES_CHANNEL_UID,
53                     new StringType("Deprecated field. Use direct channels instead"));
54             updateState(PARTITION_READY_TO_ARM_CHANNEL_UID, booleanToSwitchState(partition.getState().isReadyToArm()));
55             updateState(PARTITION_IN_EXIT_DELAY_CHANNEL_UID,
56                     booleanToSwitchState(partition.getState().isInExitDelay()));
57             updateState(PARTITION_IN_ENTRY_DELAY_CHANNEL_UID,
58                     booleanToSwitchState(partition.getState().isInEntryDelay()));
59             updateState(PARTITION_IN_TROUBLE_CHANNEL_UID, booleanToSwitchState(partition.getState().isInTrouble()));
60             updateState(PARTITION_ALARM_IN_MEMORY_CHANNEL_UID,
61                     booleanToSwitchState(partition.getState().isHasAlarmInMemory()));
62             updateState(PARTITION_ZONE_BYPASS_CHANNEL_UID, booleanToSwitchState(partition.getState().isInZoneBypass()));
63             updateState(PARTITION_ZONE_IN_TAMPER_CHANNEL_UID,
64                     booleanToSwitchState(partition.getState().isHasZoneInTamperTrouble()));
65             updateState(PARTITION_ZONE_IN_LOW_BATTERY_CHANNEL_UID,
66                     booleanToSwitchState(partition.getState().isHasZoneInLowBatteryTrouble()));
67             updateState(PARTITION_ZONE_IN_FIRE_LOOP_CHANNEL_UID,
68                     booleanToSwitchState(partition.getState().isHasZoneInFireLoopTrouble()));
69             updateState(PARTITION_ZONE_IN_SUPERVISION_TROUBLE_CHANNEL_UID,
70                     booleanToSwitchState(partition.getState().isHasZoneInSupervisionTrouble()));
71             updateState(PARTITION_STAY_INSTANT_READY_CHANNEL_UID,
72                     booleanToSwitchState(partition.getState().isStayInstantReady()));
73             updateState(PARTITION_FORCE_READY_CHANNEL_UID, booleanToSwitchState(partition.getState().isForceReady()));
74             updateState(PARTITION_BYPASS_READY_CHANNEL_UID, booleanToSwitchState(partition.getState().isBypassReady()));
75             updateState(PARTITION_INHIBIT_READY_CHANNEL_UID,
76                     booleanToSwitchState(partition.getState().isInhibitReady()));
77             updateState(PARTITION_ALL_ZONES_CLOSED_CHANNEL_UID,
78                     booleanToContactState(partition.getState().isAreAllZoneclosed()));
79         }
80     }
81
82     private OpenClosedType booleanToContactState(boolean value) {
83         return value ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
84     }
85
86     private OnOffType booleanToSwitchState(boolean value) {
87         return value ? OnOffType.ON : OnOffType.OFF;
88     }
89
90     @Override
91     public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
92         if (command instanceof StringType) {
93             boolean isDisarmCommand = PartitionCommand.DISARM.name().equals(command.toString());
94             if (isDisarmCommand && !config.isDisarmEnabled()) {
95                 logger.warn(
96                         "Received DISARM command but Disarm is not enabled. This is security relevant feature. Check documentation!");
97                 return;
98             }
99
100             Partition partition = getPartition();
101             if (partition != null) {
102                 partition.handleCommand(command.toString());
103             }
104         } else {
105             super.handleCommand(channelUID, command);
106         }
107     }
108
109     protected Partition getPartition() {
110         int index = calculateEntityIndex();
111         ParadoxIP150BridgeHandler bridge = (ParadoxIP150BridgeHandler) getBridge().getHandler();
112         ParadoxPanel panel = bridge.getPanel();
113         List<Partition> partitions = panel.getPartitions();
114         if (partitions == null) {
115             logger.debug(
116                     "Partitions collection of Paradox Panel object is null. Probably not yet initialized. Skipping update.");
117             return null;
118         }
119         if (partitions.size() <= index) {
120             logger.debug("Attempted to access partition out of bounds of current partitions list. Index: {}, List: {}",
121                     index, partitions);
122             return null;
123         }
124
125         return partitions.get(index);
126     }
127 }