"""Test for DNS IP integration Init.""" from unittest.mock import patch from aiodns.error import DNSError import pytest from homeassistant.components.dnsip.const import ( CONF_HOSTNAME, CONF_IPV4, CONF_IPV6, CONF_PORT_IPV6, CONF_RESOLVER, CONF_RESOLVER_IPV6, DEFAULT_PORT, DOMAIN, ) from homeassistant.config_entries import SOURCE_USER, ConfigEntryState from homeassistant.const import CONF_NAME, CONF_PORT from homeassistant.core import HomeAssistant from . import RetrieveDNS from tests.common import MockConfigEntry async def test_load_unload_entry(hass: HomeAssistant) -> None: """Test load and unload an entry.""" entry = MockConfigEntry( domain=DOMAIN, source=SOURCE_USER, data={ CONF_HOSTNAME: "home-assistant.io", CONF_NAME: "home-assistant.io", CONF_IPV4: True, CONF_IPV6: False, }, options={ CONF_RESOLVER: "208.67.222.222", CONF_RESOLVER_IPV6: "2620:119:53::53", CONF_PORT: 53, CONF_PORT_IPV6: 53, }, entry_id="1", unique_id="home-assistant.io", ) entry.add_to_hass(hass) with patch( "homeassistant.components.dnsip.aiodns.DNSResolver", return_value=RetrieveDNS(), ): await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() assert entry.state is ConfigEntryState.LOADED assert await hass.config_entries.async_unload(entry.entry_id) await hass.async_block_till_done() assert entry.state is ConfigEntryState.NOT_LOADED async def test_port_migration( hass: HomeAssistant, ) -> None: """Test migration of the config entry from no ports to with ports.""" entry = MockConfigEntry( domain=DOMAIN, source=SOURCE_USER, data={ CONF_HOSTNAME: "home-assistant.io", CONF_NAME: "home-assistant.io", CONF_IPV4: True, CONF_IPV6: True, }, options={ CONF_RESOLVER: "208.67.222.222", CONF_RESOLVER_IPV6: "2620:119:53::53", }, entry_id="1", unique_id="home-assistant.io", version=1, minor_version=1, ) entry.add_to_hass(hass) with patch( "homeassistant.components.dnsip.aiodns.DNSResolver", side_effect=[RetrieveDNS(), RetrieveDNS()], ): await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() assert entry.version == 1 assert entry.minor_version == 3 assert entry.options[CONF_PORT] == DEFAULT_PORT assert entry.options[CONF_PORT_IPV6] == DEFAULT_PORT assert entry.state is ConfigEntryState.LOADED async def test_remove_unique_id_migration( hass: HomeAssistant, ) -> None: """Test migration of the config entry removing the unique_id.""" entry = MockConfigEntry( domain=DOMAIN, source=SOURCE_USER, data={ CONF_HOSTNAME: "home-assistant.io", CONF_NAME: "home-assistant.io", CONF_IPV4: True, CONF_IPV6: True, }, options={ CONF_RESOLVER: "208.67.222.222", CONF_RESOLVER_IPV6: "2620:119:53::53", CONF_PORT: DEFAULT_PORT, CONF_PORT_IPV6: DEFAULT_PORT, }, entry_id="1", unique_id="home-assistant.io", version=1, minor_version=2, ) entry.add_to_hass(hass) with patch( "homeassistant.components.dnsip.aiodns.DNSResolver", side_effect=[RetrieveDNS(), RetrieveDNS()], ): await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() assert entry.version == 1 assert entry.minor_version == 3 assert entry.unique_id is None assert entry.state is ConfigEntryState.LOADED async def test_migrate_error_from_future(hass: HomeAssistant) -> None: """Test a future version isn't migrated.""" entry = MockConfigEntry( domain=DOMAIN, source=SOURCE_USER, data={ CONF_HOSTNAME: "home-assistant.io", CONF_NAME: "home-assistant.io", CONF_IPV4: True, CONF_IPV6: True, "some_new_data": "new_value", }, options={ CONF_RESOLVER: "208.67.222.222", CONF_RESOLVER_IPV6: "2620:119:53::53", }, entry_id="1", unique_id="home-assistant.io", version=2, minor_version=1, ) entry.add_to_hass(hass) with patch( "homeassistant.components.dnsip.aiodns.DNSResolver", return_value=RetrieveDNS(), ): await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() entry = hass.config_entries.async_get_entry(entry.entry_id) assert entry.state is ConfigEntryState.MIGRATION_ERROR @pytest.mark.parametrize( "error", [ TimeoutError(), DNSError(), ], ) async def test_setup_dns_error(hass: HomeAssistant, error: Exception) -> None: """Test setup raises ConfigEntryNotReady when DNS lookup fails.""" entry = MockConfigEntry( domain=DOMAIN, source=SOURCE_USER, data={ CONF_HOSTNAME: "home-assistant.io", CONF_NAME: "home-assistant.io", CONF_IPV4: True, CONF_IPV6: False, }, options={ CONF_RESOLVER: "208.67.222.222", CONF_RESOLVER_IPV6: "2620:119:53::53", CONF_PORT: 53, CONF_PORT_IPV6: 53, }, entry_id="1", unique_id="home-assistant.io", ) entry.add_to_hass(hass) with patch( "homeassistant.components.dnsip.aiodns.DNSResolver", return_value=RetrieveDNS(error=error), ): await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() assert entry.state is ConfigEntryState.SETUP_RETRY async def test_setup_ipv6_only(hass: HomeAssistant) -> None: """Test setup with only IPv6 enabled creates only the IPv6 entity.""" entry = MockConfigEntry( domain=DOMAIN, source=SOURCE_USER, data={ CONF_HOSTNAME: "home-assistant.io", CONF_NAME: "home-assistant.io", CONF_IPV4: False, CONF_IPV6: True, }, options={ CONF_RESOLVER: "208.67.222.222", CONF_RESOLVER_IPV6: "2620:119:53::53", CONF_PORT: 53, CONF_PORT_IPV6: 53, }, entry_id="1", unique_id="home-assistant.io", ) entry.add_to_hass(hass) with patch( "homeassistant.components.dnsip.aiodns.DNSResolver", return_value=RetrieveDNS(), ): await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() assert entry.state is ConfigEntryState.LOADED assert hass.states.get("sensor.home_assistant_io_ipv6") is not None assert hass.states.get("sensor.home_assistant_io") is None async def test_setup_dns_timeout(hass: HomeAssistant) -> None: """Test setup raises ConfigEntryNotReady when DNS lookup times out.""" entry = MockConfigEntry( domain=DOMAIN, source=SOURCE_USER, data={ CONF_HOSTNAME: "home-assistant.io", CONF_NAME: "home-assistant.io", CONF_IPV4: True, CONF_IPV6: False, }, options={ CONF_RESOLVER: "208.67.222.222", CONF_RESOLVER_IPV6: "2620:119:53::53", CONF_PORT: 53, CONF_PORT_IPV6: 53, }, entry_id="1", unique_id="home-assistant.io", ) entry.add_to_hass(hass) with ( patch( "homeassistant.components.dnsip.aiodns.DNSResolver", return_value=RetrieveDNS(), ), patch( "homeassistant.components.dnsip.asyncio.timeout", side_effect=TimeoutError(), ), ): await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() assert entry.state is ConfigEntryState.SETUP_RETRY