Skip to content

Commit

Permalink
[mqtt.homeassistant] Fix components with an empty name (openhab#17933)
Browse files Browse the repository at this point in the history
As opposed to null name. In either case, it's not usable,
and we need to use our fallbacks.

Signed-off-by: Cody Cutrer <[email protected]>
  • Loading branch information
ccutrer authored Dec 20, 2024
1 parent 7a3380a commit fc9e564
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public void resolveConflict() {

protected ComponentChannel.Builder buildChannel(String channelID, ComponentChannelType channelType,
Value valueState, String label, ChannelStateUpdateListener channelStateUpdateListener) {
if (groupId == null) {
if (groupId == null && newStyleChannels) {
channelID = componentId;
}
return new ComponentChannel.Builder(this, channelID, channelType.getChannelTypeUID(), valueState, label,
Expand Down Expand Up @@ -304,12 +304,15 @@ public String getUniqueId() {
*/
public String getName() {
String result = channelConfiguration.getName();
if (result.isBlank()) {
result = null;
}

Device device = channelConfiguration.getDevice();
if (result == null && device != null) {
result = device.getName();
}
if (result == null) {
if (result == null || result.isBlank()) {
result = haID.objectID;
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
@NonNullByDefault
public class ClimateTests extends AbstractComponentTests {
public static final String CONFIG_TOPIC = "climate/0x847127fffe11dd6a_climate_zigbee2mqtt";
public static final String TION_CONFIG_TOPIC = "climate/living-room-tion-3s/living-room-tion-3s";

@SuppressWarnings("null")
@Test
Expand Down Expand Up @@ -397,8 +398,82 @@ public void testClimateHumidity() {
assertState(component, Climate.TARGET_HUMIDITY_CH_ID, new QuantityType<>(50, Units.PERCENT));
}

@SuppressWarnings("null")
@Test
public void testClimateWithEmptyName() {
var component = discoverComponent(configTopicToMqtt(TION_CONFIG_TOPIC), """
{
"curr_temp_t": "living-room-tion-3s/climate/living-room-tion-3s/current_temperature/state",
"mode_cmd_t": "living-room-tion-3s/climate/living-room-tion-3s/mode/command",
"mode_stat_t": "living-room-tion-3s/climate/living-room-tion-3s/mode/state",
"modes": [
"off",
"heat",
"fan_only",
"heat_cool"
],
"temp_cmd_t": "living-room-tion-3s/climate/living-room-tion-3s/target_temperature/command",
"temp_stat_t": "living-room-tion-3s/climate/living-room-tion-3s/target_temperature/state",
"min_temp": 1,
"max_temp": 25,
"temp_step": 1,
"precision": 1,
"temp_unit": "C",
"min_hum": 30,
"max_hum": 99,
"act_t": "living-room-tion-3s/climate/living-room-tion-3s/action/state",
"fan_mode_cmd_t": "living-room-tion-3s/climate/living-room-tion-3s/fan_mode/command",
"fan_mode_stat_t": "living-room-tion-3s/climate/living-room-tion-3s/fan_mode/state",
"fan_modes": [
"1",
"2",
"3",
"4",
"5",
"6"
],
"name": "",
"ic": "mdi:air-filter",
"avty_t": "living-room-tion-3s/status",
"uniq_id": "ESPclimateliving-room-tion-3s",
"dev": {
"ids": "f09e9e213ab0",
"name": "living-room-tion-3s",
"sw": "2024.8.0 (ESPHome 2024.11.3)",
"mdl": "tion",
"mf": "dentra",
"cns": [
[
"mac",
"f09e9e213ab0"
]
]
}
}
""");

assertThat(component.channels.size(), is(5));
assertThat(component.getName(), is("living-room-tion-3s"));

assertChannel(component, Climate.ACTION_CH_ID, "living-room-tion-3s/climate/living-room-tion-3s/action/state",
"", "living-room-tion-3s", TextValue.class);
assertChannel(component, Climate.CURRENT_TEMPERATURE_CH_ID_DEPRECATED,
"living-room-tion-3s/climate/living-room-tion-3s/current_temperature/state", "", "living-room-tion-3s",
NumberValue.class);
assertChannel(component, Climate.FAN_MODE_CH_ID_DEPRECATED,
"living-room-tion-3s/climate/living-room-tion-3s/fan_mode/state",
"living-room-tion-3s/climate/living-room-tion-3s/fan_mode/command", "living-room-tion-3s",
TextValue.class);
assertChannel(component, Climate.MODE_CH_ID, "living-room-tion-3s/climate/living-room-tion-3s/mode/state",
"living-room-tion-3s/climate/living-room-tion-3s/mode/command", "living-room-tion-3s", TextValue.class);
assertChannel(component, Climate.TEMPERATURE_CH_ID,
"living-room-tion-3s/climate/living-room-tion-3s/target_temperature/state",
"living-room-tion-3s/climate/living-room-tion-3s/target_temperature/command", "living-room-tion-3s",
NumberValue.class);
}

@Override
protected Set<String> getConfigTopics() {
return Set.of(CONFIG_TOPIC);
return Set.of(CONFIG_TOPIC, TION_CONFIG_TOPIC);
}
}

0 comments on commit fc9e564

Please sign in to comment.