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.homematic.internal.model;
15 import static org.openhab.binding.homematic.internal.misc.HomematicConstants.*;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Objects;
21 import org.openhab.binding.homematic.internal.misc.MiscUtils;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * Object that represents a Homematic device.
28 * @author Gerhard Riegler - Initial contribution
30 public class HmDevice {
31 private final Logger logger = LoggerFactory.getLogger(HmDevice.class);
33 public static final String TYPE_GATEWAY_EXTRAS = "GATEWAY-EXTRAS";
34 public static final String ADDRESS_GATEWAY_EXTRAS = "GWE00000000";
36 private final HmInterface hmInterface;
37 private final String address;
38 private final String type;
40 private final String firmware;
41 private final String gatewayId;
42 private final String homegearId;
44 private List<HmChannel> channels = new ArrayList<>();
46 public HmDevice(String address, HmInterface hmInterface, String type, String gatewayId, String homegearId,
48 this.address = address;
49 this.hmInterface = hmInterface;
50 this.firmware = firmware;
51 if ("HM-ES-TX-WM".equals(type) && Float.valueOf(firmware) > 2.0) {
52 logger.debug("Found HM-ES-TX-WM with firmware version > 2.0, creating virtual type");
53 this.type = type + "2";
57 this.gatewayId = gatewayId;
58 this.homegearId = homegearId;
62 * Returns the address of the device.
64 public String getAddress() {
69 * Returns the interface of the device.
71 public HmInterface getHmInterface() {
76 * Returns the name of the device.
78 public String getName() {
83 * Sets the name of the device.
85 public void setName(String name) {
90 * Returns the type of the device.
92 public String getType() {
97 * Returns all channels of the device.
99 public List<HmChannel> getChannels() {
104 * Returns the firmware of the device.
106 public String getFirmware() {
111 * Returns the gatewayId of the device.
113 public String getGatewayId() {
118 * Returns the homegearId of the device.
120 public String getHomegearId() {
125 * Adds a channel to this device.
127 public void addChannel(HmChannel channel) {
128 channel.setDevice(this);
129 channels.add(channel);
133 * Returns the channel with the given channelNumber.
135 public HmChannel getChannel(int channelNumber) {
136 for (HmChannel hmChannel : channels) {
137 if (hmChannel.getNumber() == channelNumber) {
145 * Returns the number of datapoints.
147 public int getDatapointCount() {
149 for (HmChannel channel : channels) {
150 dpCounter += channel.getDatapoints().size();
156 * Returns true, if the device is the Homematic gateway.
158 public boolean isGatewayExtras() {
159 return ADDRESS_GATEWAY_EXTRAS.equals(address);
163 * Returns true, if the device can not be reached (offline).
165 public boolean isUnreach() {
166 return isStatusDatapointEnabled(DATAPOINT_NAME_UNREACH);
170 * Returns true, if the gateway has a config to transfer to the device.
172 public boolean isConfigPending() {
173 return isStatusDatapointEnabled(DATAPOINT_NAME_CONFIG_PENDING);
177 * Returns true, if the gateway has a update to transfer to the device.
179 public boolean isUpdatePending() {
180 return isStatusDatapointEnabled(DATAPOINT_NAME_UPDATE_PENDING);
184 * Returns true, if the device is in firmware update mode.
186 public boolean isFirmwareUpdating() {
187 return isStatusDatapointEnabled(DATAPOINT_NAME_DEVICE_IN_BOOTLOADER);
191 * Returns true, if the device is offline.
193 public boolean isOffline() {
194 return isFirmwareUpdating() || isUnreach();
197 private boolean isStatusDatapointEnabled(String datapointName) {
198 HmChannel channel = getChannel(0);
199 if (channel != null && channel.isInitialized()) {
200 HmDatapointInfo dpInfo = HmDatapointInfo.createValuesInfo(channel, datapointName);
201 HmDatapoint dp = channel.getDatapoint(dpInfo);
203 return MiscUtils.isTrueValue(dp.getValue());
210 public int hashCode() {
211 return Objects.hash(address);
215 public boolean equals(Object obj) {
216 if (obj == null || !(obj instanceof HmDevice)) {
219 HmDevice comp = (HmDevice) obj;
220 return Objects.equals(address, comp.getAddress());
224 public String toString() {
225 return String.format("%s[hmInterface=%s,address=%s,type=%s,name=%s,firmware=%s,gatewayId=%s]",
226 getClass().getSimpleName(), hmInterface, address, type, name, firmware, gatewayId);