mirror of
https://github.com/esphome/esphome.git
synced 2025-11-16 14:55:50 +00:00
[mqtt] Fix crash with empty broker during upload/logs (#11866)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
committed by
Jonathan Swoboda
parent
72da3d0f1e
commit
ff107a0674
91
tests/unit_tests/test_mqtt.py
Normal file
91
tests/unit_tests/test_mqtt.py
Normal file
@@ -0,0 +1,91 @@
|
||||
"""Unit tests for esphome.mqtt module."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from esphome.const import CONF_BROKER, CONF_ESPHOME, CONF_MQTT, CONF_NAME
|
||||
from esphome.core import EsphomeError
|
||||
from esphome.mqtt import get_esphome_device_ip
|
||||
|
||||
|
||||
def test_get_esphome_device_ip_empty_broker() -> None:
|
||||
"""Test that get_esphome_device_ip raises EsphomeError when broker is empty."""
|
||||
config = {
|
||||
CONF_MQTT: {
|
||||
CONF_BROKER: "",
|
||||
},
|
||||
CONF_ESPHOME: {
|
||||
CONF_NAME: "test-device",
|
||||
},
|
||||
}
|
||||
|
||||
with pytest.raises(
|
||||
EsphomeError,
|
||||
match="Cannot discover IP via MQTT as the broker is not configured",
|
||||
):
|
||||
get_esphome_device_ip(config)
|
||||
|
||||
|
||||
def test_get_esphome_device_ip_none_broker() -> None:
|
||||
"""Test that get_esphome_device_ip raises EsphomeError when broker is None."""
|
||||
config = {
|
||||
CONF_MQTT: {
|
||||
CONF_BROKER: None,
|
||||
},
|
||||
CONF_ESPHOME: {
|
||||
CONF_NAME: "test-device",
|
||||
},
|
||||
}
|
||||
|
||||
with pytest.raises(
|
||||
EsphomeError,
|
||||
match="Cannot discover IP via MQTT as the broker is not configured",
|
||||
):
|
||||
get_esphome_device_ip(config)
|
||||
|
||||
|
||||
def test_get_esphome_device_ip_missing_mqtt() -> None:
|
||||
"""Test that get_esphome_device_ip raises EsphomeError when mqtt config is missing."""
|
||||
config = {
|
||||
CONF_ESPHOME: {
|
||||
CONF_NAME: "test-device",
|
||||
},
|
||||
}
|
||||
|
||||
with pytest.raises(
|
||||
EsphomeError,
|
||||
match="Cannot discover IP via MQTT as the config does not include the mqtt:",
|
||||
):
|
||||
get_esphome_device_ip(config)
|
||||
|
||||
|
||||
def test_get_esphome_device_ip_missing_esphome() -> None:
|
||||
"""Test that get_esphome_device_ip raises EsphomeError when esphome config is missing."""
|
||||
config = {
|
||||
CONF_MQTT: {
|
||||
CONF_BROKER: "mqtt.local",
|
||||
},
|
||||
}
|
||||
|
||||
with pytest.raises(
|
||||
EsphomeError,
|
||||
match="Cannot discover IP via MQTT as the config does not include the device name:",
|
||||
):
|
||||
get_esphome_device_ip(config)
|
||||
|
||||
|
||||
def test_get_esphome_device_ip_missing_name() -> None:
|
||||
"""Test that get_esphome_device_ip raises EsphomeError when device name is missing."""
|
||||
config = {
|
||||
CONF_MQTT: {
|
||||
CONF_BROKER: "mqtt.local",
|
||||
},
|
||||
CONF_ESPHOME: {},
|
||||
}
|
||||
|
||||
with pytest.raises(
|
||||
EsphomeError,
|
||||
match="Cannot discover IP via MQTT as the config does not include the device name:",
|
||||
):
|
||||
get_esphome_device_ip(config)
|
||||
Reference in New Issue
Block a user