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.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_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()));
81 protected Partition getPartition() {
82 int index = calculateEntityIndex();
83 ParadoxIP150BridgeHandler bridge = (ParadoxIP150BridgeHandler) getBridge().getHandler();
84 ParadoxPanel panel = bridge.getPanel();
85 List<Partition> partitions = panel.getPartitions();
86 if (partitions == null) {
88 "Partitions collection of Paradox Panel object is null. Probably not yet initialized. Skipping update.");
91 if (partitions.size() <= index) {
92 logger.debug("Attempted to access partition out of bounds of current partitions list. Index: {}, List: {}",
97 Partition partition = partitions.get(index);
101 private OpenClosedType booleanToContactState(boolean value) {
102 return value ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
105 private OnOffType booleanToSwitchState(boolean value) {
106 return value ? OnOffType.ON : OnOffType.OFF;
110 public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
111 if (command instanceof StringType) {
112 boolean isDisarmCommand = PartitionCommand.DISARM.name().equals(command.toString());
113 if (isDisarmCommand && !config.isDisarmEnabled()) {
115 "Received DISARM command but Disarm is not enabled. This is security relevant feature. Check documentation!");
119 Partition partition = getPartition();
120 if (partition != null) {
121 partition.handleCommand(command.toString());
124 super.handleCommand(channelUID, command);