]> git.basschouten.com Git - openhab-addons.git/blob
80c537eee5b4bc173ffc81e9c345ebaa4c541d9c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.myq.internal.handler;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.myq.internal.MyQBindingConstants;
18 import org.openhab.binding.myq.internal.config.MyQDeviceConfiguration;
19 import org.openhab.binding.myq.internal.dto.DeviceDTO;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.thing.Bridge;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingStatus;
25 import org.openhab.core.thing.ThingStatusDetail;
26 import org.openhab.core.thing.binding.BaseThingHandler;
27 import org.openhab.core.thing.binding.BridgeHandler;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.RefreshType;
30
31 /**
32  * The {@link MyQLampHandler} is responsible for handling commands for a lamp thing, which are
33  * sent to one of the channels.
34  *
35  * @author Dan Cunningham - Initial contribution
36  */
37 @NonNullByDefault
38 public class MyQLampHandler extends BaseThingHandler implements MyQDeviceHandler {
39     private @Nullable DeviceDTO device;
40     private String serialNumber;
41
42     public MyQLampHandler(Thing thing) {
43         super(thing);
44         serialNumber = getConfigAs(MyQDeviceConfiguration.class).serialNumber;
45     }
46
47     @Override
48     public void initialize() {
49         updateStatus(ThingStatus.UNKNOWN);
50     }
51
52     @Override
53     public void handleCommand(ChannelUID channelUID, Command command) {
54         if (command instanceof RefreshType) {
55             updateState();
56             return;
57         }
58
59         if (command instanceof OnOffType) {
60             Bridge bridge = getBridge();
61             final DeviceDTO localDevice = device;
62             if (bridge != null && localDevice != null) {
63                 BridgeHandler handler = bridge.getHandler();
64                 if (handler != null) {
65                     ((MyQAccountHandler) handler).sendLampAction(localDevice,
66                             command == OnOffType.ON ? "turnon" : "turnoff");
67                 }
68             }
69         }
70     }
71
72     @Override
73     public String getSerialNumber() {
74         return serialNumber;
75     }
76
77     protected void updateState() {
78         final DeviceDTO localDevice = device;
79         if (localDevice != null) {
80             String lampState = localDevice.state.lampState;
81             updateState("switch", "on".equals(lampState) ? OnOffType.ON : OnOffType.OFF);
82         }
83     }
84
85     @Override
86     public void handleDeviceUpdate(DeviceDTO device) {
87         if (!MyQBindingConstants.THING_TYPE_LAMP.getId().equals(device.deviceFamily)) {
88             return;
89         }
90         this.device = device;
91         if (device.state.online) {
92             updateStatus(ThingStatus.ONLINE);
93             updateState();
94         } else {
95             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Device reports as offline");
96         }
97     }
98 }