mirror of
https://github.com/esphome/esphome.git
synced 2026-06-01 01:19:45 +08:00
some basic tests
This commit is contained in:
@@ -1,5 +0,0 @@
|
|||||||
[run]
|
|
||||||
omit =
|
|
||||||
esphome/components/*
|
|
||||||
esphome/analyze_memory/*
|
|
||||||
tests/integration/*
|
|
||||||
@@ -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