2 * Copyright (c) 2010-2021 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.myq.internal.handler;
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;
32 * The {@link MyQLampHandler} is responsible for handling commands for a lamp thing, which are
33 * sent to one of the channels.
35 * @author Dan Cunningham - Initial contribution
38 public class MyQLampHandler extends BaseThingHandler implements MyQDeviceHandler {
39 private @Nullable DeviceDTO deviceState;
40 private String serialNumber;
42 public MyQLampHandler(Thing thing) {
44 serialNumber = getConfigAs(MyQDeviceConfiguration.class).serialNumber;
48 public void initialize() {
49 updateStatus(ThingStatus.UNKNOWN);
53 public void handleCommand(ChannelUID channelUID, Command command) {
54 if (command instanceof RefreshType) {
59 if (command instanceof OnOffType) {
60 Bridge bridge = getBridge();
61 final DeviceDTO localState = deviceState;
62 if (bridge != null && localState != null) {
63 BridgeHandler handler = bridge.getHandler();
64 if (handler != null) {
65 ((MyQAccountHandler) handler).sendAction(localState.serialNumber,
66 command == OnOffType.ON ? "turnon" : "turnoff");
73 public String getSerialNumber() {
77 protected void updateState() {
78 final DeviceDTO localState = deviceState;
79 if (localState != null) {
80 String lampState = localState.state.lampState;
81 updateState("switch", "on".equals(lampState) ? OnOffType.ON : OnOffType.OFF);
86 public void handleDeviceUpdate(DeviceDTO device) {
87 if (!MyQBindingConstants.THING_TYPE_LAMP.getId().equals(device.deviceFamily)) {
91 if (device.state.online) {
92 updateStatus(ThingStatus.ONLINE);
95 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Device reports as offline");