Skip to content

Commit

Permalink
[mqtt.homeassistant] bring channel ids inline with coding standards (o…
Browse files Browse the repository at this point in the history
…penhab#17604)

but only for newStyleChannels, which is already a breaking change of
most channels getting renamed for 4.3 anyway

Signed-off-by: Cody Cutrer <cody@cutrer.us>
  • Loading branch information
ccutrer authored and KaaNee committed Nov 8, 2024
1 parent 2974a95 commit 2c89e60
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,20 @@
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 = "awayMode";
public static final String CURRENT_TEMPERATURE_CH_ID = "currentTemperature";
public static final String FAN_MODE_CH_ID = "fanMode";
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_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 SWING_CH_ID = "swing";
public static final String TEMPERATURE_CH_ID = "temperature";
public static final String TEMPERATURE_HIGH_CH_ID = "temperatureHigh";
public static final String TEMPERATURE_LOW_CH_ID = "temperatureLow";
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";

public enum TemperatureUnit {
Expand Down Expand Up @@ -231,16 +236,18 @@ public Climate(ComponentFactory.ComponentConfiguration componentConfiguration, b
channelConfiguration.auxCommandTopic, channelConfiguration.auxStateTemplate,
channelConfiguration.auxStateTopic, commandFilter);

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

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

buildOptionalChannel(FAN_MODE_CH_ID, ComponentChannelType.STRING,
buildOptionalChannel(newStyleChannels ? FAN_MODE_CH_ID : FAN_MODE_CH_ID_DEPRECATED, ComponentChannelType.STRING,
new TextValue(channelConfiguration.fanModes.toArray(new String[0])), updateListener,
channelConfiguration.fanModeCommandTemplate, channelConfiguration.fanModeCommandTopic,
channelConfiguration.fanModeStateTemplate, channelConfiguration.fanModeStateTopic, commandFilter);
Expand Down Expand Up @@ -270,14 +277,16 @@ public Climate(ComponentFactory.ComponentConfiguration componentConfiguration, b
channelConfiguration.temperatureCommandTopic, channelConfiguration.temperatureStateTemplate,
channelConfiguration.temperatureStateTopic, commandFilter);

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

buildOptionalChannel(TEMPERATURE_LOW_CH_ID, ComponentChannelType.NUMBER,
buildOptionalChannel(newStyleChannels ? TEMPERATURE_LOW_CH_ID : TEMPERATURE_LOW_CH_ID_DEPRECATED,
ComponentChannelType.NUMBER,
new NumberValue(channelConfiguration.minTemp, channelConfiguration.maxTemp,
channelConfiguration.tempStep, channelConfiguration.temperatureUnit.getUnit()),
updateListener, channelConfiguration.temperatureLowCommandTemplate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ public DefaultSchemaLight(ComponentFactory.ComponentConfiguration builder, boole
protected void buildChannels() {
AutoUpdatePolicy autoUpdatePolicy = optimistic ? AutoUpdatePolicy.RECOMMEND : null;
ComponentChannel localOnOffChannel;
localOnOffChannel = onOffChannel = buildChannel(ON_OFF_CHANNEL_ID, ComponentChannelType.SWITCH, onOffValue,
"On/Off State", this)
localOnOffChannel = onOffChannel = buildChannel(
newStyleChannels ? SWITCH_CHANNEL_ID : SWITCH_CHANNEL_ID_DEPRECATED, ComponentChannelType.SWITCH,
onOffValue, "On/Off State", this)
.stateTopic(channelConfiguration.stateTopic, channelConfiguration.stateValueTemplate)
.commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
channelConfiguration.getQos())
Expand All @@ -91,14 +92,15 @@ protected void buildChannels() {
}

if (channelConfiguration.colorModeStateTopic != null) {
buildChannel(COLOR_MODE_CHANNEL_ID, ComponentChannelType.STRING, new TextValue(), "Current color mode",
this)
buildChannel(newStyleChannels ? COLOR_MODE_CHANNEL_ID : COLOR_MODE_CHANNEL_ID_DEPRECATED,
ComponentChannelType.STRING, new TextValue(), "Current color mode", this)
.stateTopic(channelConfiguration.colorModeStateTopic, channelConfiguration.colorModeValueTemplate)
.inferOptimistic(channelConfiguration.optimistic).build();
}

if (channelConfiguration.colorTempStateTopic != null || channelConfiguration.colorTempCommandTopic != null) {
buildChannel(COLOR_TEMP_CHANNEL_ID, ComponentChannelType.NUMBER, colorTempValue, "Color Temperature", this)
buildChannel(newStyleChannels ? COLOR_TEMP_CHANNEL_ID : COLOR_TEMP_CHANNEL_ID_DEPRECATED,
ComponentChannelType.NUMBER, colorTempValue, "Color Temperature", this)
.stateTopic(channelConfiguration.colorTempStateTopic, channelConfiguration.colorTempValueTemplate)
.commandTopic(channelConfiguration.colorTempCommandTopic, channelConfiguration.isRetain(),
channelConfiguration.getQos())
Expand Down Expand Up @@ -178,7 +180,7 @@ protected void buildChannels() {
hiddenChannels.add(localOnOffChannel);
channels.put(BRIGHTNESS_CHANNEL_ID, localBrightnessChannel);
} else {
channels.put(ON_OFF_CHANNEL_ID, localOnOffChannel);
channels.put(newStyleChannels ? SWITCH_CHANNEL_ID : SWITCH_CHANNEL_ID_DEPRECATED, localOnOffChannel);
}
}

Expand Down Expand Up @@ -327,7 +329,8 @@ public void updateChannelState(ChannelUID channel, State state) {
} else {
listener.updateChannelState(primaryChannelUID, state);
}
} else if (id.equals(COLOR_TEMP_CHANNEL_ID) || channel.getIdWithoutGroup().equals(EFFECT_CHANNEL_ID)) {
} else if (id.equals(newStyleChannels ? COLOR_TEMP_CHANNEL_ID : COLOR_TEMP_CHANNEL_ID_DEPRECATED)
|| channel.getIdWithoutGroup().equals(EFFECT_CHANNEL_ID)) {
// Real channels; pass through
listener.updateChannelState(channel, state);
} else if (id.equals(HS_CHANNEL_ID) || id.equals(XY_CHANNEL_ID)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
public class Fan extends AbstractComponent<Fan.ChannelConfiguration> implements ChannelStateUpdateListener {
public static final String SWITCH_CHANNEL_ID = "fan";
public static final String SPEED_CHANNEL_ID = "speed";
public static final String PRESET_MODE_CHANNEL_ID = "preset_mode";
public static final String PRESET_MODE_CHANNEL_ID = "preset-mode";
public static final String OSCILLATION_CHANNEL_ID = "oscillation";
public static final String DIRECTION_CHANNEL_ID = "direction";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,17 @@ protected void buildChannels() {
}

if (supportedColorModes.contains(LightColorMode.COLOR_MODE_COLOR_TEMP)) {
colorTempChannel = buildChannel(COLOR_TEMP_CHANNEL_ID, ComponentChannelType.NUMBER, colorTempValue,
"Color Temperature", this).commandTopic(DUMMY_TOPIC, true, 1)
.commandFilter(command -> handleColorTempCommand(command))
colorTempChannel = buildChannel(
newStyleChannels ? COLOR_TEMP_CHANNEL_ID : COLOR_TEMP_CHANNEL_ID_DEPRECATED,
ComponentChannelType.NUMBER, colorTempValue, "Color Temperature", this)
.commandTopic(DUMMY_TOPIC, true, 1).commandFilter(command -> handleColorTempCommand(command))
.withAutoUpdatePolicy(autoUpdatePolicy).build();

if (hasColorChannel) {
colorModeValue = new TextValue(
supportedColorModes.stream().map(LightColorMode::serializedName).toArray(String[]::new));
buildChannel(COLOR_MODE_CHANNEL_ID, ComponentChannelType.STRING, colorModeValue, "Color Mode", this)
buildChannel(newStyleChannels ? COLOR_MODE_CHANNEL_ID : COLOR_MODE_CHANNEL_ID_DEPRECATED,
ComponentChannelType.STRING, colorModeValue, "Color Mode", this)
.withAutoUpdatePolicy(autoUpdatePolicy).isAdvanced(true).build();

}
Expand All @@ -115,9 +117,9 @@ protected void buildChannels() {
"Brightness", this).commandTopic(DUMMY_TOPIC, true, 1).commandFilter(this::handleCommand)
.withAutoUpdatePolicy(autoUpdatePolicy).build();
} else {
onOffChannel = buildChannel(ON_OFF_CHANNEL_ID, ComponentChannelType.SWITCH, onOffValue, "On/Off State",
this).commandTopic(DUMMY_TOPIC, true, 1).commandFilter(this::handleCommand)
.withAutoUpdatePolicy(autoUpdatePolicy).build();
onOffChannel = buildChannel(newStyleChannels ? SWITCH_CHANNEL_ID : SWITCH_CHANNEL_ID_DEPRECATED,
ComponentChannelType.SWITCH, onOffValue, "On/Off State", this).commandTopic(DUMMY_TOPIC, true, 1)
.commandFilter(this::handleCommand).withAutoUpdatePolicy(autoUpdatePolicy).build();
}

if (effectValue != null) {
Expand Down Expand Up @@ -312,7 +314,8 @@ public void updateChannelState(ChannelUID channel, State state) {
} else {
colorTempValue
.update(new QuantityType(Objects.requireNonNull(jsonState.colorTemp), Units.MIRED));
listener.updateChannelState(buildChannelUID(COLOR_TEMP_CHANNEL_ID),
listener.updateChannelState(buildChannelUID(
newStyleChannels ? COLOR_TEMP_CHANNEL_ID : COLOR_TEMP_CHANNEL_ID_DEPRECATED),
colorTempValue.getChannelState());

// Populate the color channel (if there is one) to match the color temperature.
Expand All @@ -339,7 +342,8 @@ public void updateChannelState(ChannelUID channel, State state) {
if (colorTempChannel != null) {
double kelvin = ColorUtil.xyToKelvin(xy);
colorTempValue.update(new QuantityType(kelvin, Units.KELVIN));
listener.updateChannelState(buildChannelUID(COLOR_TEMP_CHANNEL_ID),
listener.updateChannelState(buildChannelUID(
newStyleChannels ? COLOR_TEMP_CHANNEL_ID : COLOR_TEMP_CHANNEL_ID_DEPRECATED),
colorTempValue.getChannelState());
}
}
Expand Down Expand Up @@ -374,7 +378,9 @@ public void updateChannelState(ChannelUID channel, State state) {
final double[] xy = ColorUtil.hsbToXY(colorState);
double kelvin = ColorUtil.xyToKelvin(new double[] { xy[0], xy[1] });
colorTempValue.update(new QuantityType(kelvin, Units.KELVIN));
listener.updateChannelState(buildChannelUID(COLOR_TEMP_CHANNEL_ID),
listener.updateChannelState(
buildChannelUID(
newStyleChannels ? COLOR_TEMP_CHANNEL_ID : COLOR_TEMP_CHANNEL_ID_DEPRECATED),
colorTempValue.getChannelState());
}

Expand All @@ -383,7 +389,9 @@ public void updateChannelState(ChannelUID channel, State state) {
// https://github.com/home-assistant/core/blob/4f965f0eca09f0d12ae1c98c6786054063a36b44/homeassistant/components/mqtt/light/schema_json.py#L258
if (jsonState.colorTemp != null) {
colorTempValue.update(new QuantityType(Objects.requireNonNull(jsonState.colorTemp), Units.MIRED));
listener.updateChannelState(buildChannelUID(COLOR_TEMP_CHANNEL_ID),
listener.updateChannelState(
buildChannelUID(
newStyleChannels ? COLOR_TEMP_CHANNEL_ID : COLOR_TEMP_CHANNEL_ID_DEPRECATED),
colorTempValue.getChannelState());

colorModeValue.update(new StringType(LightColorMode.COLOR_MODE_COLOR_TEMP.serializedName()));
Expand Down Expand Up @@ -411,7 +419,9 @@ public void updateChannelState(ChannelUID channel, State state) {
logger.warn("Invalid color value for {}", getHaID());
}

listener.updateChannelState(buildChannelUID(COLOR_MODE_CHANNEL_ID), colorModeValue.getChannelState());
listener.updateChannelState(
buildChannelUID(newStyleChannels ? COLOR_MODE_CHANNEL_ID : COLOR_MODE_CHANNEL_ID_DEPRECATED),
colorModeValue.getChannelState());

if (localColorChannel != null) {
listener.updateChannelState(localColorChannel.getChannel().getUID(), colorValue.getChannelState());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,13 @@ public abstract class Light extends AbstractComponent<Light.ChannelConfiguration
protected static final String TEMPLATE_SCHEMA = "template";

protected static final String STATE_CHANNEL_ID = "state";
protected static final String ON_OFF_CHANNEL_ID = "on_off";
protected static final String SWITCH_CHANNEL_ID = "switch";
protected static final String SWITCH_CHANNEL_ID_DEPRECATED = "on_off";
protected static final String BRIGHTNESS_CHANNEL_ID = "brightness";
protected static final String COLOR_MODE_CHANNEL_ID = "color_mode";
protected static final String COLOR_TEMP_CHANNEL_ID = "color_temp";
protected static final String COLOR_MODE_CHANNEL_ID = "color-mode";
protected static final String COLOR_MODE_CHANNEL_ID_DEPRECATED = "color_mode";
protected static final String COLOR_TEMP_CHANNEL_ID = "color-temp";
protected static final String COLOR_TEMP_CHANNEL_ID_DEPRECATED = "color_temp";
protected static final String EFFECT_CHANNEL_ID = "effect";
// This channel is a synthetic channel that may send to other channels
// underneath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,14 @@ protected void buildChannels() {
"Brightness", this).commandTopic(DUMMY_TOPIC, true, 1)
.commandFilter(command -> handleCommand(command)).withAutoUpdatePolicy(autoUpdatePolicy).build();
} else {
onOffChannel = buildChannel(ON_OFF_CHANNEL_ID, ComponentChannelType.SWITCH, onOffValue, "On/Off State",
this).commandTopic(DUMMY_TOPIC, true, 1).commandFilter(command -> handleCommand(command))
.withAutoUpdatePolicy(autoUpdatePolicy).build();
onOffChannel = buildChannel(newStyleChannels ? SWITCH_CHANNEL_ID : SWITCH_CHANNEL_ID_DEPRECATED,
ComponentChannelType.SWITCH, onOffValue, "On/Off State", this).commandTopic(DUMMY_TOPIC, true, 1)
.commandFilter(command -> handleCommand(command)).withAutoUpdatePolicy(autoUpdatePolicy).build();
}

if (channelConfiguration.colorTempTemplate != null) {
buildChannel(COLOR_TEMP_CHANNEL_ID, ComponentChannelType.NUMBER, colorTempValue, "Color Temperature", this)
buildChannel(newStyleChannels ? COLOR_TEMP_CHANNEL_ID : COLOR_TEMP_CHANNEL_ID_DEPRECATED,
ComponentChannelType.NUMBER, colorTempValue, "Color Temperature", this)
.commandTopic(DUMMY_TOPIC, true, 1).commandFilter(command -> handleColorTempCommand(command))
.withAutoUpdatePolicy(autoUpdatePolicy).build();
}
Expand Down Expand Up @@ -284,7 +285,9 @@ public void updateChannelState(ChannelUID channel, State state) {
} else {
colorTempValue.update(new QuantityType(mireds, Units.MIRED));
}
listener.updateChannelState(buildChannelUID(COLOR_TEMP_CHANNEL_ID), colorTempValue.getChannelState());
listener.updateChannelState(
buildChannelUID(newStyleChannels ? COLOR_TEMP_CHANNEL_ID : COLOR_TEMP_CHANNEL_ID_DEPRECATED),
colorTempValue.getChannelState());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
@NonNullByDefault
public class Update extends AbstractComponent<Update.ChannelConfiguration> implements ChannelStateUpdateListener {
public static final String UPDATE_CHANNEL_ID = "update";
public static final String LATEST_VERSION_CHANNEL_ID = "latestVersion";
public static final String LATEST_VERSION_CHANNEL_ID = "latest-version";

/**
* Configuration class for MQTT component
Expand Down
Loading

0 comments on commit 2c89e60

Please sign in to comment.