Skip to content

Commit

Permalink
[mqtt.homeassistant] Remove newStyleChannels temporary back-compat
Browse files Browse the repository at this point in the history
This includes removing deprecated channel IDs controlled by the same flag.

Signed-off-by: Cody Cutrer <[email protected]>
  • Loading branch information
ccutrer committed Dec 15, 2024
1 parent 639a1cb commit 1d8c037
Show file tree
Hide file tree
Showing 49 changed files with 245 additions and 523 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public class DiscoverComponents implements MqttMessageSubscriber {
private final ScheduledExecutorService scheduler;
private final ChannelStateUpdateListener updateListener;
private final AvailabilityTracker tracker;
private final boolean newStyleChannels;

protected final CompletableFuture<@Nullable Void> discoverFinishedFuture = new CompletableFuture<>();
private final Gson gson;
Expand Down Expand Up @@ -84,15 +83,14 @@ public static interface ComponentDiscovered {
*/
public DiscoverComponents(ThingUID thingUID, ScheduledExecutorService scheduler,
ChannelStateUpdateListener channelStateUpdateListener, AvailabilityTracker tracker, Gson gson,
Jinjava jinjava, UnitProvider unitProvider, boolean newStyleChannels) {
Jinjava jinjava, UnitProvider unitProvider) {
this.thingUID = thingUID;
this.scheduler = scheduler;
this.updateListener = channelStateUpdateListener;
this.gson = gson;
this.jinjava = jinjava;
this.unitProvider = unitProvider;
this.tracker = tracker;
this.newStyleChannels = newStyleChannels;
}

@Override
Expand All @@ -108,7 +106,7 @@ public void processMessage(String topic, byte[] payload) {
if (config.length() > 0) {
try {
component = ComponentFactory.createComponent(thingUID, haID, config, updateListener, tracker, scheduler,
gson, jinjava, unitProvider, newStyleChannels);
gson, jinjava, unitProvider);
component.setConfigSeen();

logger.trace("Found HomeAssistant component {}", haID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,19 +187,9 @@ public String toShortTopic() {
*
* @return group id
*/
public String getGroupId(@Nullable final String uniqueId, boolean newStyleChannels) {
public String getGroupId(@Nullable final String uniqueId) {
String result = uniqueId;

// newStyleChannels are auto-discovered things with openHAB >= 4.3.0
// assuming the topic has both a node ID and an object ID, simply use
// the component type and object ID - without encoding(!)
// since the only character allowed in object IDs but not allowed in UID
// is `-`. It also doesn't need to be reversible, so it's okay to just
// collapse `-` to `_`.
if (!nodeID.isBlank() && newStyleChannels) {
return component + "_" + objectID.replace('-', '_');
}

// the null test is only here so the compile knows, result is not null afterwards
if (result == null || result.isBlank()) {
StringBuilder str = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ public BigDecimal getDefaultPrecision() {
protected final C channelConfiguration;

protected boolean configSeen;
protected final boolean newStyleChannels;
protected final String uniqueId;
protected @Nullable String groupId;
protected String componentId;
Expand All @@ -121,14 +120,10 @@ public BigDecimal getDefaultPrecision() {
*
* @param componentConfiguration generic componentConfiguration with not parsed JSON config
* @param clazz target configuration type
* @param newStyleChannels if new style channels should be used
* @param singleChannelComponent if this component only ever has one channel, so should never be in a group
* (only if newStyleChannels is true)
*/
public AbstractComponent(ComponentFactory.ComponentConfiguration componentConfiguration, Class<C> clazz,
boolean newStyleChannels) {
public AbstractComponent(ComponentFactory.ComponentConfiguration componentConfiguration, Class<C> clazz) {
this.componentConfiguration = componentConfiguration;
this.newStyleChannels = newStyleChannels;

this.channelConfigurationJson = componentConfiguration.getConfigJSON();
this.channelConfiguration = componentConfiguration.getConfig(clazz);
Expand All @@ -137,18 +132,11 @@ public AbstractComponent(ComponentFactory.ComponentConfiguration componentConfig
this.haID = componentConfiguration.getHaID();

String name = channelConfiguration.getName();
if (newStyleChannels) {
// try for a simple component/group ID first; if there are conflicts
// (components of different types, but the same object id)
// we'll resolve them later
groupId = componentId = haID.objectID.replace('-', '_');
} else if (name != null && !name.isEmpty()) {
groupId = componentId = this.haID.getGroupId(channelConfiguration.getUniqueId(), false);
} else {
groupId = null;
componentId = "";
}
uniqueId = haID.component + "_" + haID.getGroupId(channelConfiguration.getUniqueId(), false);
// try for a simple component/group ID first; if there are conflicts
// (components of different types, but the same object id)
// we'll resolve them later
groupId = componentId = haID.objectID.replace('-', '_');
uniqueId = haID.component + "_" + haID.getGroupId(channelConfiguration.getUniqueId());

this.configSeen = false;

Expand Down Expand Up @@ -199,9 +187,6 @@ protected void addJsonAttributesChannel() {

protected void finalizeChannels() {
addJsonAttributesChannel();
if (!newStyleChannels) {
return;
}
if (channels.size() == 1) {
groupId = null;
channels.values().forEach(c -> c.resetUID(buildChannelUID(componentId)));
Expand All @@ -214,7 +199,7 @@ protected void finalizeChannels() {
}

public void resolveConflict() {
if (newStyleChannels && channels.size() == 1) {
if (channels.size() == 1) {
componentId = componentId + "_" + haID.component;
channels.values().forEach(c -> c.resetUID(buildChannelUID(componentId)));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ abstract class AbstractRawSchemaLight extends Light {
protected ComponentChannel rawChannel;
protected TextValue colorModeValue;

public AbstractRawSchemaLight(ComponentFactory.ComponentConfiguration builder, boolean newStyleChannels) {
super(builder, newStyleChannels);
public AbstractRawSchemaLight(ComponentFactory.ComponentConfiguration builder) {
super(builder);
hiddenChannels.add(rawChannel = buildChannel(RAW_CHANNEL_ID, ComponentChannelType.STRING, new TextValue(),
"Raw state", this).stateTopic(channelConfiguration.stateTopic)
.commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
@NonNullByDefault
public class AlarmControlPanel extends AbstractComponent<AlarmControlPanel.ChannelConfiguration> {
public static final String STATE_CHANNEL_ID = "state";
public static final String STATE_CHANNEL_ID_DEPRECATED = "alarm";
public static final String SWITCH_DISARM_CHANNEL_ID = "disarm";
public static final String SWITCH_ARM_HOME_CHANNEL_ID = "armhome";
public static final String SWITCH_ARM_AWAY_CHANNEL_ID = "armaway";
Expand Down Expand Up @@ -93,8 +92,8 @@ static class ChannelConfiguration extends AbstractChannelConfiguration {
FEATURE_ARM_VACATION, FEATURE_ARM_CUSTOM_BYPASS, FEATURE_TRIGGER);
}

public AlarmControlPanel(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
super(componentConfiguration, ChannelConfiguration.class, newStyleChannels);
public AlarmControlPanel(ComponentFactory.ComponentConfiguration componentConfiguration) {
super(componentConfiguration, ChannelConfiguration.class);

List<String> stateEnum = new ArrayList(List.of(STATE_DISARMED, STATE_TRIGGERED, STATE_ARMING, STATE_DISARMING,
STATE_PENDING, STATE_TRIGGERED));
Expand Down Expand Up @@ -124,35 +123,18 @@ public AlarmControlPanel(ComponentFactory.ComponentConfiguration componentConfig
}

String commandTopic = channelConfiguration.commandTopic;
TextValue value = (newStyleChannels && commandTopic != null)
TextValue value = (commandTopic != null)
? new TextValue(stateEnum.toArray(new String[0]), commandEnum.toArray(new String[0]))
: new TextValue(stateEnum.toArray(new String[0]));
var builder = buildChannel(newStyleChannels ? STATE_CHANNEL_ID : STATE_CHANNEL_ID_DEPRECATED,
ComponentChannelType.STRING, value, getName(), componentConfiguration.getUpdateListener())
var builder = buildChannel(STATE_CHANNEL_ID, ComponentChannelType.STRING, value, getName(),
componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate());

if (newStyleChannels && commandTopic != null) {
if (commandTopic != null) {
builder.commandTopic(commandTopic, channelConfiguration.isRetain(), channelConfiguration.getQos());
}
builder.build();

if (!newStyleChannels && commandTopic != null) {
buildChannel(SWITCH_DISARM_CHANNEL_ID, ComponentChannelType.STRING,
new TextValue(new String[] { channelConfiguration.payloadDisarm }), getName(),
componentConfiguration.getUpdateListener())
.commandTopic(commandTopic, channelConfiguration.isRetain(), channelConfiguration.getQos()).build();

buildChannel(SWITCH_ARM_HOME_CHANNEL_ID, ComponentChannelType.STRING,
new TextValue(new String[] { channelConfiguration.payloadArmHome }), getName(),
componentConfiguration.getUpdateListener())
.commandTopic(commandTopic, channelConfiguration.isRetain(), channelConfiguration.getQos()).build();

buildChannel(SWITCH_ARM_AWAY_CHANNEL_ID, ComponentChannelType.STRING,
new TextValue(new String[] { channelConfiguration.payloadArmAway }), getName(),
componentConfiguration.getUpdateListener())
.commandTopic(commandTopic, channelConfiguration.isRetain(), channelConfiguration.getQos()).build();
}

finalizeChannels();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ static class ChannelConfiguration extends AbstractChannelConfiguration {
protected String payloadOff = "OFF";
}

public BinarySensor(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
super(componentConfiguration, ChannelConfiguration.class, newStyleChannels);
public BinarySensor(ComponentFactory.ComponentConfiguration componentConfiguration) {
super(componentConfiguration, ChannelConfiguration.class);

OnOffValue value = new OnOffValue(channelConfiguration.payloadOn, channelConfiguration.payloadOff);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ static class ChannelConfiguration extends AbstractChannelConfiguration {
protected String payloadPress = "PRESS";
}

public Button(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
super(componentConfiguration, ChannelConfiguration.class, newStyleChannels);
public Button(ComponentFactory.ComponentConfiguration componentConfiguration) {
super(componentConfiguration, ChannelConfiguration.class);

TextValue value = new TextValue(new String[] { channelConfiguration.payloadPress });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ static class ChannelConfiguration extends AbstractChannelConfiguration {
protected String topic = "";
}

public Camera(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
super(componentConfiguration, ChannelConfiguration.class, newStyleChannels);
public Camera(ComponentFactory.ComponentConfiguration componentConfiguration) {
super(componentConfiguration, ChannelConfiguration.class);

ImageValue value = new ImageValue();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,17 @@ public class Climate extends AbstractComponent<Climate.ChannelConfiguration> {
public static final String ACTION_CH_ID = "action";
public static final String AUX_CH_ID = "aux";
public static final String AWAY_MODE_CH_ID = "away-mode";
public static final String AWAY_MODE_CH_ID_DEPRECATED = "awayMode";
public static final String CURRENT_HUMIDITY_CH_ID = "current-humidity";
public static final String CURRENT_TEMPERATURE_CH_ID = "current-temperature";
public static final String CURRENT_TEMPERATURE_CH_ID_DEPRECATED = "currentTemperature";
public static final String FAN_MODE_CH_ID = "fan-mode";
public static final String FAN_MODE_CH_ID_DEPRECATED = "fanMode";
public static final String HOLD_CH_ID = "hold";
public static final String MODE_CH_ID = "mode";
public static final String PRESET_MODE_CH_ID = "preset-mode";
public static final String SWING_CH_ID = "swing";
public static final String TARGET_HUMIDITY_CH_ID = "target-humidity";
public static final String TEMPERATURE_CH_ID = "temperature";
public static final String TEMPERATURE_HIGH_CH_ID = "temperature-high";
public static final String TEMPERATURE_HIGH_CH_ID_DEPRECATED = "temperatureHigh";
public static final String TEMPERATURE_LOW_CH_ID = "temperature-low";
public static final String TEMPERATURE_LOW_CH_ID_DEPRECATED = "temperatureLow";
public static final String POWER_CH_ID = "power";

private static final String ACTION_OFF = "off";
Expand Down Expand Up @@ -224,8 +219,8 @@ static class ChannelConfiguration extends AbstractChannelConfiguration {
protected Boolean sendIfOff = true;
}

public Climate(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
super(componentConfiguration, ChannelConfiguration.class, newStyleChannels);
public Climate(ComponentFactory.ComponentConfiguration componentConfiguration) {
super(componentConfiguration, ChannelConfiguration.class);

TemperatureUnit temperatureUnit = channelConfiguration.temperatureUnit;
if (channelConfiguration.temperatureUnit == null) {
Expand All @@ -250,21 +245,20 @@ public Climate(ComponentFactory.ComponentConfiguration componentConfiguration, b
channelConfiguration.auxCommandTopic, channelConfiguration.auxStateTemplate,
channelConfiguration.auxStateTopic, commandFilter);

buildOptionalChannel(newStyleChannels ? AWAY_MODE_CH_ID : AWAY_MODE_CH_ID_DEPRECATED,
ComponentChannelType.SWITCH, new OnOffValue(), updateListener, null,
buildOptionalChannel(AWAY_MODE_CH_ID, ComponentChannelType.SWITCH, new OnOffValue(), updateListener, null,
channelConfiguration.awayModeCommandTopic, channelConfiguration.awayModeStateTemplate,
channelConfiguration.awayModeStateTopic, commandFilter);

buildOptionalChannel(CURRENT_HUMIDITY_CH_ID, ComponentChannelType.HUMIDITY,
new NumberValue(new BigDecimal(0), new BigDecimal(100), null, Units.PERCENT), updateListener, null,
null, channelConfiguration.currentHumidityTemplate, channelConfiguration.currentHumidityTopic, null);

buildOptionalChannel(newStyleChannels ? CURRENT_TEMPERATURE_CH_ID : CURRENT_TEMPERATURE_CH_ID_DEPRECATED,
ComponentChannelType.TEMPERATURE, new NumberValue(null, null, precision, temperatureUnit.getUnit()),
updateListener, null, null, channelConfiguration.currentTemperatureTemplate,
channelConfiguration.currentTemperatureTopic, commandFilter);
buildOptionalChannel(CURRENT_TEMPERATURE_CH_ID, ComponentChannelType.TEMPERATURE,
new NumberValue(null, null, precision, temperatureUnit.getUnit()), updateListener, null, null,
channelConfiguration.currentTemperatureTemplate, channelConfiguration.currentTemperatureTopic,
commandFilter);

buildOptionalChannel(newStyleChannels ? FAN_MODE_CH_ID : FAN_MODE_CH_ID_DEPRECATED, ComponentChannelType.STRING,
buildOptionalChannel(FAN_MODE_CH_ID, ComponentChannelType.STRING,
new TextValue(channelConfiguration.fanModes.toArray(new String[0])), updateListener,
channelConfiguration.fanModeCommandTemplate, channelConfiguration.fanModeCommandTopic,
channelConfiguration.fanModeStateTemplate, channelConfiguration.fanModeStateTopic, commandFilter);
Expand Down Expand Up @@ -306,16 +300,14 @@ ComponentChannelType.TEMPERATURE, new NumberValue(null, null, precision, tempera
channelConfiguration.temperatureCommandTopic, channelConfiguration.temperatureStateTemplate,
channelConfiguration.temperatureStateTopic, commandFilter);

buildOptionalChannel(newStyleChannels ? TEMPERATURE_HIGH_CH_ID : TEMPERATURE_HIGH_CH_ID_DEPRECATED,
ComponentChannelType.TEMPERATURE,
buildOptionalChannel(TEMPERATURE_HIGH_CH_ID, ComponentChannelType.TEMPERATURE,
new NumberValue(channelConfiguration.minTemp, channelConfiguration.maxTemp,
channelConfiguration.tempStep, temperatureUnit.getUnit()),
updateListener, channelConfiguration.temperatureHighCommandTemplate,
channelConfiguration.temperatureHighCommandTopic, channelConfiguration.temperatureHighStateTemplate,
channelConfiguration.temperatureHighStateTopic, commandFilter);

buildOptionalChannel(newStyleChannels ? TEMPERATURE_LOW_CH_ID : TEMPERATURE_LOW_CH_ID_DEPRECATED,
ComponentChannelType.TEMPERATURE,
buildOptionalChannel(TEMPERATURE_LOW_CH_ID, ComponentChannelType.TEMPERATURE,
new NumberValue(channelConfiguration.minTemp, channelConfiguration.maxTemp,
channelConfiguration.tempStep, temperatureUnit.getUnit()),
updateListener, channelConfiguration.temperatureLowCommandTemplate,
Expand Down
Loading

0 comments on commit 1d8c037

Please sign in to comment.