"""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)