2 * Copyright (c) 2010-2023 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.binding.paradoxalarm.internal.handlers;
15 import static org.openhab.binding.paradoxalarm.internal.handlers.ParadoxAlarmBindingConstants.*;
17 import java.util.List;
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;
33 * The {@link ParadoxPartitionHandler} Handler that updates states of paradox partitions from the cache.
35 * @author Konstantin Polihronov - Initial contribution
37 public class ParadoxPartitionHandler extends EntityBaseHandler {
39 private final Logger logger = LoggerFactory.getLogger(ParadoxPartitionHandler.class);
41 public ParadoxPartitionHandler(@NonNull Thing thing) {
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()));
82 private OpenClosedType booleanToContactState(boolean value) {
83 return value ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
86 private OnOffType booleanToSwitchState(boolean value) {
87 return OnOffType.from(value);
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()) {
96 "Received DISARM command but Disarm is not enabled. This is security relevant feature. Check documentation!");
100 Partition partition = getPartition();
101 if (partition != null) {
102 partition.handleCommand(command.toString());
105 super.handleCommand(channelUID, command);
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) {
116 "Partitions collection of Paradox Panel object is null. Probably not yet initialized. Skipping update.");
119 if (partitions.size() <= index) {
120 logger.debug("Attempted to access partition out of bounds of current partitions list. Index: {}, List: {}",
125 return partitions.get(index);