async def test_port_is_available(hass): """Test we can get an available port and it is actually available.""" with patch( "homeassistant.components.homekit.util.socket.socket", return_value=_mock_socket(0), ): next_port = async_find_next_available_port(hass, DEFAULT_CONFIG_FLOW_PORT) assert next_port with patch( "homeassistant.components.homekit.util.socket.socket", return_value=_mock_socket(0), ): assert async_port_is_available(next_port) with patch( "homeassistant.components.homekit.util.socket.socket", return_value=_mock_socket(5), ): next_port = async_find_next_available_port(hass, DEFAULT_CONFIG_FLOW_PORT) assert next_port == DEFAULT_CONFIG_FLOW_PORT + 5 with patch( "homeassistant.components.homekit.util.socket.socket", return_value=_mock_socket(0), ): assert async_port_is_available(next_port) with patch( "homeassistant.components.homekit.util.socket.socket", return_value=_mock_socket(1), ): assert not async_port_is_available(next_port)
async def test_port_is_available_skips_existing_entries(hass): """Test we can get an available port and it is actually available.""" entry = MockConfigEntry( domain=DOMAIN, data={ CONF_NAME: BRIDGE_NAME, CONF_PORT: DEFAULT_CONFIG_FLOW_PORT }, options={}, ) entry.add_to_hass(hass) with patch( "homeassistant.components.homekit.util.socket.socket", return_value=_mock_socket(), ): next_port = async_find_next_available_port(hass, DEFAULT_CONFIG_FLOW_PORT) assert next_port == DEFAULT_CONFIG_FLOW_PORT + 1 with patch( "homeassistant.components.homekit.util.socket.socket", return_value=_mock_socket(), ): assert async_port_is_available(next_port) with patch( "homeassistant.components.homekit.util.socket.socket", return_value=_mock_socket(4), ): next_port = async_find_next_available_port(hass, DEFAULT_CONFIG_FLOW_PORT) assert next_port == DEFAULT_CONFIG_FLOW_PORT + 5 with patch( "homeassistant.components.homekit.util.socket.socket", return_value=_mock_socket(), ): assert async_port_is_available(next_port) with pytest.raises(OSError), patch( "homeassistant.components.homekit.util.socket.socket", return_value=_mock_socket(10), ): async_find_next_available_port(hass, 65530)