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.doorbird.internal.handler;
15 import static org.openhab.binding.doorbird.internal.DoorbirdBindingConstants.*;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.doorbird.internal.api.DoorbirdAPI;
20 import org.openhab.binding.doorbird.internal.api.DoorbirdInfo;
21 import org.openhab.binding.doorbird.internal.config.ControllerConfiguration;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingStatus;
26 import org.openhab.core.thing.ThingStatusDetail;
27 import org.openhab.core.thing.binding.BaseThingHandler;
28 import org.openhab.core.types.Command;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * The {@link ControllerHandler} is responsible for handling commands
34 * to the A1081 Controller.
36 * @author Mark Hilbush - Initial contribution
39 public class ControllerHandler extends BaseThingHandler {
40 private final Logger logger = LoggerFactory.getLogger(ControllerHandler.class);
42 private @Nullable String controllerId;
44 private DoorbirdAPI api = new DoorbirdAPI();
46 public ControllerHandler(Thing thing) {
51 public void initialize() {
52 ControllerConfiguration config = getConfigAs(ControllerConfiguration.class);
53 String host = config.doorbirdHost;
55 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Doorbird host not provided");
58 String user = config.userId;
60 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "User ID not provided");
63 String password = config.userPassword;
64 if (password == null) {
65 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "User password not provided");
68 api.setAuthorization(host, user, password);
70 // Get the Id of the controller for use in the open door API
71 controllerId = getControllerId(config.controllerId);
72 if (controllerId != null) {
73 updateStatus(ThingStatus.ONLINE);
75 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
76 "Doorbird not configured with a Controller");
81 public void handleCommand(ChannelUID channelUID, Command command) {
82 logger.debug("Got command {} for channel {} of thing {}", command, channelUID, getThing().getUID());
84 switch (channelUID.getId()) {
85 case CHANNEL_OPENDOOR1:
86 handleOpenDoor(command, "1");
88 case CHANNEL_OPENDOOR2:
89 handleOpenDoor(command, "2");
91 case CHANNEL_OPENDOOR3:
92 handleOpenDoor(command, "3");
97 private void handleOpenDoor(Command command, String doorNumber) {
98 String id = controllerId;
100 logger.debug("Unable to handle open door command because controller ID is not set");
103 if (command.equals(OnOffType.ON)) {
104 api.openDoorController(id, doorNumber);
108 private @Nullable String getControllerId(@Nullable String configId) {
109 DoorbirdInfo info = api.getDoorbirdInfo();
110 return info == null ? null : info.getControllerId(configId);