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.nobohub.internal;
15 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.CHANNEL_COMPONENT_CURRENT_TEMPERATURE;
16 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.PROPERTY_MODEL;
17 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.PROPERTY_NAME;
18 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.PROPERTY_TEMPERATURE_SENSOR_FOR_ZONE;
19 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.PROPERTY_ZONE;
23 import javax.measure.quantity.Temperature;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.nobohub.internal.model.Component;
28 import org.openhab.binding.nobohub.internal.model.SerialNumber;
29 import org.openhab.binding.nobohub.internal.model.Zone;
30 import org.openhab.core.library.types.QuantityType;
31 import org.openhab.core.library.unit.SIUnits;
32 import org.openhab.core.thing.Bridge;
33 import org.openhab.core.thing.ChannelUID;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingStatus;
36 import org.openhab.core.thing.ThingStatusDetail;
37 import org.openhab.core.thing.binding.BaseThingHandler;
38 import org.openhab.core.types.Command;
39 import org.openhab.core.types.RefreshType;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * Shows information about a Component in the Nobø Hub.
46 * @author Jørgen Austvik - Initial contribution
49 public class ComponentHandler extends BaseThingHandler {
51 private final Logger logger = LoggerFactory.getLogger(ComponentHandler.class);
53 private final NoboHubTranslationProvider messages;
55 protected @Nullable SerialNumber serialNumber;
57 public ComponentHandler(Thing thing, NoboHubTranslationProvider messages) {
59 this.messages = messages;
62 public void onUpdate(Component component) {
63 updateStatus(ThingStatus.ONLINE);
65 double temp = component.getTemperature();
66 if (!Double.isNaN(temp)) {
67 QuantityType<Temperature> currentTemperature = new QuantityType<>(temp, SIUnits.CELSIUS);
68 updateState(CHANNEL_COMPONENT_CURRENT_TEMPERATURE, currentTemperature);
71 Map<String, String> properties = editProperties();
72 properties.put(Thing.PROPERTY_SERIAL_NUMBER, component.getSerialNumber().toString());
73 properties.put(PROPERTY_NAME, component.getName());
74 properties.put(PROPERTY_MODEL, component.getSerialNumber().getComponentType());
76 String zoneName = getZoneName(component.getZoneId());
77 if (zoneName != null) {
78 properties.put(PROPERTY_ZONE, zoneName);
81 String tempForZoneName = getZoneName(component.getTemperatureSensorForZoneId());
82 if (tempForZoneName != null) {
83 properties.put(PROPERTY_TEMPERATURE_SENSOR_FOR_ZONE, tempForZoneName);
85 updateProperties(properties);
88 private @Nullable String getZoneName(int zoneId) {
89 Bridge noboHub = getBridge();
90 if (null != noboHub) {
91 NoboHubBridgeHandler hubHandler = (NoboHubBridgeHandler) noboHub.getHandler();
92 if (hubHandler != null) {
93 Zone zone = hubHandler.getZone(zoneId);
95 return zone.getName();
104 public void initialize() {
105 String serialNumberString = getConfigAs(ComponentConfiguration.class).serialNumber;
106 if (serialNumberString != null && !serialNumberString.isEmpty()) {
107 SerialNumber sn = new SerialNumber(serialNumberString);
108 if (!sn.isWellFormed()) {
109 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
110 "@text/message.component.illegal.serial [\"" + serialNumberString + "\"]");
112 this.serialNumber = sn;
113 updateStatus(ThingStatus.ONLINE);
116 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "@text/message.missing.serial");
121 public void handleCommand(ChannelUID channelUID, Command command) {
122 if (command instanceof RefreshType) {
123 logger.debug("Refreshing channel {}", channelUID);
124 if (null != serialNumber) {
125 Component component = getComponent();
126 if (null == component) {
127 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.GONE,
128 messages.getText("message.component.notfound", serialNumber, channelUID));
133 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.GONE,
134 "@text/message.component.missing.id [\"" + channelUID + "\"]");
140 logger.debug("This component is a read-only device and cannot handle commands.");
143 public @Nullable SerialNumber getSerialNumber() {
147 private @Nullable Component getComponent() {
148 Bridge noboHub = getBridge();
149 if (null != noboHub) {
150 NoboHubBridgeHandler hubHandler = (NoboHubBridgeHandler) noboHub.getHandler();
151 SerialNumber serialNumber = this.serialNumber;
152 if (null != serialNumber && null != hubHandler) {
153 return hubHandler.getComponent(serialNumber);