From 22c7ca99ad44798edec937a771ecf4f923e01394 Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Thu, 2 Jan 2025 09:00:35 -0700 Subject: [PATCH] [mqtt.homeassistant] Re-fix null component name (#18021) Re-fixes #15427, regressed from #17933 Signed-off-by: Cody Cutrer --- .../internal/component/AbstractComponent.java | 2 +- .../internal/component/SwitchTests.java | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/AbstractComponent.java b/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/AbstractComponent.java index a5c53775032..253f128c1e8 100644 --- a/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/AbstractComponent.java +++ b/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/AbstractComponent.java @@ -289,7 +289,7 @@ public abstract class AbstractComponent */ public String getName() { String result = channelConfiguration.getName(); - if (result.isBlank()) { + if (result != null && result.isBlank()) { result = null; } diff --git a/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/SwitchTests.java b/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/SwitchTests.java index e8ed86f05dd..fc37e573712 100644 --- a/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/SwitchTests.java +++ b/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/SwitchTests.java @@ -166,6 +166,54 @@ public class SwitchTests extends AbstractComponentTests { assertPublished("zigbee2mqtt/th1/set/auto_lock", "AUTO"); } + @Test + public void testSwitchNoName() { + var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC), """ + { + "availability": [ + { + "topic": "zigbee2mqtt/bridge/state", + "value_template": "{{ value_json.state }}" + }, + { + "topic": "zigbee2mqtt/Master Bedroom Subwoofer/availability", + "value_template": "{{ value_json.state }}" + } + ], + "availability_mode": "all", + "command_topic": "zigbee2mqtt/Master Bedroom Subwoofer/set", + "device": { + "configuration_url": "http://z2m:8084/#/device/0x00124b0029e7388c/info", + "identifiers": [ + "zigbee2mqtt_0x00124b0029e7388c" + ], + "manufacturer": "SONOFF", + "model": "15A Zigbee smart plug (S40ZBTPB)", + "name": "Master Bedroom Subwoofer", + "sw_version": "1.1.0", + "via_device": "zigbee2mqtt_bridge_0xe0798dfffe882ce4" + }, + "name": null, + "object_id": "master_bedroom_subwoofer", + "origin": { + "name": "Zigbee2MQTT", + "sw": "1.42.0-dev", + "url": "https://www.zigbee2mqtt.io" + }, + "payload_off": "OFF", + "payload_on": "ON", + "state_topic": "zigbee2mqtt/Master Bedroom Subwoofer", + "unique_id": "0x00124b0029e7388c_switch_zigbee2mqtt", + "value_template": "{{ value_json.state }}" + } + """); + + assertThat(component.channels.size(), is(1)); + assertThat(component.getName(), is("Master Bedroom Subwoofer")); + assertChannel(component, Switch.SWITCH_CHANNEL_ID, "zigbee2mqtt/Master Bedroom Subwoofer", + "zigbee2mqtt/Master Bedroom Subwoofer/set", "Master Bedroom Subwoofer", OnOffValue.class); + } + @Override protected Set getConfigTopics() { return Set.of(CONFIG_TOPIC);