Refactor dynamic creation of zone channels (#16763)

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2024-05-14 22:42:57 +02:00 committed by GitHub
parent 73c6568547
commit bf24b4e4d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 55 deletions

View File

@ -13,8 +13,6 @@
package org.openhab.binding.denonmarantz.internal; package org.openhab.binding.denonmarantz.internal;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
@ -79,55 +77,28 @@ public class DenonMarantzBindingConstants {
public static final String CHANNEL_ZONE4_INPUT = "zone4#input"; public static final String CHANNEL_ZONE4_INPUT = "zone4#input";
// Map of Zone2 Channel Type UIDs (to be added to Thing later when needed) // Map of Zone2 Channel Type UIDs (to be added to Thing later when needed)
public static final Map<String, ChannelTypeUID> ZONE2_CHANNEL_TYPES = new LinkedHashMap<>(); public static final Map<String, ChannelTypeUID> ZONE2_CHANNEL_TYPES = Map.ofEntries(
static { Map.entry(CHANNEL_ZONE2_POWER, new ChannelTypeUID(BINDING_ID, "zonePower")),
ZONE2_CHANNEL_TYPES.put(CHANNEL_ZONE2_POWER, new ChannelTypeUID(BINDING_ID, "zonePower")); Map.entry(CHANNEL_ZONE2_VOLUME, new ChannelTypeUID(BINDING_ID, "volume")),
ZONE2_CHANNEL_TYPES.put(CHANNEL_ZONE2_VOLUME, new ChannelTypeUID(BINDING_ID, "volume")); Map.entry(CHANNEL_ZONE2_VOLUME_DB, new ChannelTypeUID(BINDING_ID, "volumeDB")),
ZONE2_CHANNEL_TYPES.put(CHANNEL_ZONE2_VOLUME_DB, new ChannelTypeUID(BINDING_ID, "volumeDB")); Map.entry(CHANNEL_ZONE2_MUTE, new ChannelTypeUID(BINDING_ID, "mute")),
ZONE2_CHANNEL_TYPES.put(CHANNEL_ZONE2_MUTE, new ChannelTypeUID(BINDING_ID, "mute")); Map.entry(CHANNEL_ZONE2_INPUT, new ChannelTypeUID(BINDING_ID, "input")));
ZONE2_CHANNEL_TYPES.put(CHANNEL_ZONE2_INPUT, new ChannelTypeUID(BINDING_ID, "input"));
}
// Map of Zone3 Channel Type UIDs (to be added to Thing later when needed) // Map of Zone3 Channel Type UIDs (to be added to Thing later when needed)
public static final Map<String, ChannelTypeUID> ZONE3_CHANNEL_TYPES = new LinkedHashMap<>(); public static final Map<String, ChannelTypeUID> ZONE3_CHANNEL_TYPES = Map.ofEntries(
static { Map.entry(CHANNEL_ZONE3_POWER, new ChannelTypeUID(BINDING_ID, "zonePower")),
ZONE3_CHANNEL_TYPES.put(CHANNEL_ZONE3_POWER, new ChannelTypeUID(BINDING_ID, "zonePower")); Map.entry(CHANNEL_ZONE3_VOLUME, new ChannelTypeUID(BINDING_ID, "volume")),
ZONE3_CHANNEL_TYPES.put(CHANNEL_ZONE3_VOLUME, new ChannelTypeUID(BINDING_ID, "volume")); Map.entry(CHANNEL_ZONE3_VOLUME_DB, new ChannelTypeUID(BINDING_ID, "volumeDB")),
ZONE3_CHANNEL_TYPES.put(CHANNEL_ZONE3_VOLUME_DB, new ChannelTypeUID(BINDING_ID, "volumeDB")); Map.entry(CHANNEL_ZONE3_MUTE, new ChannelTypeUID(BINDING_ID, "mute")),
ZONE3_CHANNEL_TYPES.put(CHANNEL_ZONE3_MUTE, new ChannelTypeUID(BINDING_ID, "mute")); Map.entry(CHANNEL_ZONE3_INPUT, new ChannelTypeUID(BINDING_ID, "input")));
ZONE3_CHANNEL_TYPES.put(CHANNEL_ZONE3_INPUT, new ChannelTypeUID(BINDING_ID, "input"));
}
// Map of Zone4 Channel Type UIDs (to be added to Thing later when needed) // Map of Zone4 Channel Type UIDs (to be added to Thing later when needed)
public static final Map<String, ChannelTypeUID> ZONE4_CHANNEL_TYPES = new LinkedHashMap<>(); public static final Map<String, ChannelTypeUID> ZONE4_CHANNEL_TYPES = Map.ofEntries(
static { Map.entry(CHANNEL_ZONE4_POWER, new ChannelTypeUID(BINDING_ID, "zonePower")),
ZONE4_CHANNEL_TYPES.put(CHANNEL_ZONE4_POWER, new ChannelTypeUID(BINDING_ID, "zonePower")); Map.entry(CHANNEL_ZONE4_VOLUME, new ChannelTypeUID(BINDING_ID, "volume")),
ZONE4_CHANNEL_TYPES.put(CHANNEL_ZONE4_VOLUME, new ChannelTypeUID(BINDING_ID, "volume")); Map.entry(CHANNEL_ZONE4_VOLUME_DB, new ChannelTypeUID(BINDING_ID, "volumeDB")),
ZONE4_CHANNEL_TYPES.put(CHANNEL_ZONE4_VOLUME_DB, new ChannelTypeUID(BINDING_ID, "volumeDB")); Map.entry(CHANNEL_ZONE4_MUTE, new ChannelTypeUID(BINDING_ID, "mute")),
ZONE4_CHANNEL_TYPES.put(CHANNEL_ZONE4_MUTE, new ChannelTypeUID(BINDING_ID, "mute")); Map.entry(CHANNEL_ZONE4_INPUT, new ChannelTypeUID(BINDING_ID, "input")));
ZONE4_CHANNEL_TYPES.put(CHANNEL_ZONE4_INPUT, new ChannelTypeUID(BINDING_ID, "input"));
}
/**
* Static mapping of ChannelType-to-ItemType (workaround while waiting for
* https://github.com/eclipse/smarthome/issues/4950 as yet there is no convenient way to extract the item type from
* thing-types.xml)
* See https://github.com/eclipse/smarthome/pull/4787#issuecomment-362287430
*/
public static final Map<String, String> CHANNEL_ITEM_TYPES = new HashMap<>();
static {
CHANNEL_ITEM_TYPES.put(CHANNEL_ZONE2_POWER, "Switch");
CHANNEL_ITEM_TYPES.put(CHANNEL_ZONE2_VOLUME, "Dimmer");
CHANNEL_ITEM_TYPES.put(CHANNEL_ZONE2_VOLUME_DB, "Number");
CHANNEL_ITEM_TYPES.put(CHANNEL_ZONE2_MUTE, "Switch");
CHANNEL_ITEM_TYPES.put(CHANNEL_ZONE2_INPUT, "String");
CHANNEL_ITEM_TYPES.put(CHANNEL_ZONE3_POWER, "Switch");
CHANNEL_ITEM_TYPES.put(CHANNEL_ZONE3_VOLUME, "Dimmer");
CHANNEL_ITEM_TYPES.put(CHANNEL_ZONE3_VOLUME_DB, "Number");
CHANNEL_ITEM_TYPES.put(CHANNEL_ZONE3_MUTE, "Switch");
CHANNEL_ITEM_TYPES.put(CHANNEL_ZONE3_INPUT, "String");
}
// Offset in dB from the actual dB value to the volume as presented by the AVR (0 == -80 dB) // Offset in dB from the actual dB value to the volume as presented by the AVR (0 == -80 dB)
public static final BigDecimal DB_OFFSET = new BigDecimal("80"); public static final BigDecimal DB_OFFSET = new BigDecimal("80");

View File

@ -53,7 +53,8 @@ import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus; import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail; import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.binding.BaseThingHandler; import org.openhab.core.thing.binding.BaseThingHandler;
import org.openhab.core.thing.binding.builder.ChannelBuilder; import org.openhab.core.thing.binding.ThingHandlerCallback;
import org.openhab.core.thing.type.ChannelKind;
import org.openhab.core.thing.type.ChannelTypeUID; import org.openhab.core.thing.type.ChannelTypeUID;
import org.openhab.core.types.Command; import org.openhab.core.types.Command;
import org.openhab.core.types.RefreshType; import org.openhab.core.types.RefreshType;
@ -376,14 +377,17 @@ public class DenonMarantzHandler extends BaseThingHandler implements DenonMarant
// add the channels that were not yet added // add the channels that were not yet added
if (!channelsToAdd.isEmpty()) { if (!channelsToAdd.isEmpty()) {
for (Entry<String, ChannelTypeUID> entry : channelsToAdd) { ThingHandlerCallback callback = getCallback();
String itemType = CHANNEL_ITEM_TYPES.get(entry.getKey()); if (callback != null) {
Channel channel = ChannelBuilder for (Entry<String, ChannelTypeUID> entry : channelsToAdd) {
.create(new ChannelUID(this.getThing().getUID(), entry.getKey()), itemType) ChannelUID channelUID = new ChannelUID(this.getThing().getUID(), entry.getKey());
.withType(entry.getValue()).build(); channels.add(callback.createChannelBuilder(channelUID, entry.getValue())
channels.add(channel); .withKind(ChannelKind.STATE).build());
}
channelsUpdated = true;
} else {
logger.warn("Could not create zone channels");
} }
channelsUpdated = true;
} else { } else {
logger.debug("No zone channels have been added"); logger.debug("No zone channels have been added");
} }