]> git.basschouten.com Git - openhab-addons.git/blob
04cb77b92c03fdfa3230f8d75ca2d2aa949bcfdb
[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.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, command == OnOffType.ON ? "on" : "off");
66                 }
67             }
68         }
69     }
70
71     @Override
72     public String getSerialNumber() {
73         return serialNumber;
74     }
75
76     protected void updateState() {
77         final DeviceDTO localDevice = device;
78         if (localDevice != null) {
79             String lampState = localDevice.state.lampState;
80             updateState("switch", "on".equals(lampState) ? OnOffType.ON : OnOffType.OFF);
81         }
82     }
83
84     @Override
85     public void handleDeviceUpdate(DeviceDTO device) {
86         if (!MyQBindingConstants.THING_TYPE_LAMP.getId().equals(device.deviceFamily)) {
87             return;
88         }
89         this.device = device;
90         if (device.state.online) {
91             updateStatus(ThingStatus.ONLINE);
92             updateState();
93         } else {
94             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Device reports as offline");
95         }
96     }
97 }