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.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 device;
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 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");
72 public String getSerialNumber() {
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);
85 public void handleDeviceUpdate(DeviceDTO device) {
86 if (!MyQBindingConstants.THING_TYPE_LAMP.getId().equals(device.deviceFamily)) {
90 if (device.state.online) {
91 updateStatus(ThingStatus.ONLINE);
94 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Device reports as offline");