]> git.basschouten.com Git - openhab-addons.git/blob
94e95351f6b283f2a22d411f85b02be951f4f78c
[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.boschshc.internal.devices.windowcontact;
14
15 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.CHANNEL_CONTACT;
16
17 import java.util.List;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.boschshc.internal.devices.AbstractBatteryPoweredDeviceHandler;
21 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
22 import org.openhab.binding.boschshc.internal.services.shuttercontact.ShutterContactService;
23 import org.openhab.binding.boschshc.internal.services.shuttercontact.ShutterContactState;
24 import org.openhab.binding.boschshc.internal.services.shuttercontact.dto.ShutterContactServiceState;
25 import org.openhab.core.library.types.OpenClosedType;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.types.State;
28
29 /**
30  * Detects open windows and doors.
31  *
32  * @author Stefan Kästle - Initial contribution
33  */
34 @NonNullByDefault
35 public class WindowContactHandler extends AbstractBatteryPoweredDeviceHandler {
36
37     public WindowContactHandler(Thing thing) {
38         super(thing);
39     }
40
41     @Override
42     protected void initializeServices() throws BoschSHCException {
43         super.initializeServices();
44
45         this.createService(ShutterContactService::new, this::updateChannels, List.of(CHANNEL_CONTACT));
46     }
47
48     private void updateChannels(ShutterContactServiceState state) {
49         State contact = state.value == ShutterContactState.CLOSED ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
50         updateState(CHANNEL_CONTACT, contact);
51     }
52 }