From cdebcc7ca98fd67dc08b4bf869bd4f2d4afaf20f Mon Sep 17 00:00:00 2001 From: Mark Herwege Date: Sat, 23 Oct 2021 16:27:56 +0200 Subject: [PATCH] [nikohomecontrol] Prepare for translation (#11319) * Externalize strings to support translation Signed-off-by: Mark Herwege * Name events thread. Signed-off-by: Mark Herwege * Fix formatting Signed-off-by: Mark Herwege * Fix SAT warning Signed-off-by: Mark Herwege * Fix threadname Signed-off-by: Mark Herwege * Fix thing lifecycle. Signed-off-by: Mark Herwege * Adjustments from review. Signed-off-by: Mark Herwege * Adjustment from review. Signed-off-by: Mark Herwege --- .../NikoHomeControlBindingConstants.java | 3 + .../handler/NikoHomeControlActionHandler.java | 47 +++--- .../handler/NikoHomeControlBridgeHandler.java | 4 +- .../NikoHomeControlBridgeHandler1.java | 9 +- .../NikoHomeControlBridgeHandler2.java | 7 +- .../NikoHomeControlEnergyMeterHandler.java | 97 +++++------ .../NikoHomeControlThermostatHandler.java | 43 ++--- .../protocol/NikoHomeControlDiscover.java | 5 +- .../nhc1/NikoHomeControlCommunication1.java | 14 +- .../nhc2/NikoHomeControlCommunication2.java | 11 +- .../main/resources/OH-INF/binding/binding.xml | 4 +- .../OH-INF/i18n/nikohomecontrol.properties | 120 ++++++++++++++ .../resources/OH-INF/thing/thing-types.xml | 156 +++++++++--------- 13 files changed, 308 insertions(+), 212 deletions(-) create mode 100644 bundles/org.openhab.binding.nikohomecontrol/src/main/resources/OH-INF/i18n/nikohomecontrol.properties diff --git a/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/NikoHomeControlBindingConstants.java b/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/NikoHomeControlBindingConstants.java index d140f2a12fa51..a411f5c3c2a11 100644 --- a/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/NikoHomeControlBindingConstants.java +++ b/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/NikoHomeControlBindingConstants.java @@ -31,6 +31,9 @@ public class NikoHomeControlBindingConstants { public static final String BINDING_ID = "nikohomecontrol"; + // Listener threadname prefix + public static final String THREAD_NAME_PREFIX = "OH-binding-"; + // List of all Thing Type UIDs // bridge diff --git a/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlActionHandler.java b/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlActionHandler.java index 3cd76e9da2d16..9003d502253e6 100644 --- a/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlActionHandler.java +++ b/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlActionHandler.java @@ -66,8 +66,8 @@ public NikoHomeControlActionHandler(Thing thing) { public void handleCommand(ChannelUID channelUID, Command command) { NikoHomeControlCommunication nhcComm = getCommunication(); if (nhcComm == null) { - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, - "Bridge communication not initialized when trying to execute action command " + actionId); + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED, + "@text/offline.bridge-unitialized"); return; } @@ -92,7 +92,7 @@ private void handleCommandSelection(ChannelUID channelUID, Command command) { logger.debug("handle command {} for {}", command, channelUID); - if (command == REFRESH) { + if (REFRESH.equals(command)) { actionEvent(nhcAction.getState()); return; } @@ -107,15 +107,12 @@ private void handleCommandSelection(ChannelUID channelUID, Command command) { handleBrightnessCommand(command); updateStatus(ThingStatus.ONLINE); break; - case CHANNEL_ROLLERSHUTTER: handleRollershutterCommand(command); updateStatus(ThingStatus.ONLINE); break; - default: - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, - "Channel unknown " + channelUID.getId()); + logger.debug("unexpected command for channel {}", channelUID.getId()); } } @@ -217,23 +214,25 @@ public void initialize() { NikoHomeControlCommunication nhcComm = getCommunication(); if (nhcComm == null) { - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, - "Connection with controller not started yet, could not initialize action " + actionId); + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED, + "@text/offline.bridge-unitialized"); return; + } else { + updateStatus(ThingStatus.UNKNOWN); } // We need to do this in a separate thread because we may have to wait for the communication to become active scheduler.submit(() -> { if (!nhcComm.communicationActive()) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, - "No connection with controller, could not initialize action " + actionId); + "@text/offline.communication-error"); return; } NhcAction nhcAction = nhcComm.getActions().get(actionId); if (nhcAction == null) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, - "Action " + actionId + " does not match an action in the controller"); + "@text/offline.configuration-error.actionId"); return; } @@ -313,7 +312,7 @@ public void actionEvent(int actionState) { break; default: updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, - "Unknown action type " + actionType); + "@text/offline.configuration-error.actionType"); } } @@ -328,7 +327,7 @@ public void actionInitialized() { @Override public void actionRemoved() { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, - "Action " + actionId + " has been removed from the controller"); + "@text/offline.configuration-error.actionRemoved"); } private void restartCommunication(NikoHomeControlCommunication nhcComm) { @@ -337,35 +336,27 @@ private void restartCommunication(NikoHomeControlCommunication nhcComm) { nhcComm.restartCommunication(); // If still not active, take thing offline and return. if (!nhcComm.communicationActive()) { - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Communication error"); + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, + "@text/offline.communication-error"); return; } // Also put the bridge back online NikoHomeControlBridgeHandler nhcBridgeHandler = getBridgeHandler(); if (nhcBridgeHandler != null) { nhcBridgeHandler.bridgeOnline(); + } else { + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED, + "@text/offline.bridge-unitialized"); } } private @Nullable NikoHomeControlCommunication getCommunication() { NikoHomeControlBridgeHandler nhcBridgeHandler = getBridgeHandler(); - if (nhcBridgeHandler == null) { - updateStatus(ThingStatus.UNINITIALIZED, ThingStatusDetail.BRIDGE_UNINITIALIZED, - "No bridge initialized for action " + actionId); - return null; - } - NikoHomeControlCommunication nhcComm = nhcBridgeHandler.getCommunication(); - return nhcComm; + return nhcBridgeHandler != null ? nhcBridgeHandler.getCommunication() : null; } private @Nullable NikoHomeControlBridgeHandler getBridgeHandler() { Bridge nhcBridge = getBridge(); - if (nhcBridge == null) { - updateStatus(ThingStatus.UNINITIALIZED, ThingStatusDetail.BRIDGE_UNINITIALIZED, - "No bridge initialized for action " + actionId); - return null; - } - NikoHomeControlBridgeHandler nhcBridgeHandler = (NikoHomeControlBridgeHandler) nhcBridge.getHandler(); - return nhcBridgeHandler; + return nhcBridge != null ? (NikoHomeControlBridgeHandler) nhcBridge.getHandler() : null; } } diff --git a/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlBridgeHandler.java b/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlBridgeHandler.java index b7e4472ff5071..d035c9d543565 100644 --- a/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlBridgeHandler.java +++ b/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlBridgeHandler.java @@ -77,6 +77,8 @@ protected void startCommunication() { return; } + updateStatus(ThingStatus.UNKNOWN); + scheduler.submit(() -> { comm.startCommunication(); if (!comm.communicationActive()) { @@ -141,7 +143,7 @@ private void setupRefreshTimer(int refreshInterval) { */ protected void bridgeOffline() { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, - "Error with bridge connection"); + "@text/offline.communication-error"); } /** diff --git a/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlBridgeHandler1.java b/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlBridgeHandler1.java index e0938e1a2cdb1..8a7de65a62b18 100644 --- a/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlBridgeHandler1.java +++ b/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlBridgeHandler1.java @@ -12,6 +12,8 @@ */ package org.openhab.binding.nikohomecontrol.internal.handler; +import static org.openhab.binding.nikohomecontrol.internal.NikoHomeControlBindingConstants.THREAD_NAME_PREFIX; + import java.net.InetAddress; import java.util.HashMap; import java.util.Map; @@ -50,11 +52,12 @@ public void initialize() { logger.debug("bridge handler host {}, port {}", addr, port); if (addr != null) { - nhcComm = new NikoHomeControlCommunication1(this, scheduler); + String eventThreadName = THREAD_NAME_PREFIX + thing.getUID().getAsString(); + nhcComm = new NikoHomeControlCommunication1(this, scheduler, eventThreadName); startCommunication(); } else { - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, - "Cannot resolve bridge IP with hostname " + config.addr); + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR, + "@text/offline.configuration-error.ip"); } } diff --git a/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlBridgeHandler2.java b/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlBridgeHandler2.java index e6687926e96a2..e0063f2065410 100644 --- a/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlBridgeHandler2.java +++ b/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlBridgeHandler2.java @@ -69,14 +69,15 @@ public void initialize() { // advanced configuration, skipping token validation. // This behavior would allow the same logic to be used (with profile UUID) as before token validation // was introduced. - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR, "Token is empty"); + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR, + "@text/offline.configuration-error.tokenEmpty"); return; } } else { Date now = new Date(); if (expiryDate.before(now)) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR, - "Hobby api token has expired"); + "@text/offline.configuration-error.tokenExpired"); return; } } @@ -90,7 +91,7 @@ public void initialize() { } catch (CertificateException e) { // this should not happen unless there is a programming error updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, - "Not able to set SSL context"); + "@text/offline.communication-error"); return; } } diff --git a/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlEnergyMeterHandler.java b/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlEnergyMeterHandler.java index 637f065f5af01..96b1ef5c85e09 100644 --- a/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlEnergyMeterHandler.java +++ b/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlEnergyMeterHandler.java @@ -64,7 +64,7 @@ public void handleCommand(ChannelUID channelUID, Command command) { return; } - if (command == REFRESH) { + if (REFRESH.equals(command)) { energyMeterEvent(nhcEnergyMeter.getPower()); } } @@ -77,9 +77,11 @@ public void initialize() { NikoHomeControlCommunication nhcComm = getCommunication(); if (nhcComm == null) { - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, - "Connection with controller not started yet, could not initialize energy meter " + energyMeterId); + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED, + "@text/offline.bridge-unitialized"); return; + } else { + updateStatus(ThingStatus.UNKNOWN); } // We need to do this in a separate thread because we may have to wait for the @@ -87,14 +89,14 @@ public void initialize() { scheduler.submit(() -> { if (!nhcComm.communicationActive()) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, - "No connection with controller, could not initialize energy meter " + energyMeterId); + "@text/offline.communication-error"); return; } NhcEnergyMeter nhcEnergyMeter = nhcComm.getEnergyMeters().get(energyMeterId); if (nhcEnergyMeter == null) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, - "Energy meter " + energyMeterId + " does not match a energy meter in the controller"); + "@text/offline.configuration-error.energyMeterId"); return; } @@ -163,7 +165,7 @@ public void energyMeterInitialized() { @Override public void energyMeterRemoved() { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, - "Energy meter " + energyMeterId + " has been removed from the controller"); + "@text/offline.configuration-error.energyMeterRemoved"); } @Override @@ -171,47 +173,40 @@ public void energyMeterRemoved() { // the channel public void channelLinked(ChannelUID channelUID) { NikoHomeControlCommunication nhcComm = getCommunication(); - if (nhcComm == null) { - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, - "Bridge communication not initialized when trying to start energy meter " + energyMeterId); - return; + if (nhcComm != null) { + // This can be expensive, therefore do it in a job. + scheduler.submit(() -> { + if (!nhcComm.communicationActive()) { + restartCommunication(nhcComm); + } + + if (nhcComm.communicationActive()) { + nhcComm.startEnergyMeter(energyMeterId); + updateStatus(ThingStatus.ONLINE); + } + }); } - - // This can be expensive, therefore do it in a job. - scheduler.submit(() -> { - if (!nhcComm.communicationActive()) { - restartCommunication(nhcComm); - } - - if (nhcComm.communicationActive()) { - nhcComm.startEnergyMeter(energyMeterId); - updateStatus(ThingStatus.ONLINE); - } - }); } @Override public void channelUnlinked(ChannelUID channelUID) { NikoHomeControlCommunication nhcComm = getCommunication(); - if (nhcComm == null) { - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, - "Bridge communication not initialized when trying to stop energy meter " + energyMeterId); - return; + if (nhcComm != null) { + // This can be expensive, therefore do it in a job. + scheduler.submit(() -> { + if (!nhcComm.communicationActive()) { + restartCommunication(nhcComm); + } + + if (nhcComm.communicationActive()) { + nhcComm.stopEnergyMeter(energyMeterId); + // as this is momentary power production/consumption, we set it UNDEF as we do not get readings + // anymore + updateState(CHANNEL_POWER, UnDefType.UNDEF); + updateStatus(ThingStatus.ONLINE); + } + }); } - - // This can be expensive, therefore do it in a job. - scheduler.submit(() -> { - if (!nhcComm.communicationActive()) { - restartCommunication(nhcComm); - } - - if (nhcComm.communicationActive()) { - nhcComm.stopEnergyMeter(energyMeterId); - // as this is momentary power production/consumption, we set it UNDEF as we do not get readings anymore - updateState(CHANNEL_POWER, UnDefType.UNDEF); - updateStatus(ThingStatus.ONLINE); - } - }); } private void restartCommunication(NikoHomeControlCommunication nhcComm) { @@ -220,35 +215,27 @@ private void restartCommunication(NikoHomeControlCommunication nhcComm) { nhcComm.restartCommunication(); // If still not active, take thing offline and return. if (!nhcComm.communicationActive()) { - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Communication error"); + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, + "@text/offline.communication-error"); return; } // Also put the bridge back online NikoHomeControlBridgeHandler nhcBridgeHandler = getBridgeHandler(); if (nhcBridgeHandler != null) { nhcBridgeHandler.bridgeOnline(); + } else { + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED, + "@text/offline.bridge-unitialized"); } } private @Nullable NikoHomeControlCommunication getCommunication() { NikoHomeControlBridgeHandler nhcBridgeHandler = getBridgeHandler(); - if (nhcBridgeHandler == null) { - updateStatus(ThingStatus.UNINITIALIZED, ThingStatusDetail.BRIDGE_UNINITIALIZED, - "No bridge initialized for energy meter " + energyMeterId); - return null; - } - NikoHomeControlCommunication nhcComm = nhcBridgeHandler.getCommunication(); - return nhcComm; + return nhcBridgeHandler != null ? nhcBridgeHandler.getCommunication() : null; } private @Nullable NikoHomeControlBridgeHandler getBridgeHandler() { Bridge nhcBridge = getBridge(); - if (nhcBridge == null) { - updateStatus(ThingStatus.UNINITIALIZED, ThingStatusDetail.BRIDGE_UNINITIALIZED, - "No bridge initialized for energy meter " + energyMeterId); - return null; - } - NikoHomeControlBridgeHandler nhcBridgeHandler = (NikoHomeControlBridgeHandler) nhcBridge.getHandler(); - return nhcBridgeHandler; + return nhcBridge != null ? (NikoHomeControlBridgeHandler) nhcBridge.getHandler() : null; } } diff --git a/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlThermostatHandler.java b/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlThermostatHandler.java index d852f07ade143..4b71fa2760c95 100644 --- a/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlThermostatHandler.java +++ b/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/handler/NikoHomeControlThermostatHandler.java @@ -68,9 +68,8 @@ public NikoHomeControlThermostatHandler(Thing thing) { public void handleCommand(ChannelUID channelUID, Command command) { NikoHomeControlCommunication nhcComm = getCommunication(); if (nhcComm == null) { - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, - "Bridge communication not initialized when trying to execute thermostat command on " - + thermostatId); + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED, + "@text/offline.bridge-unitialized"); return; } @@ -143,10 +142,8 @@ private void handleCommandSelection(ChannelUID channelUID, Command command) { } updateStatus(ThingStatus.ONLINE); break; - default: - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, - "Channel unknown " + channelUID.getId()); + logger.debug("unexpected command for channel {}", channelUID.getId()); } } @@ -159,9 +156,11 @@ public void initialize() { NikoHomeControlCommunication nhcComm = getCommunication(); if (nhcComm == null) { - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, - "Connection with controller not started yet, could not initialize thermostat " + thermostatId); + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED, + "@text/offline.bridge-unitialized"); return; + } else { + updateStatus(ThingStatus.UNKNOWN); } // We need to do this in a separate thread because we may have to wait for the @@ -169,14 +168,14 @@ public void initialize() { scheduler.submit(() -> { if (!nhcComm.communicationActive()) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, - "No connection with controller, could not initialize thermostat " + thermostatId); + "@text/offline.communication-error"); return; } NhcThermostat nhcThermostat = nhcComm.getThermostats().get(thermostatId); if (nhcThermostat == null) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, - "Thermostat " + thermostatId + " does not match a thermostat in the controller"); + "@text/offline.configuration-error.thermostatId"); return; } @@ -288,7 +287,7 @@ public void thermostatInitialized() { @Override public void thermostatRemoved() { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, - "Thermostat " + thermostatId + " has been removed from the controller"); + "@text/offline.configuration-error.thermostatRemoved"); } private void restartCommunication(NikoHomeControlCommunication nhcComm) { @@ -297,35 +296,27 @@ private void restartCommunication(NikoHomeControlCommunication nhcComm) { nhcComm.restartCommunication(); // If still not active, take thing offline and return. if (!nhcComm.communicationActive()) { - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Communication error"); + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, + "@text/offline.communication-error"); return; } // Also put the bridge back online NikoHomeControlBridgeHandler nhcBridgeHandler = getBridgeHandler(); if (nhcBridgeHandler != null) { nhcBridgeHandler.bridgeOnline(); + } else { + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED, + "@text/offline.bridge-unitialized"); } } private @Nullable NikoHomeControlCommunication getCommunication() { NikoHomeControlBridgeHandler nhcBridgeHandler = getBridgeHandler(); - if (nhcBridgeHandler == null) { - updateStatus(ThingStatus.UNINITIALIZED, ThingStatusDetail.BRIDGE_UNINITIALIZED, - "No bridge initialized for thermostat " + thermostatId); - return null; - } - NikoHomeControlCommunication nhcComm = nhcBridgeHandler.getCommunication(); - return nhcComm; + return nhcBridgeHandler != null ? nhcBridgeHandler.getCommunication() : null; } private @Nullable NikoHomeControlBridgeHandler getBridgeHandler() { Bridge nhcBridge = getBridge(); - if (nhcBridge == null) { - updateStatus(ThingStatus.UNINITIALIZED, ThingStatusDetail.BRIDGE_UNINITIALIZED, - "No bridge initialized for thermostat " + thermostatId); - return null; - } - NikoHomeControlBridgeHandler nhcBridgeHandler = (NikoHomeControlBridgeHandler) nhcBridge.getHandler(); - return nhcBridgeHandler; + return nhcBridge != null ? (NikoHomeControlBridgeHandler) nhcBridge.getHandler() : null; } } diff --git a/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/protocol/NikoHomeControlDiscover.java b/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/protocol/NikoHomeControlDiscover.java index 708e399929f54..5b75ab8359fa0 100644 --- a/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/protocol/NikoHomeControlDiscover.java +++ b/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/protocol/NikoHomeControlDiscover.java @@ -105,10 +105,7 @@ public String getNhcBridgeId() { */ private boolean isNhc(DatagramPacket packet) { byte[] packetData = packet.getData(); - if ((packet.getLength() > 2) && (packetData[0] == 0x44)) { - return true; - } - return false; + return ((packet.getLength() > 2) && (packetData[0] == 0x44)); } /** diff --git a/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/protocol/nhc1/NikoHomeControlCommunication1.java b/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/protocol/nhc1/NikoHomeControlCommunication1.java index a2c8b7bd40908..c1f1c6b445dd5 100644 --- a/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/protocol/nhc1/NikoHomeControlCommunication1.java +++ b/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/protocol/nhc1/NikoHomeControlCommunication1.java @@ -12,6 +12,8 @@ */ package org.openhab.binding.nikohomecontrol.internal.protocol.nhc1; +import static org.openhab.binding.nikohomecontrol.internal.NikoHomeControlBindingConstants.THREAD_NAME_PREFIX; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @@ -56,6 +58,8 @@ public class NikoHomeControlCommunication1 extends NikoHomeControlCommunication private Logger logger = LoggerFactory.getLogger(NikoHomeControlCommunication1.class); + private String eventThreadName = THREAD_NAME_PREFIX; + private final NhcSystemInfo1 systemInfo = new NhcSystemInfo1(); private final Map locations = new ConcurrentHashMap<>(); @@ -77,9 +81,11 @@ public class NikoHomeControlCommunication1 extends NikoHomeControlCommunication * Niko Home Control IP-interface. * */ - public NikoHomeControlCommunication1(NhcControllerEvent handler, ScheduledExecutorService scheduler) { + public NikoHomeControlCommunication1(NhcControllerEvent handler, ScheduledExecutorService scheduler, + String eventThreadName) { super(handler); this.scheduler = scheduler; + this.eventThreadName = eventThreadName; // When we set up this object, we want to get the proper gson adapter set up once GsonBuilder gsonBuilder = new GsonBuilder(); @@ -113,11 +119,11 @@ public synchronized void startCommunication() { // Start Niko Home Control event listener. This listener will act on all messages coming from // IP-interface. - (new Thread(this::runNhcEvents)).start(); + (new Thread(this::runNhcEvents, eventThreadName)).start(); } catch (IOException | InterruptedException e) { stopCommunication(); - handler.controllerOffline("Error initializing communication"); + handler.controllerOffline("@text/offline.communication-error"); } } @@ -227,7 +233,7 @@ private synchronized void sendMessage(Object nhcMessage) { logger.debug("resend json {}", json); nhcOut.println(json); if (nhcOut.checkError()) { - handler.controllerOffline("Error resending message"); + handler.controllerOffline("@text/offline.communication-error"); } } } diff --git a/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/protocol/nhc2/NikoHomeControlCommunication2.java b/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/protocol/nhc2/NikoHomeControlCommunication2.java index d77caa49ba861..722346fcd74c5 100644 --- a/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/protocol/nhc2/NikoHomeControlCommunication2.java +++ b/bundles/org.openhab.binding.nikohomecontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/protocol/nhc2/NikoHomeControlCommunication2.java @@ -778,8 +778,7 @@ private void sendDeviceMessage(String topic, String gsonMessage) { mqttConnection.connectionPublish(topic, gsonMessage); } catch (MqttException e) { - String message = e.getMessage(); - message = (message != null) ? message : "Communication error"; + String message = e.getLocalizedMessage(); logger.debug("sending command failed, trying to restart communication"); restartCommunication(); @@ -791,12 +790,12 @@ private void sendDeviceMessage(String topic, String gsonMessage) { logger.debug("failed to restart communication"); } } catch (MqttException e1) { - message = e1.getMessage(); - message = (message != null) ? message : "Communication error"; + message = e1.getLocalizedMessage(); logger.debug("error resending device command"); } if (!communicationActive()) { + message = (message != null) ? message : "@text/offline.communication-error"; connectionLost(message); } } @@ -862,8 +861,8 @@ public String getServices() { public void connectionStateChanged(MqttConnectionState state, @Nullable Throwable error) { if (error != null) { logger.debug("Connection state: {}", state, error); - String message = error.getMessage(); - message = (message != null) ? message : "Error communicating with the controller"; + String message = error.getLocalizedMessage(); + message = (message != null) ? message : "@text/offline.communication-error"; if (!MqttConnectionState.CONNECTING.equals(state)) { // This is a connection loss, try to restart restartCommunication(); diff --git a/bundles/org.openhab.binding.nikohomecontrol/src/main/resources/OH-INF/binding/binding.xml b/bundles/org.openhab.binding.nikohomecontrol/src/main/resources/OH-INF/binding/binding.xml index fb1a80c26819e..e83e74d10aeda 100644 --- a/bundles/org.openhab.binding.nikohomecontrol/src/main/resources/OH-INF/binding/binding.xml +++ b/bundles/org.openhab.binding.nikohomecontrol/src/main/resources/OH-INF/binding/binding.xml @@ -3,6 +3,6 @@ xmlns:binding="https://openhab.org/schemas/binding/v1.0.0" xsi:schemaLocation="https://openhab.org/schemas/binding/v1.0.0 https://openhab.org/schemas/binding-1.0.0.xsd"> - Niko Home Control Binding - This is the binding for the Niko Home Control system + @text/bindingName + @text/bindingDescription diff --git a/bundles/org.openhab.binding.nikohomecontrol/src/main/resources/OH-INF/i18n/nikohomecontrol.properties b/bundles/org.openhab.binding.nikohomecontrol/src/main/resources/OH-INF/i18n/nikohomecontrol.properties new file mode 100644 index 0000000000000..e7c7a4ce0b8da --- /dev/null +++ b/bundles/org.openhab.binding.nikohomecontrol/src/main/resources/OH-INF/i18n/nikohomecontrol.properties @@ -0,0 +1,120 @@ +# binding +bindingName = Niko Home Control Binding +bindingDescription = This is the binding for the Niko Home Control system + +# bridge types +bridgeLabel = Niko Home Control I Bridge +bridgeDescription = This bridge represents a Niko Home Control I IP-interface + +bridge2Label = Niko Home Control II Bridge +bridge2Description = This bridge represents a Niko Home Control II Connected Controller or Wireless Smart Hub + +bridgeConfigAddressLabel = IP or Host Name +bridgeConfigAddressDescription = IP Address of Niko Home Control IP-interface +bridge2ConfigAddressDescription = IP Address of Connected Controller or Wireless Smart Hub + +bridgeConfigPortLabel = Bridge Port +bridgeConfigPortDescription = Port to communicate with Niko Home Control IP-interface, default 8000 +bridge2ConfigPortDescription = Port for secure MQTT communication with Connected Controller or Wireless Smart Hub, default 8884 + +bridge2ConfigProfileLabel = Profile +bridge2ConfigProfileDescription = Profile used in Niko Home Control II for hobby API + +bridge2ConfigPasswordLabel = API Token +bridge2ConfigPasswordDescription = Token for Niko Home Control II hobby API, should not be empty. This token will have to be renewed after expiration (1 year after creation) + +bridgeConfigRefreshLabel = Refresh Interval +bridgeConfigRefreshDescription = Refresh interval for connection with Niko Home Control IP-interface (min), default 300. If set to 0 or left empty, no refresh will be scheduled +bridge2ConfigRefreshDescription = Refresh interval for connection with Connected Controller or Wireless Smart Hub (min), default 300. If set to 0 or left empty, no refresh will be scheduled + +# thing types +pushButtonLabel = Pushbutton +pushButtonDescription = Pushbutton type action in Niko Home Control + +onOffLabel = Switch +onOffDescription = On/Off type action in Niko Home Control + +dimmerLabel = Dimmer +dimmerDescription = Dimmer type action in Niko Home Control + +blindLabel = Shutter +blindDescription = Rollershutter type action in Niko Home Control + +thermostatLabel = Thermostat +thermostatDescription = Thermostat in the Niko Home Control system + +energyMeterLabel = Energy Meter +energyMeterDescription = Energy meter in the Niko Home Control system + +actionConfigActionIdLabel = Action ID +actionConfigActionIdDescription = Niko Home Control action ID + +dimmerConfigStepLabel = Step Value +dimmerConfigStepDescription = Step value used for increase/decrease of dimmer brightness, default 10% + +blindConfigInvertLabel = Invert Direction +blindConfigInvertDescription = Invert rollershutter direction + +thermostatConfigThermostatIdLabel = Thermostat ID +thermostatConfigThermostatIdDescription = Niko Home Control Thermostat ID + +thermostatConfigOverruleTimeLabel = Overrule Time +thermostatConfigOverruleTimeDescription = Default overrule duration in minutes when an overrule temperature is set without providing overrule \ + time, 60 minutes by default + +energyMeterConfigEnergyMeterIdLabel = Energy Meter ID +energyMeterConfigEnergyMeterIdDescription = Niko Home Control Energy Meter ID + +#channel types +channelButtonLabel = Button +channelButtonDescription = Pushbutton control for action in Niko Home Control + +channelRollershutterLabel = Rollershutter +channelRollershutterDescription = Rollershutter control for rollershutter action in Niko Home Control + +channelMeasuredLabel = Measured +channelMeasuredDescription = Temperature measured by thermostat + +channelSetpointLabel = Setpoint +channelSetpointDescription = Setpoint temperature of thermostat + +channelOverruletimeLabel = Overrule Time +channelOverruletimeDescription = Time duration for overruling thermostat target temperature in min. + +channelModeLabel = Mode +channelModeDescription = Thermostat mode +channelModeOption0 = day +channelModeOption1 = night +channelModeOption2 = eco +channelModeOption3 = off +channelModeOption4 = cool +channelModeOption5 = prog 1 +channelModeOption6 = prog 2 +channelModeOption7 = prog 3 + +channelPowerLabel = Power +channelPowerDescription = Momentary power consumption/production (positive is consumption) + +channelAlarmLabel = Alarm +channelAlarmDescription = Alarm from Niko Home Control + +channelNoticeLabel = Notice +channelNoticeDescription = Notice from Niko Home Control + +# thing status messages +offline.configuration-error.ip = Cannot resolve bridge IP with given host name +offline.configuration-error.tokenEmpty = Hobby API token is empty +offline.configuration-error.tokenExpired = Hobby API token has expired + +offline.configuration-error.actionId = Configured action ID does not match an action in controller +offline.configuration-error.actionType = Unsupported action type +offline.configuration-error.actionRemoved = Action has been removed from controller + +offline.configuration-error.energyMeterId = Configured energy meter ID does not match an energy meter in controller +offline.configuration-error.energyMeterRemoved = Energy meter has been removed from controller + +offline.configuration-error.thermostatId = Configured thermostat ID does not match an thermostat in controller +offline.configuration-error.thermostatRemoved = Thermostat has been removed from controller + +offline.communication-error = Error communicating with controller +offline.bridge-unitialized = Bridge not initialized diff --git a/bundles/org.openhab.binding.nikohomecontrol/src/main/resources/OH-INF/thing/thing-types.xml b/bundles/org.openhab.binding.nikohomecontrol/src/main/resources/OH-INF/thing/thing-types.xml index 362b08fd33abd..e75e3a666f7be 100644 --- a/bundles/org.openhab.binding.nikohomecontrol/src/main/resources/OH-INF/thing/thing-types.xml +++ b/bundles/org.openhab.binding.nikohomecontrol/src/main/resources/OH-INF/thing/thing-types.xml @@ -5,70 +5,67 @@ xsi:schemaLocation="https://openhab.org/schemas/thing-description/v1.0.0 https://openhab.org/schemas/thing-description-1.0.0.xsd"> - - This bridge represents a Niko Home Control I IP-interface + + @text/bridgeDescription - - IP Address of Niko Home Control IP-interface + + @text/bridgeConfigAddressDescription false network-address - - Port to communicate with Niko Home Control IP-interface, default 8000 + + @text/bridgeConfigPortDescription 8000 true - - Refresh interval for connection with Niko Home Control IP-interface (min), default 300. If set to 0 or - left empty, no refresh will be scheduled + + @text/bridgeConfigRefreshDescription 300 true - - This bridge represents a Niko Home Control II Connected Controller + + @text/bridge2Description - - IP Address of Connected Controller + + @text/bridge2ConfigAddressDescription false network-address - - Port for secure MQTT communication with Connected Controller, default 8884 + + @text/bridge2ConfigPortDescription 8884 true - - Profile used in Niko Home Control II for hobby API + + @text/bridge2ConfigProfileDescription hobby true - - Token for Niko Home Control II hobby API, should not be empty. This token will have to be renewed after - expiration (1 year after creation) + + @text/bridge2ConfigPasswordDescription password - - Refresh interval for connection with Connected Controller (min), default 300. If set to 0 or left - empty, no refresh will be scheduled + + @text/bridge2ConfigRefreshDescription 300 true @@ -80,15 +77,15 @@ - - Pushbutton type action in Niko Home Control + + @text/pushButtonDescription - - Niko Home Control action ID + + @text/actionConfigActionIdDescription false @@ -98,15 +95,15 @@ - - On/Off type action in Niko Home Control + + @text/onOffDescription - - Niko Home Control action ID + + @text/actionConfigActionIdDescription false @@ -116,20 +113,20 @@ - - Dimmer type action in Niko Home Control + + @text/dimmerDescription - - Niko Home Control action ID + + @text/actionConfigActionIdDescription false - - Step value used for increase/decrease of dimmer brightness, default 10% + + @text/dimmerConfigStepValue 10 true @@ -140,20 +137,20 @@ - - Rollershutter type action in Niko Home Control + + @text/blindDescription - - Niko Home Control action ID + + @text/actionConfigActionIdDescription false - - Invert rollershutter direction + + @text/blindConfigInvertDescription false true @@ -164,8 +161,8 @@ - - Thermostat in the Niko Home Control system + + @textThermostatDescription @@ -174,14 +171,13 @@ - - Niko Home Control Thermostat ID + + @text/thermostatConfigThermostatIdDescription false - - Default overrule duration in minutes when an overrule temperature is set without providing overrule - time, 60 minutes by default + + @text/thermostatConfigOverruleTimeDescription 60 true @@ -191,15 +187,15 @@ - - Energy meter in the Niko Home Control system + + @text/energyMeterDescription - - Niko Home Control Energy Meter ID + + @text/energyMeterConfigEnergyMeterIdDescription false @@ -207,22 +203,22 @@ Switch - - Pushbutton control for action in Niko Home Control + + @text/channelButtonDescription Switch veto Rollershutter - - Rollershutter control for rollershutter action in Niko Home Control + + @text/channelRollershutterDescription Blinds Number:Temperature - - Temperature measured by thermostat + + @text/channelMeasuredDescription Temperature Measurement @@ -232,8 +228,8 @@ Number:Temperature - - Setpoint temperature of thermostat + + @text/channelSetpointDescription Temperature Setpoint @@ -243,33 +239,33 @@ Number - - Time duration for overruling thermostat target temperature in min. + + @text/channelOverruletimeDescription Number Number - - Thermostat mode + + @text/channelModeDescription Number - - - - - - - - + + + + + + + + Number:Power - - Momentary power consumption/production (positive is consumption) + + @text/channelPowerDescription Number @@ -277,13 +273,13 @@ trigger - - Alarm from Niko Home Control + + @text/channelAlarmDescription trigger - - Notice from Niko Home Control + + @text/channelNoticeDescription