]> git.basschouten.com Git - openhab-addons.git/blob
a9027650699cf1fbc43972204794de7d0b56f2fa
[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.model.ParadoxPanel;
21 import org.openhab.binding.paradoxalarm.internal.model.Zone;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.library.types.OpenClosedType;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.thing.Thing;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * The {@link ParadoxZoneHandler} Handler that updates states of paradox zones from the cache.
31  *
32  * @author Konstantin Polihronov - Initial contribution
33  */
34 public class ParadoxZoneHandler extends EntityBaseHandler {
35
36     private final Logger logger = LoggerFactory.getLogger(ParadoxZoneHandler.class);
37
38     public ParadoxZoneHandler(@NonNull Thing thing) {
39         super(thing);
40     }
41
42     @Override
43     protected void updateEntity() {
44         int index = calculateEntityIndex();
45         ParadoxIP150BridgeHandler bridge = (ParadoxIP150BridgeHandler) getBridge().getHandler();
46         ParadoxPanel panel = bridge.getPanel();
47         List<Zone> zones = panel.getZones();
48         if (zones == null) {
49             logger.debug(
50                     "Zones collection of Paradox Panel object is null. Probably not yet initialized. Skipping update.");
51             return;
52         }
53         if (zones.size() <= index) {
54             logger.debug("Attempted to access zone out of bounds of current zone list. Index: {}, List: {}", index,
55                     zones);
56             return;
57         }
58
59         Zone zone = zones.get(index);
60         if (zone != null) {
61             updateState(ZONE_LABEL_CHANNEL_UID, new StringType(zone.getLabel()));
62             updateState(ZONE_OPENED_CHANNEL_UID, booleanToContactState(zone.getZoneState().isOpened()));
63             updateState(ZONE_TAMPERED_CHANNEL_UID, booleanToSwitchState(zone.getZoneState().isTampered()));
64             updateState(ZONE_LOW_BATTERY_CHANNEL_UID, booleanToSwitchState(zone.getZoneState().hasLowBattery()));
65         }
66     }
67
68     private OpenClosedType booleanToContactState(boolean value) {
69         return value ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
70     }
71
72     private OnOffType booleanToSwitchState(boolean value) {
73         return value ? OnOffType.ON : OnOffType.OFF;
74     }
75 }