diff --git a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/PlugwiseHAController.java b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/PlugwiseHAController.java index 3aead2f977164..d697eb28faaf7 100644 --- a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/PlugwiseHAController.java +++ b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/PlugwiseHAController.java @@ -101,10 +101,13 @@ public void start(Runnable callback) throws PlugwiseHAException { callback.run(); } + /** + * Refreshes all changed objects. Will result in a call to the remote service. + * + * @throws PlugwiseHAException + */ public void refresh() throws PlugwiseHAException { - synchronized (this) { - this.getUpdatedDomainObjects(); - } + domainObjects = this.getUpdatedDomainObjects(); } // Public API methods @@ -113,7 +116,7 @@ public GatewayInfo getGatewayInfo() throws PlugwiseHAException { return getGatewayInfo(false); } - public GatewayInfo getGatewayInfo(Boolean forceRefresh) throws PlugwiseHAException { + private synchronized GatewayInfo getGatewayInfo(Boolean forceRefresh) throws PlugwiseHAException { GatewayInfo gatewayInfo = null; DomainObjects localDomainObjects = this.domainObjects; if (localDomainObjects != null) { @@ -133,12 +136,12 @@ public GatewayInfo getGatewayInfo(Boolean forceRefresh) throws PlugwiseHAExcepti DomainObjects domainObjects = executeRequest(request); this.gatewayUpdateDateTime = ZonedDateTime.parse(request.getServerDateTime(), PlugwiseHAController.FORMAT); - - return mergeDomainObjects(domainObjects).getGatewayInfo(); + domainObjects = mergeDomainObjects(domainObjects); + return domainObjects.getGatewayInfo(); } } - public Appliances getAppliances(Boolean forceRefresh) throws PlugwiseHAException { + private synchronized Appliances getAppliances(Boolean forceRefresh) throws PlugwiseHAException { Appliances appliances = null; DomainObjects localDomainObjects = this.domainObjects; if (localDomainObjects != null) { @@ -167,6 +170,13 @@ public Appliances getAppliances(Boolean forceRefresh) throws PlugwiseHAException } } + /** + * Gets the appliance with the givven id. May result in a call to the remote service. + * + * @param id of the appliance + * @return Appliance may be null + * @throws PlugwiseHAException + */ public @Nullable Appliance getAppliance(String id) throws PlugwiseHAException { Appliances appliances = this.getAppliances(false); if (!appliances.containsKey(id)) { @@ -181,7 +191,7 @@ public Appliances getAppliances(Boolean forceRefresh) throws PlugwiseHAException } } - public Locations getLocations(Boolean forceRefresh) throws PlugwiseHAException { + private synchronized Locations getLocations(Boolean forceRefresh) throws PlugwiseHAException { Locations locations = null; DomainObjects localDomainObjects = this.domainObjects; if (localDomainObjects != null) { @@ -209,6 +219,13 @@ public Locations getLocations(Boolean forceRefresh) throws PlugwiseHAException { } } + /** + * Gets the location with the givven id. May result in a call to the remote service. + * + * @param id of the location + * @return Location may be null + * @throws PlugwiseHAException + */ public @Nullable Location getLocation(String id) throws PlugwiseHAException { Locations locations = this.getLocations(false); if (!locations.containsKey(id)) { @@ -223,7 +240,13 @@ public Locations getLocations(Boolean forceRefresh) throws PlugwiseHAException { } } - public @Nullable DomainObjects getDomainObjects() throws PlugwiseHAException { + /** + * Gets and caches all objects from the remote service resulting in a call to the remote service. + * + * @return up to date DomainObjects may be null + * @throws PlugwiseHAException + */ + public synchronized @Nullable DomainObjects getAndMergeDomainObjects() throws PlugwiseHAException { PlugwiseHAControllerRequest request; request = newRequest(DomainObjects.class, this.domainObjectsTransformer); @@ -239,27 +262,24 @@ public Locations getLocations(Boolean forceRefresh) throws PlugwiseHAException { return mergeDomainObjects(domainObjects); } - public @Nullable DomainObjects getUpdatedDomainObjects() throws PlugwiseHAException { + private @Nullable DomainObjects getUpdatedDomainObjects() throws PlugwiseHAException { ZonedDateTime localGatewayUpdateDateTime = this.gatewayUpdateDateTime; ZonedDateTime localGatewayFullUpdateDateTime = this.gatewayFullUpdateDateTime; if (localGatewayUpdateDateTime == null || localGatewayFullUpdateDateTime == null) { - return getDomainObjects(); + return getAndMergeDomainObjects(); } else if (localGatewayUpdateDateTime.isBefore(ZonedDateTime.now().minusSeconds(maxAgeSecondsRefresh))) { - return getUpdatedDomainObjects(localGatewayUpdateDateTime); + return getUpdatedAndMergeDomainObjects(localGatewayUpdateDateTime.toEpochSecond()); } else if (localGatewayFullUpdateDateTime .isBefore(ZonedDateTime.now().minusMinutes(MAX_AGE_MINUTES_FULL_REFRESH))) { - return getDomainObjects(); + return getAndMergeDomainObjects(); } else { return null; } } - public @Nullable DomainObjects getUpdatedDomainObjects(ZonedDateTime since) throws PlugwiseHAException { - return getUpdatedDomainObjects(since.toEpochSecond()); - } - - public @Nullable DomainObjects getUpdatedDomainObjects(Long since) throws PlugwiseHAException { + private synchronized @Nullable DomainObjects getUpdatedAndMergeDomainObjects(Long since) + throws PlugwiseHAException { PlugwiseHAControllerRequest request; request = newRequest(DomainObjects.class, this.domainObjectsTransformer); @@ -276,7 +296,7 @@ public Locations getLocations(Boolean forceRefresh) throws PlugwiseHAException { return mergeDomainObjects(domainObjects); } - public void setLocationThermostat(Location location, Double temperature) throws PlugwiseHAException { + public synchronized void setLocationThermostat(Location location, Double temperature) throws PlugwiseHAException { PlugwiseHAControllerRequest request = newRequest(Void.class); Optional thermostat = location.getActuatorFunctionalities().getFunctionalityThermostat(); @@ -291,7 +311,7 @@ public void setLocationThermostat(Location location, Double temperature) throws } } - public void setThermostat(Appliance appliance, Double temperature) throws PlugwiseHAException { + public synchronized void setThermostat(Appliance appliance, Double temperature) throws PlugwiseHAException { PlugwiseHAControllerRequest request = newRequest(Void.class); Optional thermostat = appliance.getActuatorFunctionalities() .getFunctionalityThermostat(); @@ -307,7 +327,7 @@ public void setThermostat(Appliance appliance, Double temperature) throws Plugwi } } - public void setOffsetTemperature(Appliance appliance, Double temperature) throws PlugwiseHAException { + public synchronized void setOffsetTemperature(Appliance appliance, Double temperature) throws PlugwiseHAException { PlugwiseHAControllerRequest request = newRequest(Void.class); Optional offsetTemperatureFunctionality = appliance.getActuatorFunctionalities() .getFunctionalityOffsetTemperature(); @@ -323,7 +343,7 @@ public void setOffsetTemperature(Appliance appliance, Double temperature) throws } } - public void setPreHeating(Location location, Boolean state) throws PlugwiseHAException { + public synchronized void setPreHeating(Location location, Boolean state) throws PlugwiseHAException { PlugwiseHAControllerRequest request = newRequest(Void.class); Optional thermostat = location.getActuatorFunctionalities().getFunctionalityThermostat(); @@ -335,7 +355,7 @@ public void setPreHeating(Location location, Boolean state) throws PlugwiseHAExc executeRequest(request); } - public void setAllowCooling(Location location, Boolean state) throws PlugwiseHAException { + public synchronized void setAllowCooling(Location location, Boolean state) throws PlugwiseHAException { PlugwiseHAControllerRequest request = newRequest(Void.class); Optional thermostat = location.getActuatorFunctionalities().getFunctionalityThermostat(); @@ -347,7 +367,7 @@ public void setAllowCooling(Location location, Boolean state) throws PlugwiseHAE executeRequest(request); } - public void setRegulationControl(Location location, String state) throws PlugwiseHAException { + public synchronized void setRegulationControl(Location location, String state) throws PlugwiseHAException { List allowStates = Arrays.asList("active", "passive", "off"); if (!allowStates.contains(state.toLowerCase())) { this.logger.warn("Trying to set the regulation control to an invalid state"); @@ -365,7 +385,7 @@ public void setRegulationControl(Location location, String state) throws Plugwis executeRequest(request); } - public void setRelay(Appliance appliance, Boolean state) throws PlugwiseHAException { + public synchronized void setRelay(Appliance appliance, Boolean state) throws PlugwiseHAException { PlugwiseHAControllerRequest request = newRequest(Void.class); request.setPath("/core/appliances"); @@ -375,7 +395,7 @@ public void setRelay(Appliance appliance, Boolean state) throws PlugwiseHAExcept executeRequest(request); } - public void setRelayLock(Appliance appliance, Boolean state) throws PlugwiseHAException { + public synchronized void setRelayLock(Appliance appliance, Boolean state) throws PlugwiseHAException { PlugwiseHAControllerRequest request = newRequest(Void.class); request.setPath("/core/appliances"); @@ -385,7 +405,7 @@ public void setRelayLock(Appliance appliance, Boolean state) throws PlugwiseHAEx executeRequest(request); } - public void setPresetScene(Location location, String state) throws PlugwiseHAException { + public synchronized void setPresetScene(Location location, String state) throws PlugwiseHAException { List allowStates = Arrays.asList("home", "asleep", "away", "vacation", "no_frost"); if (!allowStates.contains(state.toLowerCase())) { this.logger.warn("Trying to set the preset scene to an invalid state"); @@ -409,19 +429,6 @@ public void setPresetScene(Location location, String state) throws PlugwiseHAExc executeRequest(request); } - public ZonedDateTime ping() throws PlugwiseHAException { - PlugwiseHAControllerRequest request; - - request = newRequest(Void.class, null); - - request.setPath("/cache/gateways"); - request.addPathParameter("ping"); - - executeRequest(request); - - return ZonedDateTime.parse(request.getServerDateTime(), PlugwiseHAController.FORMAT); - } - // Protected and private methods private static Transformer setXSLT(StreamSource xsltSource) throws PlugwiseHAException { @@ -444,16 +451,13 @@ private PlugwiseHAControllerRequest newRequest(Class responseType) { @SuppressWarnings("null") private T executeRequest(PlugwiseHAControllerRequest request) throws PlugwiseHAException { - T result; - result = request.execute(); - return result; + return request.execute(); } private DomainObjects mergeDomainObjects(@Nullable DomainObjects updatedDomainObjects) { DomainObjects localDomainObjects = this.domainObjects; if (localDomainObjects == null && updatedDomainObjects != null) { - this.domainObjects = updatedDomainObjects; - return updatedDomainObjects; + return this.domainObjects = updatedDomainObjects; } else if (localDomainObjects != null && updatedDomainObjects == null) { return localDomainObjects; } else if (localDomainObjects != null && updatedDomainObjects != null) { @@ -467,7 +471,6 @@ private DomainObjects mergeDomainObjects(@Nullable DomainObjects updatedDomainOb if (locations != null) { localDomainObjects.mergeLocations(locations); } - this.domainObjects = localDomainObjects; return localDomainObjects; } else { return new DomainObjects(); diff --git a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/PlugwiseHAControllerRequest.java b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/PlugwiseHAControllerRequest.java index 71cf83647f3a1..cbe160fb5e8be 100644 --- a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/PlugwiseHAControllerRequest.java +++ b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/PlugwiseHAControllerRequest.java @@ -72,6 +72,7 @@ public class PlugwiseHAControllerRequest { private static final String CONTENT_TYPE_TEXT_XML = MimeTypes.Type.TEXT_XML_8859_1.toString(); private static final long TIMEOUT_SECONDS = 5; private static final int REQUEST_MAX_RETRY_COUNT = 3; + private static final int RETRY_DELAY_TIMOUT = 3000; private final Logger logger = LoggerFactory.getLogger(PlugwiseHAControllerRequest.class); private final XStream xStream; @@ -252,6 +253,12 @@ private ContentResponse getContentResponse(int retries) throws PlugwiseHAExcepti } catch (TimeoutException e) { if (retries > 0) { this.logger.debug("TimeoutException occured, remaining retries {}", retries - 1); + try { + Thread.sleep(RETRY_DELAY_TIMOUT); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + throw new PlugwiseHATimeoutException(ie); + } return getContentResponse(retries - 1); } else { throw new PlugwiseHATimeoutException(e); diff --git a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/converter/DateTimeConverter.java b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/converter/DateTimeConverter.java index 1a3813b35c5ec..c744889bfec5c 100644 --- a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/converter/DateTimeConverter.java +++ b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/converter/DateTimeConverter.java @@ -38,10 +38,7 @@ public class DateTimeConverter extends AbstractSingleValueConverter { @Override public boolean canConvert(@Nullable @SuppressWarnings("rawtypes") Class type) { - if (type == null) { - return false; - } - return ZonedDateTime.class.isAssignableFrom(type); + return (type == null) ? false : ZonedDateTime.class.isAssignableFrom(type); } @Override diff --git a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/ActuatorFunctionalityThermostat.java b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/ActuatorFunctionalityThermostat.java index a0586f303ebbf..931e13b5bcfbf 100644 --- a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/ActuatorFunctionalityThermostat.java +++ b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/ActuatorFunctionalityThermostat.java @@ -24,15 +24,12 @@ public class ActuatorFunctionalityThermostat extends ActuatorFunctionality { @SuppressWarnings("unused") private Double setpoint; - @SuppressWarnings("unused") @XStreamAlias("preheating_allowed") private Boolean preheatingAllowed; - @SuppressWarnings("unused") @XStreamAlias("cooling_allowed") private Boolean coolingAllowed; - @SuppressWarnings("unused") @XStreamAlias("regulation_control") private String regulationControl; diff --git a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/DomainObjects.java b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/DomainObjects.java index d447cee4c4e9c..26cafcb1a89c2 100644 --- a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/DomainObjects.java +++ b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/DomainObjects.java @@ -30,7 +30,6 @@ public class DomainObjects { @XStreamImplicit(itemFieldName = "location", keyFieldName = "id") private Locations locations = new Locations(); - @SuppressWarnings("unused") @XStreamImplicit(itemFieldName = "module", keyFieldName = "id") private Modules modules = new Modules(); @@ -47,7 +46,9 @@ public Locations getLocations() { } public Appliances mergeAppliances(Appliances updatedAppliances) { - if (updatedAppliances != null) { + if (appliances == null) { + this.appliances = updatedAppliances; + } else if (updatedAppliances != null) { this.appliances.merge(updatedAppliances); } @@ -55,7 +56,9 @@ public Appliances mergeAppliances(Appliances updatedAppliances) { } public Locations mergeLocations(Locations updatedLocations) { - if (updatedLocations != null) { + if (locations == null) { + this.locations = updatedLocations; + } else if (updatedLocations != null) { this.locations.merge(updatedLocations); } diff --git a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/Log.java b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/Log.java index 8a1ef23b0d199..2cef71064c3c4 100644 --- a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/Log.java +++ b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/Log.java @@ -76,7 +76,7 @@ public Optional getMeasurementAsDouble() { } public Optional getMeasurementUnit() { - return Optional.ofNullable(unit); + return Optional.ofNullable(unit != null && !unit.isBlank() ? unit : null); } public ZonedDateTime getMeasurementDate() { diff --git a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/Module.java b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/Module.java index dd9cccbc84315..026fba256e6cc 100644 --- a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/Module.java +++ b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/Module.java @@ -30,7 +30,6 @@ @XStreamAlias("module") public class Module extends PlugwiseBaseModel implements PlugwiseComparableDate { - @SuppressWarnings("unused") @XStreamImplicit(itemFieldName = "service", keyFieldName = "id") private Services services; diff --git a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/Service.java b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/Service.java index 91911c1c3ec42..c64e88d638d53 100644 --- a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/Service.java +++ b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/Service.java @@ -20,11 +20,9 @@ @XStreamAlias("service") public class Service extends PlugwiseBaseModel { - @SuppressWarnings("unused") @XStreamAlias("log_type") private String logType; - @SuppressWarnings("unused") @XStreamAlias("point_log") private String pointLogId; } diff --git a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/discovery/PlugwiseHADiscoveryService.java b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/discovery/PlugwiseHADiscoveryService.java index 051a85bc13339..8227e38e1e4f5 100644 --- a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/discovery/PlugwiseHADiscoveryService.java +++ b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/discovery/PlugwiseHADiscoveryService.java @@ -99,7 +99,7 @@ private void discoverDomainObjects() throws PlugwiseHAException { PlugwiseHAController controller = thingHandler.getController(); if (controller != null) { - DomainObjects domainObjects = controller.getDomainObjects(); + DomainObjects domainObjects = controller.getAndMergeDomainObjects(); if (domainObjects != null) { for (Location location : domainObjects.getLocations().values()) { diff --git a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/handler/PlugwiseHAApplianceHandler.java b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/handler/PlugwiseHAApplianceHandler.java index 57e38ccccef68..ccfd84d92fc9d 100644 --- a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/handler/PlugwiseHAApplianceHandler.java +++ b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/handler/PlugwiseHAApplianceHandler.java @@ -146,10 +146,8 @@ protected void handleCommand(Appliance entity, ChannelUID channelUID, Command co } break; case APPLIANCE_OFFSET_CHANNEL: - if (command instanceof QuantityType quantityCommand) { - Unit unit = entity.getOffsetTemperatureUnit().orElse(UNIT_CELSIUS).equals(UNIT_CELSIUS) - ? SIUnits.CELSIUS - : ImperialUnits.FAHRENHEIT; + if (command instanceof QuantityType quantityCommand) { + Unit unit = getRemoteTemperatureUnit(entity); QuantityType state = quantityCommand.toUnit(unit); if (state != null) { @@ -172,9 +170,8 @@ protected void handleCommand(Appliance entity, ChannelUID channelUID, Command co } break; case APPLIANCE_SETPOINT_CHANNEL: - if (command instanceof QuantityType quantityCommand) { - Unit unit = entity.getSetpointTemperatureUnit().orElse(UNIT_CELSIUS) - .equals(UNIT_CELSIUS) ? SIUnits.CELSIUS : ImperialUnits.FAHRENHEIT; + if (command instanceof QuantityType quantityCommand) { + Unit unit = getRemoteTemperatureUnit(entity); QuantityType state = quantityCommand.toUnit(unit); if (state != null) { @@ -228,6 +225,11 @@ private State getDefaultState(String channelID) { return state; } + private Unit getRemoteTemperatureUnit(Appliance entity) { + return UNIT_CELSIUS.equals(entity.getDHWTempUnit().orElse(UNIT_CELSIUS)) ? SIUnits.CELSIUS + : ImperialUnits.FAHRENHEIT; + } + @Override protected void refreshChannel(Appliance entity, ChannelUID channelUID) { String channelID = channelUID.getIdWithoutGroup(); @@ -280,9 +282,7 @@ protected void refreshChannel(Appliance entity, ChannelUID channelUID) { break; case APPLIANCE_OFFSET_CHANNEL: if (entity.getOffsetTemperature().isPresent()) { - Unit unit = entity.getOffsetTemperatureUnit().orElse(UNIT_CELSIUS).equals(UNIT_CELSIUS) - ? SIUnits.CELSIUS - : ImperialUnits.FAHRENHEIT; + Unit unit = getRemoteTemperatureUnit(entity); state = new QuantityType<>(entity.getOffsetTemperature().get(), unit); } break; @@ -298,16 +298,13 @@ protected void refreshChannel(Appliance entity, ChannelUID channelUID) { break; case APPLIANCE_SETPOINT_CHANNEL: if (entity.getSetpointTemperature().isPresent()) { - Unit unit = entity.getSetpointTemperatureUnit().orElse(UNIT_CELSIUS) - .equals(UNIT_CELSIUS) ? SIUnits.CELSIUS : ImperialUnits.FAHRENHEIT; + Unit unit = getRemoteTemperatureUnit(entity); state = new QuantityType<>(entity.getSetpointTemperature().get(), unit); } break; case APPLIANCE_TEMPERATURE_CHANNEL: if (entity.getTemperature().isPresent()) { - Unit unit = entity.getTemperatureUnit().orElse(UNIT_CELSIUS).equals(UNIT_CELSIUS) - ? SIUnits.CELSIUS - : ImperialUnits.FAHRENHEIT; + Unit unit = getRemoteTemperatureUnit(entity); state = new QuantityType<>(entity.getTemperature().get(), unit); } break; @@ -330,8 +327,7 @@ protected void refreshChannel(Appliance entity, ChannelUID channelUID) { break; case APPLIANCE_INTENDEDBOILERTEMP_CHANNEL: if (entity.getIntendedBoilerTemp().isPresent()) { - Unit unit = entity.getIntendedBoilerTempUnit().orElse(UNIT_CELSIUS) - .equals(UNIT_CELSIUS) ? SIUnits.CELSIUS : ImperialUnits.FAHRENHEIT; + Unit unit = getRemoteTemperatureUnit(entity); state = new QuantityType<>(entity.getIntendedBoilerTemp().get(), unit); } break; @@ -358,17 +354,13 @@ protected void refreshChannel(Appliance entity, ChannelUID channelUID) { break; case APPLIANCE_RETURNWATERTEMPERATURE_CHANNEL: if (entity.getBoilerTemp().isPresent()) { - Unit unit = entity.getReturnWaterTempUnit().orElse(UNIT_CELSIUS).equals(UNIT_CELSIUS) - ? SIUnits.CELSIUS - : ImperialUnits.FAHRENHEIT; + Unit unit = getRemoteTemperatureUnit(entity); state = new QuantityType<>(entity.getReturnWaterTemp().get(), unit); } break; case APPLIANCE_DHWTEMPERATURE_CHANNEL: if (entity.getDHWTemp().isPresent()) { - Unit unit = entity.getDHWTempUnit().orElse(UNIT_CELSIUS).equals(UNIT_CELSIUS) - ? SIUnits.CELSIUS - : ImperialUnits.FAHRENHEIT; + Unit unit = getRemoteTemperatureUnit(entity); state = new QuantityType<>(entity.getDHWTemp().get(), unit); } break; @@ -379,25 +371,19 @@ protected void refreshChannel(Appliance entity, ChannelUID channelUID) { break; case APPLIANCE_BOILERTEMPERATURE_CHANNEL: if (entity.getBoilerTemp().isPresent()) { - Unit unit = entity.getBoilerTempUnit().orElse(UNIT_CELSIUS).equals(UNIT_CELSIUS) - ? SIUnits.CELSIUS - : ImperialUnits.FAHRENHEIT; + Unit unit = getRemoteTemperatureUnit(entity); state = new QuantityType<>(entity.getBoilerTemp().get(), unit); } break; case APPLIANCE_DHWSETPOINT_CHANNEL: if (entity.getDHTSetpoint().isPresent()) { - Unit unit = entity.getDHTSetpointUnit().orElse(UNIT_CELSIUS).equals(UNIT_CELSIUS) - ? SIUnits.CELSIUS - : ImperialUnits.FAHRENHEIT; + Unit unit = getRemoteTemperatureUnit(entity); state = new QuantityType<>(entity.getDHTSetpoint().get(), unit); } break; case APPLIANCE_MAXBOILERTEMPERATURE_CHANNEL: if (entity.getMaxBoilerTemp().isPresent()) { - Unit unit = entity.getMaxBoilerTempUnit().orElse(UNIT_CELSIUS).equals(UNIT_CELSIUS) - ? SIUnits.CELSIUS - : ImperialUnits.FAHRENHEIT; + Unit unit = getRemoteTemperatureUnit(entity); state = new QuantityType<>(entity.getMaxBoilerTemp().get(), unit); } break; diff --git a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/handler/PlugwiseHAZoneHandler.java b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/handler/PlugwiseHAZoneHandler.java index 0155e76b74a75..fc12171b5404e 100644 --- a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/handler/PlugwiseHAZoneHandler.java +++ b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/handler/PlugwiseHAZoneHandler.java @@ -31,6 +31,7 @@ import org.openhab.binding.plugwiseha.internal.api.model.PlugwiseHAController; import org.openhab.binding.plugwiseha.internal.api.model.dto.Location; import org.openhab.binding.plugwiseha.internal.config.PlugwiseHAThingConfig; +import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.QuantityType; import org.openhab.core.library.types.StringType; @@ -108,6 +109,11 @@ protected synchronized void initialize(PlugwiseHAThingConfig config, PlugwiseHAB return controller.getLocation(config.getId()); } + private Unit getRemoteTemperatureUnit(Location entity) { + return UNIT_CELSIUS.equals(entity.getTemperatureUnit().orElse(UNIT_CELSIUS)) ? SIUnits.CELSIUS + : ImperialUnits.FAHRENHEIT; + } + @Override protected void handleCommand(Location entity, ChannelUID channelUID, Command command) throws PlugwiseHAException { String channelID = channelUID.getIdWithoutGroup(); @@ -127,17 +133,19 @@ protected void handleCommand(Location entity, ChannelUID channelUID, Command com } break; case ZONE_SETPOINT_CHANNEL: - if (command instanceof QuantityType quantityCommand) { - Unit unit = entity.getSetpointTemperatureUnit().orElse(UNIT_CELSIUS) - .equals(UNIT_CELSIUS) ? SIUnits.CELSIUS : ImperialUnits.FAHRENHEIT; - QuantityType state = quantityCommand.toUnit(unit); - if (state != null) { - try { - controller.setLocationThermostat(entity, state.doubleValue()); - } catch (PlugwiseHAException e) { - logger.warn("Unable to update setpoint for zone '{}': {} -> {}", entity.getName(), - entity.getSetpointTemperature().orElse(null), state.doubleValue()); - } + Unit remoteUnit = getRemoteTemperatureUnit(entity); + QuantityType state = null; + if (command instanceof QuantityType quantityCommand) { + state = quantityCommand.toUnit(remoteUnit); + } else if (command instanceof DecimalType decimalCommand) { + state = new QuantityType<>(decimalCommand.doubleValue(), remoteUnit); + } + if (state != null) { + try { + controller.setLocationThermostat(entity, state.doubleValue()); + } catch (PlugwiseHAException e) { + logger.warn("Unable to update setpoint for zone '{}': {} -> {}", entity.getName(), + entity.getSetpointTemperature().orElse(null), state.doubleValue()); } } break; @@ -216,8 +224,7 @@ protected void refreshChannel(Location entity, ChannelUID channelUID) { break; case ZONE_SETPOINT_CHANNEL: if (entity.getSetpointTemperature().isPresent()) { - Unit unit = entity.getSetpointTemperatureUnit().orElse(UNIT_CELSIUS) - .equals(UNIT_CELSIUS) ? SIUnits.CELSIUS : ImperialUnits.FAHRENHEIT; + Unit unit = getRemoteTemperatureUnit(entity); state = new QuantityType<>(entity.getSetpointTemperature().get(), unit); } break; @@ -229,9 +236,7 @@ protected void refreshChannel(Location entity, ChannelUID channelUID) { break; case ZONE_TEMPERATURE_CHANNEL: if (entity.getTemperature().isPresent()) { - Unit unit = entity.getTemperatureUnit().orElse(UNIT_CELSIUS).equals(UNIT_CELSIUS) - ? SIUnits.CELSIUS - : ImperialUnits.FAHRENHEIT; + Unit unit = getRemoteTemperatureUnit(entity); state = new QuantityType<>(entity.getTemperature().get(), unit); } break; diff --git a/bundles/org.openhab.binding.plugwiseha/src/main/resources/OH-INF/config/config.xml b/bundles/org.openhab.binding.plugwiseha/src/main/resources/OH-INF/config/config.xml index 712cbce465c24..8ad358168d894 100644 --- a/bundles/org.openhab.binding.plugwiseha/src/main/resources/OH-INF/config/config.xml +++ b/bundles/org.openhab.binding.plugwiseha/src/main/resources/OH-INF/config/config.xml @@ -23,11 +23,11 @@ The Smile ID is the 8 letter code on the sticker on the back of the Adam boiler gateway - + seconds Refresh interval in seconds - 5 + 15 true