From 11c595bb09a775a674f08fbef16cd15a304a5ffc Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 17 Sep 2025 14:38:02 -0500 Subject: [PATCH] [mqtt] Fix KeyError when MQTT logging configured without explicit level (#10774) --- esphome/__main__.py | 2 +- tests/unit_tests/test_main.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/esphome/__main__.py b/esphome/__main__.py index 0147a82530..f54fa8e3c6 100644 --- a/esphome/__main__.py +++ b/esphome/__main__.py @@ -212,7 +212,7 @@ def has_mqtt_logging() -> bool: if CONF_TOPIC not in log_topic: return False - return log_topic[CONF_LEVEL] != "NONE" + return log_topic.get(CONF_LEVEL, None) != "NONE" def has_mqtt() -> bool: diff --git a/tests/unit_tests/test_main.py b/tests/unit_tests/test_main.py index bfebb44545..fff0b2cd48 100644 --- a/tests/unit_tests/test_main.py +++ b/tests/unit_tests/test_main.py @@ -1226,6 +1226,18 @@ def test_has_mqtt_logging_no_log_topic() -> None: setup_core(config={}) assert has_mqtt_logging() is False + # Setup MQTT config with CONF_LOG_TOPIC but no CONF_LEVEL (regression test for #10771) + # This simulates the default configuration created by validate_config in the MQTT component + setup_core( + config={ + CONF_MQTT: { + CONF_BROKER: "mqtt.local", + CONF_LOG_TOPIC: {CONF_TOPIC: "esphome/debug"}, + } + } + ) + assert has_mqtt_logging() is True + def test_has_mqtt() -> None: """Test has_mqtt function."""