mirror of
https://github.com/esphome/esphome.git
synced 2025-11-15 06:15:47 +00:00
92 lines
2.4 KiB
Python
92 lines
2.4 KiB
Python
"""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)
|