"""Setup/unload ConfigEntry: клиент стартует, останавливается, ошибки порта.""" from __future__ import annotations import socket from homeassistant.core import HomeAssistant from pytest_homeassistant_custom_component.common import MockConfigEntry from custom_components.fglair.const import CONF_LISTEN_PORT, DOMAIN from pyfglair import Prop, State async def test_setup_unload(hass: HomeAssistant, config_entry, socket_enabled): config_entry.add_to_hass(hass) assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() client = config_entry.runtime_data assert client is not None assert isinstance(client.state, State) assert await hass.config_entries.async_unload(config_entry.entry_id) await hass.async_block_till_done() assert client.cached(Prop.OPERATION_MODE) is None async def test_setup_port_busy(hass: HomeAssistant, entry_data, socket_enabled): sock = socket.socket() sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 0) sock.bind(("127.0.0.1", entry_data[CONF_LISTEN_PORT])) sock.listen(1) try: entry = MockConfigEntry( domain=DOMAIN, data=entry_data, unique_id=entry_data["dsn"] ) entry.add_to_hass(hass) assert not await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() assert entry.state.name == "SETUP_RETRY" finally: sock.close()