2 * Copyright (c) 2010-2022 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.elroconnects.internal.devices;
15 import static org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.POWER_STATE;
17 import java.io.IOException;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsBridgeHandler;
21 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsDeviceHandler;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.thing.ThingStatusDetail;
25 import org.openhab.core.types.State;
26 import org.openhab.core.types.UnDefType;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * The {@link ElroConnectsDevicePowerSocket} is representing an ELRO Connects power socket device.
33 * @author Mark Herwege - Initial contribution
36 public class ElroConnectsDevicePowerSocket extends ElroConnectsDevice {
38 private final Logger logger = LoggerFactory.getLogger(ElroConnectsDevicePowerSocket.class);
41 private static final String STAT_ON = "00";
42 private static final String STAT_OFF = "01";
45 protected static final String CMD_OFF = "0101FFFF";
46 protected static final String CMD_ON = "0100FFFF";
48 public ElroConnectsDevicePowerSocket(int deviceId, ElroConnectsBridgeHandler bridge) {
49 super(deviceId, bridge);
53 public void switchState(boolean state) {
55 bridge.deviceControl(deviceId, state ? CMD_ON : CMD_OFF);
56 } catch (IOException e) {
57 logger.debug("Failed to control device: {}", e.getMessage());
62 public void updateState() {
63 ElroConnectsDeviceHandler handler = getHandler();
64 if (handler == null) {
68 String deviceStatus = this.deviceStatus;
69 if (deviceStatus.length() < 6) {
70 logger.debug("Could not decode device status: {}", deviceStatus);
74 String status = deviceStatus.substring(4, 6);
75 State state = STAT_ON.equals(status) ? OnOffType.ON
76 : (STAT_OFF.equals(status) ? OnOffType.OFF : UnDefType.UNDEF);
77 handler.updateState(POWER_STATE, state);
78 if (UnDefType.UNDEF.equals(state)) {
79 handler.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
80 "Device " + deviceId + " is not syncing with K1 hub");
82 handler.updateStatus(ThingStatus.ONLINE);
87 public void testAlarm() {
92 public void muteAlarm() {