[hdpowerview] Battery channels visible only when required (#13324)
* [hdpowerview] add batteryKind field * [hdpowerview] battery channels visible depending on batteryKind Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
This commit is contained in:
committed by
GitHub
parent
c825171caf
commit
f7295bd00f
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2022 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.hdpowerview.internal.api;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* An enum for the type of power supply in a shade.
|
||||
*
|
||||
* @author Andrew Fiddian-Green - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public enum BatteryKind {
|
||||
ERROR_UNKNOWN(-1),
|
||||
HARDWIRED_POWER_SUPPLY(1),
|
||||
BATTERY_WAND(2),
|
||||
RECHARGEABLE_BATTERY_WAND(3);
|
||||
|
||||
private int batteryKind;
|
||||
|
||||
private BatteryKind(int i) {
|
||||
this.batteryKind = i;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the BatteryKind by parsing the given string value.
|
||||
*
|
||||
* @param value the string to parse, or null.
|
||||
* @return the BatteryKind or ERROR_UNKNOWN in case of error.
|
||||
*/
|
||||
public static BatteryKind fromString(@Nullable String value) {
|
||||
if (value != null) {
|
||||
try {
|
||||
int intValue = Integer.parseInt(value);
|
||||
for (BatteryKind e : values()) {
|
||||
if (e.batteryKind == intValue) {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
// fall through
|
||||
}
|
||||
}
|
||||
return ERROR_UNKNOWN;
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ import java.util.List;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.binding.hdpowerview.internal.api.BatteryKind;
|
||||
import org.openhab.binding.hdpowerview.internal.api.Firmware;
|
||||
import org.openhab.binding.hdpowerview.internal.api.ShadePosition;
|
||||
|
||||
@@ -55,9 +56,15 @@ public class Shades {
|
||||
public @Nullable Integer capabilities;
|
||||
public @Nullable Firmware firmware;
|
||||
public @Nullable Firmware motor;
|
||||
// note: in old JSON batteryKind was a string but now it's a number; fortunately GSON string accepts either
|
||||
public @Nullable String batteryKind;
|
||||
|
||||
public String getName() {
|
||||
return new String(Base64.getDecoder().decode(name));
|
||||
}
|
||||
|
||||
public BatteryKind getBatteryKind() {
|
||||
return BatteryKind.fromString(batteryKind);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants;
|
||||
import org.openhab.binding.hdpowerview.internal.HDPowerViewWebTargets;
|
||||
import org.openhab.binding.hdpowerview.internal.api.BatteryKind;
|
||||
import org.openhab.binding.hdpowerview.internal.api.CoordinateSystem;
|
||||
import org.openhab.binding.hdpowerview.internal.api.Firmware;
|
||||
import org.openhab.binding.hdpowerview.internal.api.ShadePosition;
|
||||
@@ -274,7 +275,7 @@ public class HDPowerViewShadeHandler extends AbstractHubbedThingHandler {
|
||||
logger.debug("Caching capabilities {} for shade {}", capabilities.getValue(), shade.id);
|
||||
this.capabilities = capabilities;
|
||||
|
||||
updateDynamicChannels(capabilities);
|
||||
updateDynamicChannels(capabilities, shade);
|
||||
}
|
||||
|
||||
private Capabilities getCapabilitiesOrDefault() {
|
||||
@@ -618,9 +619,12 @@ public class HDPowerViewShadeHandler extends AbstractHubbedThingHandler {
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove previously statically created channels if the shade does not support them.
|
||||
* Remove previously statically created channels if the shade does not support them or they are not relevant.
|
||||
*
|
||||
* @param capabilities the capabilities of the shade.
|
||||
* @param shade the shade data.
|
||||
*/
|
||||
private void updateDynamicChannels(Capabilities capabilities) {
|
||||
private void updateDynamicChannels(Capabilities capabilities, ShadeData shade) {
|
||||
List<Channel> removeList = new ArrayList<>();
|
||||
|
||||
removeListProcessChannel(removeList, CHANNEL_SHADE_POSITION, capabilities.supportsPrimary());
|
||||
@@ -631,6 +635,10 @@ public class HDPowerViewShadeHandler extends AbstractHubbedThingHandler {
|
||||
removeListProcessChannel(removeList, CHANNEL_SHADE_VANE,
|
||||
capabilities.supportsTiltAnywhere() || capabilities.supportsTiltOnClosed());
|
||||
|
||||
boolean batteryChannelsRequired = shade.getBatteryKind() != BatteryKind.HARDWIRED_POWER_SUPPLY;
|
||||
removeListProcessChannel(removeList, CHANNEL_SHADE_BATTERY_LEVEL, batteryChannelsRequired);
|
||||
removeListProcessChannel(removeList, CHANNEL_SHADE_LOW_BATTERY, batteryChannelsRequired);
|
||||
|
||||
if (!removeList.isEmpty()) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
StringJoiner joiner = new StringJoiner(", ");
|
||||
|
||||
Reference in New Issue
Block a user